[Python.NET] Updates to RunString

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[Python.NET] Updates to RunString

Michael Eddington
Been playing around with RunString implementations looking for what
works best.  Here is my result.  I am able to run any python script
loaded from file just fine.  Also, between calls to RunString local
variables are saved just fine.

        protected static PyDict locals = null;

        public static PyObject RunString(string code)
                PyObject module = ImportModule("__main__");
                PyDict globals = new PyDict(Runtime.PyModule_GetDict(module.Handle));

        if (locals == null)
            locals = new PyDict(Runtime.PyDict_New());

        IntPtr result = Runtime.PyRun_String(code, (IntPtr)257,
globals.Handle, locals.Handle);

        if (result == IntPtr.Zero)
            return null;

        return new PyObject(result);
Python.NET mailing list - [hidden email]