@@ -156,11 +156,7 @@ public static void Initialize(IEnumerable<string> args)
156156 string code =
157157 "import atexit, clr\n " +
158158 "atexit.register(clr._AtExit)\n " ;
159- PyObject r = PythonEngine . RunString ( code ) ;
160- if ( r != null )
161- {
162- r . Dispose ( ) ;
163- }
159+ PythonEngine . Exec ( code ) ;
164160
165161 // Load the clr.py resource into the clr module
166162 IntPtr clr = Python . Runtime . ImportHook . GetCLRModule ( ) ;
@@ -180,12 +176,7 @@ public static void Initialize(IEnumerable<string> args)
180176 {
181177 // add the contents of clr.py to the module
182178 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 ) ;
189180 }
190181
191182 // add the imported module to the clr module, and copy the API functions
@@ -253,11 +244,7 @@ public static void InitExt()
253244 " exec(line)\n " +
254245 " break\n " ;
255246
256- PyObject r = PythonEngine . RunString ( code ) ;
257- if ( r != null )
258- {
259- r . Dispose ( ) ;
260- }
247+ PythonEngine . Exec ( code ) ;
261248 }
262249 catch ( PythonException e )
263250 {
@@ -405,6 +392,38 @@ public static PyObject ModuleFromString(string name, string code)
405392 }
406393
407394
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+
408427 /// <summary>
409428 /// RunString Method
410429 /// </summary>
@@ -413,8 +432,8 @@ public static PyObject ModuleFromString(string name, string code)
413432 /// executing the code string as a PyObject instance, or null if
414433 /// an exception was raised.
415434 /// </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
418437 )
419438 {
420439 var borrowedGlobals = true ;
@@ -439,7 +458,7 @@ public static PyObject RunString(
439458 borrowedLocals = false ;
440459 }
441460
442- var flag = ( IntPtr ) 257 ; /* Py_file_input */
461+ var flag = ( IntPtr ) _flag ;
443462
444463 try
445464 {
@@ -465,6 +484,13 @@ public static PyObject RunString(
465484 }
466485 }
467486
487+ public enum RunFlagType
488+ {
489+ Single = 256 ,
490+ File = 257 , /* Py_file_input */
491+ Eval = 258
492+ }
493+
468494 public static class Py
469495 {
470496 public static GILState GIL ( )
0 commit comments