Hello !

First of all, thanks for your work on Python.NET ; I'm a new user, and I'm 
quite pleased with the abilities of this library !

I'm currently setting up a project aiming at dynamic GUI creation for python 
objects of an existing codebase. So far, I've been able to make some 
bootstrapping work, allowing me to view and edit arbitrary python objects using 
MS PropertyGrid control.

The application I'm working on already embeds a Python interpreter, and I've 
been able to also embed Python.NET and seamlessly make all this communicate 
without too much problems.

The only point I had to tweak was PythonEngine.RunString code : the original 
code calls "PyEval_GetGlobals" which invariably crashed my application. 
Moreover, I needed a version of RunString that used "Py_eval_input".

I tried to write my own version of RunString using Python.Runtime.Runtime 
methods, but as they are marked as 'internal', all I could do is patch 
python.NET code.

For now, I rewrote another version of RunString, which looks similar to the 
RunString code that is often recommanded ::
Index: pythonengine.cs
===================================================================
--- pythonengine.cs     (révision 90)
+++ pythonengine.cs     (copie de travail)
@@ -314,23 +314,24 @@
         /// </remarks>

         public static PyObject RunString(string code) {
-            IntPtr globals = Runtime.PyEval_GetGlobals();
-            IntPtr locals = Runtime.PyDict_New();
+            return RunString(code, Runtime.Py_file_input);
+        }

-            IntPtr builtins = Runtime.PyEval_GetBuiltins();
-            Runtime.PyDict_SetItemString(locals, "__builtins__", builtins);
+        public static PyObject RunString(string code, IntPtr flags)
+        {
+            IntPtr main_module = Runtime.PyImport_AddModule("__main__");
+            IntPtr main_namespace = Runtime.PyModule_GetDict(main_module);

-            IntPtr flag = (IntPtr)257; /* Py_file_input */
-            IntPtr result = Runtime.PyRun_String(code, flag, globals, locals);
-            Runtime.Decref(locals);
-            if (result == IntPtr.Zero) {
+            IntPtr result = Runtime.PyRun_String(code, flags, main_namespace, 
main_namespace);
+
+            if (result == IntPtr.Zero)
+            {
                 return null;
             }
             return new PyObject(result);
         }


-
     }

It seems that there is often people having problems with a default unique 
RunString implementation.

As I'd prefer not to have to maintain my own set of patches to Python.NET, I 
was wondering if it could be possible to remove the 'internal' modifiers on 
Python.Runtime.Runtime methods so that everyone out there could use them, at 
their own risk, to create custom python interaction functionalities ?

Any toughts ?

Thanks in advance, cheers,

Nicolas.
_________________________________________________
Python.NET mailing list - PythonDotNet@python.org
http://mail.python.org/mailman/listinfo/pythondotnet

Reply via email to