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

Attachment: pgpsNcJ3ylwlJ.pgp
Description: PGP signature

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to