【发布时间】:2014-04-21 19:27:03
【问题描述】:
我正在关注this 使用 Python 封装 C/C++ 的教程。我已经逐字复制了示例代码,但仍会在下面列出。
你好.c
#include <stdio.h>
#include <Python.h>
// Original C Function
char * hello(char * what)
{
printf("Hello %s!\n", what);
return what;
}
// 1) Wrapper Function that returns Python stuff
static PyObject * hello_wrapper(PyObject * self, PyObject * args)
{
char * input;
char * result;
PyObject * ret;
// parse arguments
if (!PyArg_ParseTuple(args, "s", &input)) {
return NULL;
}
// run the actual function
result = hello(input);
// build the resulting string into a Python object.
ret = PyString_FromString(result);
free(result);
return ret;
}
脚本hello.c 定义了一个简单的“hello”函数,以及一个返回 Python 对象的包装器,并且(假设地)释放 c char * 指针。 这是代码因运行时错误而失败的地方: Error in '/usr/bin/python': free(): invalid pointer: 0x00000000011fbd44。虽然我认为错误应该限制在这个范围内,但让我们检查一下包装器的其余部分以防万一......
hello.c 包含在模块的定义中,它允许在 Python 中调用其方法。模块定义如下:
hellomodule.c
#include "hello.c"
#include <Python.h>
// 2) Python module
static PyMethodDef HelloMethods[] =
{
{ "hello", hello_wrapper, METH_VARARGS, "Say hello" },
{ NULL, NULL, 0, NULL }
};
// 3) Module init function
DL_EXPORT(void) inithello(void)
{
Py_InitModule("hello", HelloMethods);
}
最后,实现了一个 Python 脚本来构建模块:
setup.py
#!/usr/bin/python
from distutils.core import setup, Extension
# the c++ extension module
extension_mod = Extension("hello", ["hellomodule.c"]) #, "hello.c"])
setup(name = "hello", ext_modules=[extension_mod])
一旦setup.py 运行,该模块就可以被导入到任何Python 脚本中,并且它的成员函数应该是可以访问的,并且已经被证明是可以访问的,但无效指针错误除外。我花了很多时间在这方面无济于事。请帮忙。
【问题讨论】:
标签: python c malloc wrapper free