[Python.NET] Adding PyRun_String to Runtime

Michael Eddington meddington at gmail.com
Sun Aug 14 08:35:53 CEST 2005


I would like to run Python strings and get an object return from .NET.
 To this end I added PyRun_String to the Runtime object and another
method to PythonEngine ala the existing SimpleString implementation.

Everything compiles, but  Runtime.PyRun_String always returns a NULL,
and it does not look like the code is being run.

Is there any trick to adding new methods to the Runtime?

// From Runtime.cs

	[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
		   ExactSpelling=true, CharSet=CharSet.Ansi)]
        internal unsafe static extern IntPtr
        PyRun_String(string code, int start, IntPtr globals, IntPtr locals);
  

// From Python.cs

		public static PyObject RunString(string code)
		{
			IntPtr globals = Runtime.PyEval_GetGlobals();
			IntPtr locals = Runtime.PyEval_GetLocals();
			IntPtr ret;

			ret = Runtime.PyRun_String(code, 257,globals, locals);
			
			Runtime.Decref(globals);
			Runtime.Decref(locals);

			if (ret == IntPtr.Zero) 
			{
				throw new PythonException();
			}

			return new PyObject(ret);
		}


More information about the PythonDotNet mailing list