试图在C++中扩展嵌入式Python。在这种情况下,“PyMethodDef”函数中的一个方法定义可以正常工作。我认为更多的方法定义只适用于第一个。举个例子:
static char *y = new char[100];
/* Return the number of arguments of the application command line */
static PyObject* hashlib_sha256(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args,"y", &y))
return NULL;
return Py_BuildValue("i", 1);
}
static PyObject* hashlib_hexdigest(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
return Py_BuildValue("i", 0);
}
static PyMethodDef HahlibMethods[] = {
{"sha256", hashlib_sha256, METH_VARARGS, ""},
{"hexdigest", hashlib_hexdigest, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
}
static PyModuleDef HashlibModule = {
PyModuleDef_HEAD_INIT, "hashlib", NULL, -1, HahlibMethods,
NULL, NULL, NULL, NULL
}
static PyObject* PyInit_hashlib(void)
{
return PyModule_Create(&HashlibModule);
}
在Py_Initialize()
调用之前,我主要使用以下方法
PyImport_AppendInittab("hashlib", &PyInit_hashlib);
Python代码:
import hashlib
def getILoomaAddress():
sha_256 = hashlib.sha256(b'Hello World!')
result = sha_256.hexdigest()
return result
所以第一个sha256
方法可以很好地工作,但是hexdigest
不行。
这里的问题是什么,为什么不调用hashlib_hexdigest
?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐