-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Seperate APIs for simple expression and multiple lines. Make them ext…
…ension methods.
- Loading branch information
1 parent
e863786
commit 93aa143
Showing
4 changed files
with
98 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
| ||
using CSnakes.Runtime.CPython; | ||
using CSnakes.Runtime.Python; | ||
|
||
namespace CSnakes.Runtime; | ||
public static class PythonRunString | ||
{ | ||
/// <summary> | ||
/// Execute a single expression in Python and return the result, | ||
/// e.g. `1 + 1` or `len([1, 2, 3])` | ||
/// </summary> | ||
/// <param name="code">The Python code</param> | ||
/// <returns>The resulting Python object</returns> | ||
public static PyObject ExecuteExpression(this IPythonEnvironment env, string code) | ||
{ | ||
using (GIL.Acquire()) | ||
{ | ||
using var globals = PyObject.Create(CPythonAPI.PyDict_New()); | ||
using var locals = PyObject.Create(CPythonAPI.PyDict_New()); | ||
return CPythonAPI.PyRun_String(code, CPythonAPI.InputType.Py_eval_input, globals, locals); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Execute a single expression in Python and return the result, with locals | ||
/// e.g. `1 + b` | ||
/// </summary> | ||
/// <param name="code">The Python code</param> | ||
/// <param name="locals">A dictionary of local variables</param> | ||
/// <returns>The resulting Python object</returns> | ||
public static PyObject ExecuteExpression(this IPythonEnvironment env, string code, IDictionary<string, PyObject> locals) | ||
{ | ||
using (GIL.Acquire()) | ||
{ | ||
using var localsPyDict = PyObject.From(locals); | ||
using var globalsPyDict = PyObject.Create(CPythonAPI.PyDict_New()); | ||
return CPythonAPI.PyRun_String(code, CPythonAPI.InputType.Py_eval_input, globalsPyDict, localsPyDict); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Execute a single expression in Python and return the result, with locals | ||
/// e.g. `1 + b` | ||
/// </summary> | ||
/// <param name="code">The Python code</param> | ||
/// <param name="locals">A dictionary of local variables</param> | ||
/// <param name="globals">A dictionary of global variables</param> | ||
/// <returns>The resulting Python object</returns> | ||
public static PyObject ExecuteExpression(this IPythonEnvironment env, string code, IDictionary<string, PyObject> locals, IDictionary<string, PyObject> globals) | ||
{ | ||
using (GIL.Acquire()) | ||
{ | ||
using var localsPyDict = PyObject.From(locals); | ||
using var globalsPyDict = PyObject.From(globals); | ||
return CPythonAPI.PyRun_String(code, CPythonAPI.InputType.Py_eval_input, globalsPyDict, localsPyDict); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Execute Python program from a string, typically multiple lines of code | ||
/// </summary> | ||
/// <param name="code">The Python code</param> | ||
/// <param name="locals">A dictionary of local variables</param> | ||
/// <param name="globals">A dictionary of global variables</param> | ||
/// <returns>Normally nothing</returns> | ||
public static PyObject Execute(this IPythonEnvironment env, string code, IDictionary<string, PyObject> locals, IDictionary<string, PyObject> globals) | ||
{ | ||
using (GIL.Acquire()) | ||
{ | ||
using var localsPyDict = PyObject.From(locals); | ||
using var globalsPyDict = PyObject.From(globals); | ||
return CPythonAPI.PyRun_String(code, CPythonAPI.InputType.Py_file_input, globalsPyDict, localsPyDict); | ||
} | ||
} | ||
} |