-
Notifications
You must be signed in to change notification settings - Fork 748
Add PythonEngine Eval and Exec #389
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using Python.Runtime; | ||
|
||
namespace Python.EmbeddingTest | ||
{ | ||
public class RunStringTest | ||
{ | ||
private Py.GILState gil; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
gil = Py.GIL(); | ||
} | ||
|
||
[TearDown] | ||
public void Dispose() | ||
{ | ||
gil.Dispose(); | ||
} | ||
|
||
[Test] | ||
public void TestRunSimpleString() | ||
{ | ||
int aa = PythonEngine.RunSimpleString("import sys"); | ||
Assert.AreEqual(0, aa); | ||
|
||
int bb = PythonEngine.RunSimpleString("import 1234"); | ||
Assert.AreEqual(-1, bb); | ||
} | ||
|
||
[Test] | ||
public void TestEval() | ||
{ | ||
dynamic sys = Py.Import("sys"); | ||
sys.attr1 = 100; | ||
var locals = new PyDict(); | ||
locals.SetItem("sys", sys); | ||
locals.SetItem("a", new PyInt(10)); | ||
|
||
object b = PythonEngine.Eval("sys.attr1 + a + 1", null, locals.Handle) | ||
.AsManagedObject(typeof(int)); | ||
Assert.AreEqual(111, b); | ||
} | ||
|
||
[Test] | ||
public void TestExec() | ||
{ | ||
dynamic sys = Py.Import("sys"); | ||
sys.attr1 = 100; | ||
var locals = new PyDict(); | ||
locals.SetItem("sys", sys); | ||
locals.SetItem("a", new PyInt(10)); | ||
|
||
PythonEngine.Exec("c = sys.attr1 + a + 1", null, locals.Handle); | ||
object c = locals.GetItem("c").AsManagedObject(typeof(int)); | ||
Assert.AreEqual(111, c); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,11 +156,7 @@ public static void Initialize(IEnumerable<string> args) | |
string code = | ||
"import atexit, clr\n" + | ||
"atexit.register(clr._AtExit)\n"; | ||
PyObject r = PythonEngine.RunString(code); | ||
if (r != null) | ||
{ | ||
r.Dispose(); | ||
} | ||
PythonEngine.Exec(code); | ||
|
||
// Load the clr.py resource into the clr module | ||
IntPtr clr = Python.Runtime.ImportHook.GetCLRModule(); | ||
|
@@ -180,12 +176,7 @@ public static void Initialize(IEnumerable<string> args) | |
{ | ||
// add the contents of clr.py to the module | ||
string clr_py = reader.ReadToEnd(); | ||
PyObject result = RunString(clr_py, module_globals, locals.Handle); | ||
if (null == result) | ||
{ | ||
throw new PythonException(); | ||
} | ||
result.Dispose(); | ||
Exec(clr_py, module_globals, locals.Handle); | ||
} | ||
|
||
// add the imported module to the clr module, and copy the API functions | ||
|
@@ -253,11 +244,7 @@ public static void InitExt() | |
" exec(line)\n" + | ||
" break\n"; | ||
|
||
PyObject r = PythonEngine.RunString(code); | ||
if (r != null) | ||
{ | ||
r.Dispose(); | ||
} | ||
PythonEngine.Exec(code); | ||
} | ||
catch (PythonException e) | ||
{ | ||
|
@@ -405,6 +392,38 @@ public static PyObject ModuleFromString(string name, string code) | |
} | ||
|
||
|
||
/// <summary> | ||
/// Eval Method | ||
/// </summary> | ||
/// <remarks> | ||
/// Evaluate a Python expression and returns the result. | ||
/// It's a subset of Python eval function. | ||
/// </remarks> | ||
public static PyObject Eval(string code, IntPtr? globals = null, IntPtr? locals = null) | ||
{ | ||
PyObject result = RunString(code, globals, locals, RunFlagType.Eval); | ||
return result; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Exec Method | ||
/// </summary> | ||
/// <remarks> | ||
/// Run a string containing Python code. | ||
/// It's a subset of Python exec function. | ||
/// </remarks> | ||
public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = null) | ||
{ | ||
PyObject result = RunString(code, globals, locals, RunFlagType.File); | ||
if (result.obj != Runtime.PyNone) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @filmor then on the flip-side, is this check here then superfluous? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vmuriart It can be removed. RunString method will always return None when passing the last parameter Runtime.Py_file_input. So this check failed only when there is a runtime bug of CPython. |
||
{ | ||
throw new PythonException(); | ||
} | ||
result.Dispose(); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// RunString Method | ||
/// </summary> | ||
|
@@ -414,7 +433,7 @@ public static PyObject ModuleFromString(string name, string code) | |
/// an exception was raised. | ||
/// </remarks> | ||
public static PyObject RunString( | ||
string code, IntPtr? globals = null, IntPtr? locals = null | ||
string code, IntPtr? globals = null, IntPtr? locals = null, RunFlagType _flag = RunFlagType.File | ||
) | ||
{ | ||
var borrowedGlobals = true; | ||
|
@@ -439,7 +458,7 @@ public static PyObject RunString( | |
borrowedLocals = false; | ||
} | ||
|
||
var flag = (IntPtr)257; /* Py_file_input */ | ||
var flag = (IntPtr)_flag; | ||
|
||
try | ||
{ | ||
|
@@ -465,6 +484,13 @@ public static PyObject RunString( | |
} | ||
} | ||
|
||
public enum RunFlagType | ||
{ | ||
Single = 256, | ||
File = 257, /* Py_file_input */ | ||
Eval = 258 | ||
} | ||
|
||
public static class Py | ||
{ | ||
public static GILState GIL() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the result be checked for exception before returning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed,
RunString
already does that viaPy.Throw
.