python - 在 C 中嵌入 Python - 段错误

标签 python c segmentation-fault

来自阅读另一个post ,我正在尝试将一些 Python 代码嵌入到 C:

ma​​in.c

#include <Python.h>

int callModuleFunc(int array[], size_t size) {
    PyObject *mymodule = PyImport_ImportModule("py_function");
    PyObject *myfunc = PyObject_GetAttrString(mymodule, "printlist");
    PyObject *mylist = PyList_New(size);
    for (size_t i = 0; i != size; ++i) {
        PyList_SET_ITEM(mylist, i, PyInt_FromLong(array[i]));
    }
    PyObject *arglist = Py_BuildValue("(o)", mylist);
    PyObject *result = PyObject_CallObject(myfunc, arglist);
    int retval = (int)PyInt_AsLong(result);
    Py_DECREF(result);
    Py_DECREF(arglist);
    Py_DECREF(mylist);
    Py_DECREF(myfunc);
    Py_DECREF(mymodule);
    return retval;
}

int main(int argc, char *argv[])
{
    int a[] = {1,2,3,4};
    callModuleFunc(a, 4);
    return 0;
}

py_function.py

'''py_function.py - Python source designed to '''
'''demonstrate the use of python embedding'''

def printlist(mylist):
    print mylist

然后我编译了:

gcc main.c -I/usr/include/python2.7 -lpython2.7

但后来我运行了该应用程序,它给了我一个段错误:

/a.out
[1]    18890 segmentation fault  ./a.out

有什么我想念的吗?

最佳答案

你的代码有几个问题:

  1. Py_Initialize() 没有被调用。
  2. PyImport_ImportModule() 找不到您的 Python 文件,因为在嵌入式 Python 中您开始时没有一个初始模块,搜索可以相对于该模块进行。解决方法是在 sys.path 中显式包含当前目录。
  3. Py_BuildValue() 中的
  4. "(O)" 应该使用大写的 'O'
  5. printlist 函数应该返回一个值(因为这是 C 代码所期望的)。

这应该有效:

ma​​in.c

#include <Python.h>

void initPython()
{
    Py_Initialize();
    PyObject *sysmodule = PyImport_ImportModule("sys");
    PyObject *syspath = PyObject_GetAttrString(sysmodule, "path");
    PyList_Append(syspath, PyString_FromString("."));
    Py_DECREF(syspath);
    Py_DECREF(sysmodule);
}

int callModuleFunc(int array[], size_t size) {
    PyObject *mymodule = PyImport_ImportModule("py_function");
    assert(mymodule != NULL);
    PyObject *myfunc = PyObject_GetAttrString(mymodule, "printlist");
    assert(myfunc != NULL);
    PyObject *mylist = PyList_New(size);
    for (size_t i = 0; i != size; ++i) {
        PyList_SET_ITEM(mylist, i, PyInt_FromLong(array[i]));
    }
    PyObject *arglist = Py_BuildValue("(O)", mylist);
    assert(arglist != NULL);
    PyObject *result = PyObject_CallObject(myfunc, arglist);
    assert(result != NULL);
    int retval = (int)PyInt_AsLong(result);
    Py_DECREF(result);
    Py_DECREF(arglist);
    Py_DECREF(mylist);
    Py_DECREF(myfunc);
    Py_DECREF(mymodule);
    return retval;
}

int main(int argc, char *argv[])
{
    initPython();

    int a[] = {1,2,3,4,5,6,7};
    callModuleFunc(a, 4);
    callModuleFunc(a+2, 5);

    Py_Finalize();
    return 0;
}

py_function.py

'''py_function.py - Python source designed to '''
'''demonstrate the use of python embedding'''

def printlist(mylist):
    print mylist
    return 0

关于python - 在 C 中嵌入 Python - 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37687770/

相关文章:

python - 动态函数文档字符串

python - Hadoop 流式传输多个 python 文件

c - 初始化结构导致段错误

python - Homebrew 的 python gtk 导入导致段错误

c - 仅在 pthread 初始化时发生段错误

python - 从 Python 子进程调用 Perl 脚本中的 GetOptions

python - Cython:存储指针时 Python/C++ 之间的内存所有权问题

c - 为什么每当我在表达式中输入括号时,以下代码都会显示段错误?

c++ - 父子进程的c变量值

c - 为什么 strcat 函数会出现段错误?