python - 如何从Python扩展模块的C代码调用内置函数(或方法)?

标签 python c python-3.x python-itertools extension-modules

我目前想要完成的是调整Python的itertools模块函数combinations,以在使用它创建组合之前对传递的iterable进行排序对返回的组合进行排序的目标。

我是第一次开发Python扩展模块,到目前为止我唯一的经验是编写和编译一个像Python扩展模块一样的“Hello World”,但我希望我的整体编程经验能够在几次编程中得到提升语言是我能够成功应对这一挑战的坚实基础。

我知道有一个内置的 Python 函数 sorted() ,它可以对传递给 combinations 的迭代进行排序,但我不知道如何从内部调用它扩展模块的C代码。

我尝试只写 iterable = Sorted(iterable); 但即使模块编译(带有警告),已编译模块的导入也会失败,并出现 ImportError: cgitertools.cpython -36m-x86_64-linux-gnu.so: undefined symbol :已排序

我的问题是:

How to call the Pythons builtin method (using as example sorted()) from within C code of a Python extension module?

下面是我尝试过的所有详细信息以及为什么它不起作用:

combinations_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    combinationsobject *co;
    Py_ssize_t n;
    Py_ssize_t r;
    PyObject *pool = NULL;
    PyObject *iterable = NULL;
    Py_ssize_t *indices = NULL;
    Py_ssize_t i;
    static char *kwargs[] = {"iterable", "r", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations", kwargs,
                                     &iterable, &r))
        return NULL;

    // iterable.sort(); doesn't work ... cgitertoolsmodule.c:2398:13: error: request for member ‘sort’ in something not a structure or union
    // iterable.__sort__(); doesn't work either with same error
    // COMPILES, but gives ERROR on import in Python: 
    iterable = sorted(iterable);

$ python3.6 cgitertoolsmodule-setup.py build
running build
running build_ext
building 'cgitertools' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python3.6m -c cgitertoolsmodule.c -o build/temp.linux-x86_64-3.6/cgitertoolsmodule.o
cgitertoolsmodule.c: In function ‘combinations_new’:
cgitertoolsmodule.c:2400:16: warning: implicit declaration of function ‘sorted’ [-Wimplicit-function-declaration]
     iterable = sorted(iterable);
                ^
cgitertoolsmodule.c:2400:14: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     iterable = sorted(iterable);
              ^
gcc -pthread -shared build/temp.linux-x86_64-3.6/cgitertoolsmodule.o -o build/lib.linux-x86_64-3.6/cgitertools.cpython-36m-x86_64-linux-gnu.so

$ python3.6
Python 3.6.1 (default, Apr 18 2017, 23:00:41) 
[GCC 5.4.1 20160904] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from cgitertools import combinations
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cgitertools.cpython-36m-x86_64-linux-gnu.so: undefined symbol: sorted

最佳答案

您应该从内置函数中获取sorted函数,然后调用它:

PyObject *builtins = PyEval_GetBuiltins(); 
PyObject *sorted = PyDict_GetItemString(builtins , "sorted");
PyObject *sorted_list = PyEval_CallFunction(sorted, "(O)", iterable);

//... do something with the sorted_list

Py_DECREF(sorted_list);

关于python - 如何从Python扩展模块的C代码调用内置函数(或方法)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43704106/

相关文章:

Python 创建 C 线程

c - UEFI hello world编译错误

c - 浮点异常C代码

python-3.x - 如何使用Socket库使用python3连接到ipv6主机

python - 有没有更有效的方法来编写合并函数?

python - 在 Python 的二维数组中输入值

python - Django Tastypie 添加 Content-Length header

python - 拆分混合数据框的列

c - 哪个库可以在 C 中用鼠标操作图像

python - Django 麻烦 : django. setup() 抛出 "ImportError: No module named ' project_name'"