embedding python in C, working but with exception at the end
jepler at unpythonic.net
jepler at unpythonic.net
Sat Sep 3 10:55:20 EDT 2005
I see a couple of problems. First, because I'm using Unix, where filenames are
case-sensitive, I had to '#include "Python.h"' instead of '#include
"python.h"'.
Next, it looks like the behavior that '.' is placed on sys.path isn't done
automatically when embedding. So I had to set the environment variable
"PYTHONPATH=." since Test.py was in the current directory.
Before I did this, I got this output:
Exception exceptions.ImportError: 'No module named Test' in 'garbage
collection' ignored
Fatal Python error: unexpected exception during garbage collection
Aborted
which was a clue about the problem you were running into. This ImportError was
being caused back at PyImport_Import but was being transmuted into a fatal
error down at Py_Finalize(). By adding 'else { PyErr_Print(); }' to the end of
'if(mod)', I got the error message printed and cleared. Now, garbage
collection which is kicked off by Py_Finalize() doesn't find the existing error
condition and treat it as a fatal error.
In your code, 'rslt' will be a Python Integer, not a Python Float, so
PyFloat_AsDouble will fail. You could either write something like
if (rslt)
{
if(PyFloat_Check(rslt)) {
answer = PyFloat_AsDouble(rslt);
} else {
printf("not a float\n");
answer = 1.0;
}
Py_XDECREF(rslt);
}
instead, or use PyErr_Check() + PyErr_Print() or PyErr_Clear().
If you want to accept integer or float returns, then maybe you want
if(PyFloat_Check(rslt)) { answer = PyFloat_AsDouble(rslt); }
else if(PyInt_Check(rslt)) { answer = PyInt_AsLong(rslt); }
else probably not a numeric type
Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050903/5158f6a2/attachment.sig>
More information about the Python-list
mailing list