import numpy as np
import tensorflow as tf
class PyInterface(object):
def __init__(self):
self.X = None
self.Y = None
def generate_np_uint8(self):
np_array = np.random.randint(low=0, high=255, size=(2,2,2),dtype=np.uint8);
print("Python, uint8:", np_array)
return np_array
def generate_np_float32(self):
np_array = np.random.rand(2,2,2).astype(np.float32)
print("Python, float:", np_array)
return np_array
if __name__ == '__main__':
py = PyInterface();
py.generate_np_uint8();
py.generate_np_float32();
这是我的扩展代码Python
的C++代码 ^{pr2}$这是我的主.cpp
static int numargs=0;
static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
if(!PyArg_ParseTuple(args, ":numargs"))
return NULL;
return PyLong_FromLong(numargs);
}
static PyMethodDef EmbMethods[] = {
{"numargs", emb_numargs, METH_VARARGS,
"Return the number of arguments received by the process."},
{NULL, NULL, 0, NULL}
};
static PyModuleDef EmbModule = {
PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
NULL, NULL, NULL, NULL
};
static PyObject*
PyInit_emb(void)
{
return PyModule_Create(&EmbModule);
}
int main(int argc, char *argv[]) {
if (argc < 1) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
numargs = argc;
PyImport_AppendInittab("emb", &PyInit_emb);
Py_Initialize(); // initialize python interpreter
PyRun_SimpleString("import sys");
PyRun_SimpleString("if not hasattr(sys, 'argv'):\n sys.argv=['']");
PyRun_SimpleString("sys.path.insert(0, \"./\")");
PyRun_SimpleString("sys.path.insert(0, \"./venv/Lib\")");
PyRun_SimpleString("sys.path.insert(0, \"./venv/Lib/site-packages\")");
// instance cpp interface object
CppPythonHandler * pInter = new CppPythonHandler("main", "PyInterface");
// unsigned char * np_uint8 = pInter->get_uint8();
float * np_float = pInter->get_float();
std::cout<<"End test"<<std::endl;
// delete []pInter;
if (Py_FinalizeEx() < 0) {
std::cout << "Fails to release" << std::endl;
return 120;
}
system("pause");
return 0;
}
从调用堆栈中我知道它会在PyObject\u Alloc中损坏
但我还是不知道到底是什么问题。有人能告诉我吗
编辑1: 这是我的构造师
CppPythonHandler::CppPythonHandler(const char* pModuleName, const char* pClassName) {
interfaceModule = NULL;
interfaceClass = NULL;
interface = NULL;
interfaceModule = PyImport_ImportModule(pModuleName);
if (interfaceModule == NULL) {
PyErr_Print();
fprintf(stderr,"Fails to import the module.\n");
Py_DECREF(interfaceModule);
}
else{
// import interface class
interfaceClass = PyObject_GetAttrString(interfaceModule, pClassName);
if (interfaceClass && PyCallable_Check(interfaceClass)) {
// NULL represents no args
interface = PyObject_CallObject(interfaceClass, NULL);
Py_DECREF(interfaceClass);
Py_DECREF(interfaceModule);
if(interface == NULL){
fprintf(stderr,"Fails to instance interface.\n");
Py_DECREF(interface);
}
std::cout<<"Initailization done"<<std::endl;
}
else{
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr,"Fails to import the class.\n");
Py_DECREF(interfaceClass);
Py_DECREF(interfaceModule);
}
}
}
interfaceModule、interfaceClass和interface是私有PyObject*
我也遇到了同样的错误消息,几乎是偶然的。在
小结: 这似乎是由PyObject*的ref计数不正确引起的。 出错时使用的pyobject不一定是ref计数不正确的对象! 阅读https://docs.python.org/3/c-api/intro.html#objects-types-and-reference-counts可能会对你有所帮助。在
在我的例子中,我的C++正在从Python函数中读取pyObjts列表中的项。但是,当从列表中读取这些项时,它们的refcount没有增加,因为我调用的是
PyList_GetItem()
,而不是PySequence_GetItem()
。这是一个很难调试的问题,因为错误是从运行PyObject_CallFunction()
的完全无关的代码段抛出的。在希望这有帮助!在
相关问题 更多 >
编程相关推荐