Python calling C with callback to Python - crash!
Will G.
wmistar at yahoo.com
Sat Mar 16 23:55:43 EST 2002
I know I'm rather demented to write something like this:
A.py calls B.dll using dynwin (windll)
B.dll has a callback to C.py and potentially calling it repeatedly.
When I run A.py, the callback is successfully called until it's last
invocation when "bam!!" python.exe crashes spectacularly.
I know the problem is on Python's side because when I use a C program
to invoke the B.dll, the callback to C.py is called and executed
flawlessly.
Appreciate any help!
Will G.
PS:
Here's the callback code, it's adapted (almost verbatim) from the
python on embedding doc:
Py_Initialize();
pName = PyString_FromString(HCFAMGR_MODULE);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
if (pModule != NULL)
{
pDict = PyModule_GetDict(pModule);
/* pDict is a borrowed reference */
pFunc = PyDict_GetItemString(pDict, PYTHON_FUNCTION);
/* pFun: Borrowed reference */
if (pFunc && PyCallable_Check(pFunc))
{
// WG - Specify the number of parameter we're going to
pass to this
// function
pArgs = PyTuple_New(4);
// WG - Set the main claim as first argument (LPTSTR)
pValue = PyString_FromString(charptrToHCFA);
if (!pValue)
fprintf(stderr, "Can't pass claim\n");
PyTuple_SetItem(pArgs, 0, pValue);
... (finish assigning the parameters)
// WG - Call the Python function
pValue = PyObject_CallObject(pFunc, pArgs);
if (pValue != NULL)
{
result = PyInt_AsLong(pValue);
Py_DECREF(pValue);
}
else
{
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
Py_DECREF(pArgs);
/* pDict and pFunc are borrowed and must not be
Py_DECREF-ed */
}
else
{
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n",
PYTHON_FUNCTION);
}
Py_DECREF(pModule);
}
else
{
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", HCFAMGR_MODULE);
return 1;
}
Py_DECREF(pName);
Py_Finalize();
More information about the Python-list
mailing list