Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to run Python code from a string #340

Merged
merged 5 commits into from
Jan 22, 2025
Merged

Conversation

tonybaloney
Copy link
Owner

@tonybaloney tonybaloney commented Jan 15, 2025

Implements #290

Adds two different types of API:

  • IPythonEnvironment.ExecuteExpression which takes a simple expression (1+1) and returns the result
  • IPythonEnvironment.Execute which takes a string, typically multiple lines

@tonybaloney tonybaloney requested a review from Copilot January 15, 2025 04:47
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

@@ -144,4 +145,34 @@ public bool IsDisposed()
{
return disposedValue;
}

public PyObject Execute(string code)
Copy link
Preview

Copilot AI Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method does not handle potential exceptions that could be thrown by CPythonAPI.PyRun_String. Consider adding exception handling to catch and log errors.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
}
}

public PyObject Execute(string code, IDictionary<string, PyObject> locals)
Copy link
Preview

Copilot AI Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method does not handle potential exceptions that could be thrown by CPythonAPI.PyRun_String. Consider adding exception handling to catch and log errors.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
}
}

public PyObject Execute(string code, IDictionary<string, PyObject> globals, IDictionary<string, PyObject> locals)
Copy link
Preview

Copilot AI Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method does not handle potential exceptions that could be thrown by CPythonAPI.PyRun_String. Consider adding exception handling to catch and log errors.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@tonybaloney tonybaloney changed the title Add run string function Allow users to run Python code from a string Jan 15, 2025
Copy link
Collaborator

@aaronpowell aaronpowell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the code itself looks fine I question the general value of this, as what role is .NET really playing here? Like, you really need to know the inputs and outputs beforehand so that you can read/write them, or you just pass through arbitrary code with .NET not really "touching" anything at either end (which seems inherently dangerous)

@tonybaloney
Copy link
Owner Author

This would be used for edge cases, like running complicated expressions on an object that you've got back from CSnakes, or dynamically running a bit of interpreted code on the module.

@aaronpowell
Copy link
Collaborator

Yeah, I read the issue so I "get" the use-case but it's the kind of thing I wonder if CSnakes is the best tool for, since .NET isn't exactly idea for a scripting environment.

@tonybaloney tonybaloney merged commit 3c318eb into main Jan 22, 2025
40 checks passed
@tonybaloney tonybaloney deleted the abitrary_code branch January 22, 2025 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants