Skip to content

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

Merged
merged 2 commits into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="pyiter.cs" />
<Compile Include="pylong.cs" />
<Compile Include="pyobject.cs" />
<Compile Include="pyrunstring.cs" />
<Compile Include="pythonexception.cs" />
<Compile Include="pytuple.cs" />
</ItemGroup>
Expand Down
61 changes: 61 additions & 0 deletions src/embed_tests/pyrunstring.cs
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);
}
}
}
62 changes: 44 additions & 18 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Copy link
Contributor

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?

Copy link
Member

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 via Py.Throw.

}


/// <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)
Copy link
Contributor

Choose a reason for hiding this comment

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

@filmor then on the flip-side, is this check here then superfluous?

Copy link
Contributor Author

@yagweb yagweb Feb 23, 2017

Choose a reason for hiding this comment

The 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>
Expand All @@ -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;
Expand All @@ -439,7 +458,7 @@ public static PyObject RunString(
borrowedLocals = false;
}

var flag = (IntPtr)257; /* Py_file_input */
var flag = (IntPtr)_flag;

try
{
Expand All @@ -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()
Expand Down