[Python.NET] RunString does not work

J. Merrill jvm_cop at spamcop.net
Sun Mar 4 18:13:37 CET 2007

Without any specific reference to Python.Net or C#, my experience has been that cross-process or cross-language calling is made easier by defining a routine on the receiving side that accepts parameters of a string to be run and an indication of whether a return value is desired.  The called routine sets up its own try/except block, and runs the string.  The return value is a tuple of two values:  a boolean indicating whether an error occurred and data.  If the boolean indicates failure, the data is the error message as a string or, if you want, a more complex value with error message and execution state information.  If the boolean indicates success, the data is the result of running the string if requested or an empty value.  (Avoiding sending a potentially large value back as a result is an optimization in the case where the caller doesn't need it.)

When you do this, you can use all the power of the called environment to do error-handling in a routine that's written once and re-used.

Good luck.

At 04:37 AM 3/4/2007, Seshagiri Cherukuri wrote
>hi All, 
>I have embedded pythonnet into my C# application and this works great as far as creating .net objects from python, executing methods etc. 
>Here I am using the RunSimpleString (the PyRun_SimpleString) wrapper to send commands from the C# application to the pythonnet. 
>However I am facing the following problems: 
>1. After RunSimpleString, PyErr_Occurred or PyErr_Fetch are not able to detect any exceptions even if they did occur
>- for example, consider the code - curErr = PythonEngine.RunSimpleString("10/0\n" ); 
>- this code does return a -1 value indicating error.
>- however after this none of PyErr_Occurred or PyErr_Fetch do not detect this error
>- from python documentation it looks like there is no way to obtain the exception information
>2. using RunString
>- because of above problem I want to send a multi-line code including a try-except: block
>- I have tried using RunString with all the three modes - Py_eval_input, Py_single_input and Py_file_input
>- The RunString works only if the code does not have any .net objects or does not include any .net exceptions
>- If the code sent to RunString includes a .net object e.g. obj, then RunString always returns in NameError, obj is not defined
>3. Strangely, if I run the console application, it is able to work with .net exceptions - both .net and user defined exceptions
>4. Is this because of the new dictionary created while in RunString? 
>Sesh Cherukuri

J. Merrill / Analytical Software Corp
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythondotnet/attachments/20070304/857c4ba7/attachment.htm 

More information about the PythonDotNet mailing list