Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vmuriart merged 2 commits intopythonnet:masterfromyagweb:add_eval
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionssrc/embed_tests/Python.EmbeddingTest.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/embed_tests/pyrunstring.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/runtime/pythonengine.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 viaPy.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
ContributorAuthor

@yagwebyagwebFeb 23, 2017
edited
Loading

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

[8]ページ先頭

©2009-2025 Movatter.jp