我有PyHVL的问题。我在我的c代码中使用PyImport_Import函数。代码构建没有错误,但函数总是返回NULL。我在这个门户网站上看到了同样的问题和很多答案,但几乎没有任何解决方案适合我。有人知道吗?
提前感谢..
这是函数定义。
int do_py_initialization(pyhvl_inst_p pp)
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
char *pyhvl_prerun;
if (pyhvl_is_initialized == 0) {
if (debugging) {
vpi_printf("+++ PyHVL: Just before Py_Initialize\n");
}
Py_Initialize();
/* See if there is a pre-run command */
/* not enabled by default, getenv() messes up visual c link step */
#ifdef ALLOW_PRERUN_HOOK
pyhvl_prerun = getenv("PyHVL_PRERUN_HOOK");
if (pyhvl_prerun != NULL) {
if (debugging) {
vpi_printf("+++ PyHVL: Running prerun hook: %s\n", pyhvl_prerun);
}
PyRun_SimpleString(pyhvl_prerun);
}
#endif
/* Add our module*/
initpyhvl_vpi();
if (debugging) {
vpi_printf("+++ PyHVL: Just after Py_Initialize\n");
}
pyhvl_is_initialized = 1;
}
pName = PyString_FromString(pp->module);
/* Error checking of pName omitted */
pModule = PyImport_Import(pName);
if (pModule != NULL) {
if (debugging) {
vpi_printf("+++ PyHVL: successfully loaded module: %s\n", pp->module);
}
pDict = PyModule_GetDict(pModule);
/* pDict is a borrowed reference */
pFunc = PyDict_GetItemString(pDict, pp->c_class);
/* pFunc is borrowed reference */
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(6);
pValue = PyCObject_FromVoidPtrAndDesc((void*) pp->systf_h, cob_vpiHandle_desc, NULL);
if (!pValue) {
pyhvl_msgerr("Cannot create PyCObject from systf_h vpiHandle");
}
PyTuple_SetItem(pArgs, 0, pValue);
pValue = PyCObject_FromVoidPtrAndDesc((void*) pyhvl_callback, cob_callbackfn_desc, NULL);
if (!pValue) {
pyhvl_msgerr("Cannot create PyCObject for callback function.");
}
PyTuple_SetItem(pArgs, 1, pValue);
pValue = PyString_FromString(pp->name);
if (!pValue) {
pyhvl_msgerr("cannot convert arg 1");
}
PyTuple_SetItem(pArgs, 2, pValue);
pValue = PyString_FromString(pp->module);
if (!pValue) {
pyhvl_msgerr("cannot convert arg 2");
}
PyTuple_SetItem(pArgs, 3, pValue);
pValue = PyString_FromString(pp->c_class);
if (!pValue) {
pyhvl_msgerr("cannot convert arg 3");
}
PyTuple_SetItem(pArgs, 4, pValue);
{
PyObject *pVobjs;
int i;
pVobjs = PyTuple_New(pp->argc);
for (i = 0; i < pp->argc; i++) {
pValue = PyCObject_FromVoidPtrAndDesc((void*) (pp->argi[i]), cob_vpiHandle_desc, NULL);
if (!pValue) {
pyhvl_msgerr("Problem converting vpiHandle to PyCObject");
}
PyTuple_SetItem(pVobjs, i, pValue);
}
PyTuple_SetItem(pArgs, 5, pVobjs);
}
/* Call our class constructor */
pValue = PyObject_CallObject(pFunc, pArgs);
if (pValue != NULL) {
if (debugging) {
/* TOM: finally figured it out. Calling PyString_AsString for debugging
* output causes memory corruption problems. Need to understand sometime.
*/
#if 0
vpi_printf("+++ PyHVL: Result of Class Constructor pycall: %0x, %s\n", (int)pValue,
PyString_AsString(pValue));
#else
vpi_printf("+++ PyHVL: Result of Class Constructor pycall: %0lx\n", (unsigned long)pValue);
#endif
}
/* TOM: I don't want to decref this - i want to hang on to it!!! */
/* Py_DECREF(pValue); */
pp->pyinst = pValue;
}
else {
PyErr_Print();
pyhvl_msgerr("Pycall failed :-( (init)\n");
}
Py_DECREF(pArgs);
/* pDict and pFunc are borrowed and must not be decref'd */
}
else {
PyErr_Print();
pyhvl_msgerr("Cannot find class \"%s\"\n", pp->c_class);
}
}
else {
PyErr_Print();
pyhvl_msgerr("Failed to load python module \"%s\"\n", pp->module);
}
Py_DECREF(pName);
return 0;
}