| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent edc7621 commit ea44eef
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -86,6 +86,7 @@ | |||
| 86 | 86 | <Compile Include="pyiter.cs" /> | |
| 87 | 87 | <Compile Include="pylong.cs" /> | |
| 88 | 88 | <Compile Include="pyobject.cs" /> | |
| 89 | + <Compile Include="pyrunstring.cs" /> | ||
| 89 | 90 | <Compile Include="pythonexception.cs" /> | |
| 90 | 91 | <Compile Include="pytuple.cs" /> | |
| 91 | 92 | </ItemGroup> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,61 @@ | |||
| 1 | + using System; | ||
| 2 | + using NUnit.Framework; | ||
| 3 | + using Python.Runtime; | ||
| 4 | + | ||
| 5 | + namespace Python.EmbeddingTest | ||
| 6 | + { | ||
| 7 | + public class RunStringTest | ||
| 8 | + { | ||
| 9 | + private Py.GILState gil; | ||
| 10 | + | ||
| 11 | + [SetUp] | ||
| 12 | + public void SetUp() | ||
| 13 | + { | ||
| 14 | + gil = Py.GIL(); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + [TearDown] | ||
| 18 | + public void Dispose() | ||
| 19 | + { | ||
| 20 | + gil.Dispose(); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + [Test] | ||
| 24 | + public void TestRunSimpleString() | ||
| 25 | + { | ||
| 26 | + int aa = PythonEngine.RunSimpleString("import sys"); | ||
| 27 | + Assert.AreEqual(aa, 0); | ||
| 28 | + | ||
| 29 | + int bb = PythonEngine.RunSimpleString("import 1234"); | ||
| 30 | + Assert.AreEqual(bb, -1); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + [Test] | ||
| 34 | + public void TestEval() | ||
| 35 | + { | ||
| 36 | + dynamic sys = Py.Import("sys"); | ||
| 37 | + sys.attr1 = 100; | ||
| 38 | + var locals = new PyDict(); | ||
| 39 | + locals.SetItem("sys", sys); | ||
| 40 | + locals.SetItem("a", new PyInt(10)); | ||
| 41 | + | ||
| 42 | + object b = PythonEngine.Eval("sys.attr1 + a + 1", null, locals.Handle) | ||
| 43 | + .AsManagedObject(typeof(Int32)); | ||
| 44 | + Assert.AreEqual(b, 111); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + [Test] | ||
| 48 | + public void TestExec() | ||
| 49 | + { | ||
| 50 | + dynamic sys = Py.Import("sys"); | ||
| 51 | + sys.attr1 = 100; | ||
| 52 | + var locals = new PyDict(); | ||
| 53 | + locals.SetItem("sys", sys); | ||
| 54 | + locals.SetItem("a", new PyInt(10)); | ||
| 55 | + | ||
| 56 | + PythonEngine.Exec("c = sys.attr1 + a + 1", null, locals.Handle); | ||
| 57 | + object c = locals.GetItem("c").AsManagedObject(typeof(Int32)); | ||
| 58 | + Assert.AreEqual(c, 111); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -156,11 +156,7 @@ public static void Initialize(IEnumerable<string> args) | |||
| 156 | 156 | string code = | |
| 157 | 157 | "import atexit, clr\n" + | |
| 158 | 158 | "atexit.register(clr._AtExit)\n"; | |
| 159 | - PyObject r = PythonEngine.RunString(code); | ||
| 160 | - if (r != null) | ||
| 161 | - { | ||
| 162 | - r.Dispose(); | ||
| 163 | - } | ||
| 159 | + PythonEngine.Exec(code); | ||
| 164 | 160 | ||
| 165 | 161 | // Load the clr.py resource into the clr module | |
| 166 | 162 | IntPtr clr = Python.Runtime.ImportHook.GetCLRModule(); | |
@@ -180,12 +176,7 @@ public static void Initialize(IEnumerable<string> args) | |||
| 180 | 176 | { | |
| 181 | 177 | // add the contents of clr.py to the module | |
| 182 | 178 | string clr_py = reader.ReadToEnd(); | |
| 183 | - PyObject result = RunString(clr_py, module_globals, locals.Handle); | ||
| 184 | - if (null == result) | ||
| 185 | - { | ||
| 186 | - throw new PythonException(); | ||
| 187 | - } | ||
| 188 | - result.Dispose(); | ||
| 179 | + Exec(clr_py, module_globals, locals.Handle); | ||
| 189 | 180 | } | |
| 190 | 181 | ||
| 191 | 182 | // add the imported module to the clr module, and copy the API functions | |
@@ -253,11 +244,7 @@ public static void InitExt() | |||
| 253 | 244 | " exec(line)\n" + | |
| 254 | 245 | " break\n"; | |
| 255 | 246 | ||
| 256 | - PyObject r = PythonEngine.RunString(code); | ||
| 257 | - if (r != null) | ||
| 258 | - { | ||
| 259 | - r.Dispose(); | ||
| 260 | - } | ||
| 247 | + PythonEngine.Exec(code); | ||
| 261 | 248 | } | |
| 262 | 249 | catch (PythonException e) | |
| 263 | 250 | { | |
@@ -405,6 +392,38 @@ public static PyObject ModuleFromString(string name, string code) | |||
| 405 | 392 | } | |
| 406 | 393 | ||
| 407 | 394 | ||
| 395 | + /// <summary> | ||
| 396 | + /// Eval Method | ||
| 397 | + /// </summary> | ||
| 398 | + /// <remarks> | ||
| 399 | + /// Evaluate a Python expression and returns the result. | ||
| 400 | + /// It's a subset of Python eval function. | ||
| 401 | + /// </remarks> | ||
| 402 | + public static PyObject Eval(string code, IntPtr? globals = null, IntPtr? locals = null) | ||
| 403 | + { | ||
| 404 | + PyObject result = RunString(code, globals, locals, RunFlagType.Eval); | ||
| 405 | + return result; | ||
| 406 | + } | ||
| 407 | + | ||
| 408 | + | ||
| 409 | + /// <summary> | ||
| 410 | + /// Exec Method | ||
| 411 | + /// </summary> | ||
| 412 | + /// <remarks> | ||
| 413 | + /// Run a string containing Python code. | ||
| 414 | + /// It's a subset of Python exec function. | ||
| 415 | + /// </remarks> | ||
| 416 | + public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = null) | ||
| 417 | + { | ||
| 418 | + PyObject result = RunString(code, globals, locals, RunFlagType.File); | ||
| 419 | + if (result.obj != Runtime.PyNone) | ||
| 420 | + { | ||
| 421 | + throw new PythonException(); | ||
| 422 | + } | ||
| 423 | + result.Dispose(); | ||
| 424 | + } | ||
| 425 | + | ||
| 426 | + | ||
| 408 | 427 | /// <summary> | |
| 409 | 428 | /// RunString Method | |
| 410 | 429 | /// </summary> | |
@@ -413,8 +432,8 @@ public static PyObject ModuleFromString(string name, string code) | |||
| 413 | 432 | /// executing the code string as a PyObject instance, or null if | |
| 414 | 433 | /// an exception was raised. | |
| 415 | 434 | /// </remarks> | |
| 416 | - public static PyObject RunString( | ||
| 417 | - string code, IntPtr? globals = null, IntPtr? locals = null | ||
| 435 | + internal static PyObject RunString( | ||
| 436 | + string code, IntPtr? globals = null, IntPtr? locals = null, RunFlagType _flag = RunFlagType.File | ||
| 418 | 437 | ) | |
| 419 | 438 | { | |
| 420 | 439 | var borrowedGlobals = true; | |
@@ -439,7 +458,7 @@ public static PyObject RunString( | |||
| 439 | 458 | borrowedLocals = false; | |
| 440 | 459 | } | |
| 441 | 460 | ||
| 442 | - var flag = (IntPtr)257; /* Py_file_input */ | ||
| 461 | + var flag = (IntPtr)_flag; | ||
| 443 | 462 | ||
| 444 | 463 | try | |
| 445 | 464 | { | |
@@ -465,6 +484,13 @@ public static PyObject RunString( | |||
| 465 | 484 | } | |
| 466 | 485 | } | |
| 467 | 486 | ||
| 487 | + public enum RunFlagType | ||
| 488 | + { | ||
| 489 | + Single = 256, | ||
| 490 | + File = 257, /* Py_file_input */ | ||
| 491 | + Eval = 258 | ||
| 492 | + } | ||
| 493 | + | ||
| 468 | 494 | public static class Py | |
| 469 | 495 | { | |
| 470 | 496 | public static GILState GIL() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments