Getting the error in embedded python

G. David Kuhlman dkuhlman at netcom.com
Thu Mar 9 17:58:35 EST 2000


Here is the relevant info from the C API doc:

int PyRun_SimpleString (char *command) 
     Executes the Python source code from command in the __main__
     module. If __main__ does not already exist, it is created.
     Returns 0 on success or -1 if an exception was raised. If
     there was an error, there is no way to get the exception
     information. 



Here is what I do, and it seems to work.  However, according to
Mark's message and the doc on PyRun_SimpleString, it seems that my
call to PyErr_Occurred() is futile.

------------------------------------

result = PyRun_SimpleString(const_cast<char *>(scriptStr));
PyObject * exception = PyErr_Occurred();
if ((result != 0) || (exception != NULL))
{
	if (exception != NULL)
	{
		PyErr_Print();
	}
	// Format an additional error message.
	sprintf(buf, "import sys; sys.stderr.write(\"Error in ...\\n\")", ...);
	result = PyRun_SimpleString(buf);
}

------------------------------------

Boris Ottlewski (Boris.Ottlewski at NOSPAMsystemsoft.de) wrote:
> Hi,
> 
> I just run a simple script with a syntax error:
> 
>   int res = PyRun_SimpleString("import this_module_does_not exist");
>   if (res!=0)
>   {
>        if (PyErr_Occurred())
>       {
>          //never happens
>       }
>    }
> 
> I never get an PyErr_Occured. Also when I do PyErr_Fetch its empty. (res
> is -1 if I got an error).
> 
> Why is that so? How do I get the error message?
> 
> regards,
> 
> Boris Ottlewski
> 
> 



More information about the Python-list mailing list