public static PyObject Compile(string code, string filename = "", RunFlagType mode = RunFlagType.File) { var flag = (int)mode; IntPtr ptr = Runtime.Py_CompileString(code, filename, flag); PythonException.ThrowIfIsNull(ptr); return(new PyObject(ptr)); }
public static PyObject Compile(string code, string filename = "", RunFlagType mode = RunFlagType.File) { var flag = (IntPtr)mode; IntPtr ptr = Runtime.Py_CompileString(code, filename, flag); Runtime.CheckExceptionOccurred(); return(new PyObject(ptr)); }
/// <summary> /// Internal RunString Method. /// </summary> /// <remarks> /// Run a string containing Python code. Returns the result of /// executing the code string as a PyObject instance, or null if /// an exception was raised. /// </remarks> internal static PyObject RunString(string code, IntPtr?globals, IntPtr?locals, RunFlagType flag) { var borrowedGlobals = true; if (globals == null) { globals = Runtime.PyEval_GetGlobals(); if (globals == IntPtr.Zero) { globals = Runtime.PyDict_New(); Runtime.PyDict_SetItemString( globals.Value, "__builtins__", Runtime.PyEval_GetBuiltins() ); borrowedGlobals = false; } } if (locals == null) { locals = globals; } try { NewReference result = Runtime.PyRun_String( code, (IntPtr)flag, globals.Value, locals.Value ); PythonException.ThrowIfIsNull(result); return(result.MoveToPyObject()); } finally { if (!borrowedGlobals) { Runtime.XDecref(globals.Value); } } }
/// <summary> /// Internal RunString Method. /// </summary> /// <remarks> /// Run a string containing Python code. Returns the result of /// executing the code string as a PyObject instance, or null if /// an exception was raised. /// </remarks> internal static PyObject RunString(string code, IntPtr?globals, IntPtr?locals, RunFlagType flag) { var borrowedGlobals = true; if (globals == null) { globals = Runtime.PyEval_GetGlobals(); if (globals == IntPtr.Zero) { globals = Runtime.PyDict_New(); Runtime.PyDict_SetItemString( globals.Value, "__builtins__", Runtime.PyEval_GetBuiltins() ); borrowedGlobals = false; } } if (locals == null) { locals = globals; } try { IntPtr result = Runtime.PyRun_String( code, (IntPtr)flag, globals.Value, locals.Value ); Runtime.CheckExceptionOccurred(); return(new PyObject(result)); } finally { if (!borrowedGlobals) { Runtime.XDecref(globals.Value); } } }
public static PyObject CompileToModule(string name, string code, string filename = "", RunFlagType mode = RunFlagType.File) { var flag = (IntPtr)mode; IntPtr ptr = Runtime.Py_CompileString(code, filename, flag); Runtime.CheckExceptionOccurred(); IntPtr m = Runtime.PyImport_ExecCodeModule(name, ptr); Runtime.CheckExceptionOccurred(); PyObject res = new PyObject(m); if (!objs.ContainsKey(name)) { objs.TryAdd(name, res); } return(res); }