如何在 C python 扩展中使用 PyObject* args?

How do I use PyObject* args in C python extension?

我正在尝试用 C 语言做一个简单的扩展,它应该能够扩展 python 代码。 我在 https://github.com/munirhossain/py_c_extension

上找到了该代码
#include <Python.h>

// Function 1: A simple 'hello world' function
static PyObject* helloworld(PyObject* self, PyObject* args) 
{   
    printf("Hello Munir\n");
    Py_RETURN_NONE;
    return Py_None;
}

// Function 2: A C fibonacci implementation
// this is nothing special and looks exactly
// like a normal C version of fibonacci would look
int Cfib(int n)
{
    if (n < 2)
        return n;
    else
        return Cfib(n-1)+Cfib(n-2);
}
// Our Python binding to our C function
// This will take one and only one non-keyword argument
static PyObject* fib(PyObject* self, PyObject* args)
{
    // instantiate our `n` value
    int n;
    // if our `n` value 
    if(!PyArg_ParseTuple(args, "i", &n))
        return NULL;
    // return our computed fib number
    return Py_BuildValue("i", Cfib(n));
}

// Our Module's Function Definition struct
// We require this `NULL` to signal the end of our method
// definition 
static PyMethodDef myMethods[] = {
    { "helloworld", helloworld, METH_NOARGS, "Prints Hello Munir" },
    { "fib", fib, METH_VARARGS, "Computes Fibonacci" },
    { NULL, NULL, 0, NULL }
};

// Our Module Definition struct
static struct PyModuleDef myModule = {
    PyModuleDef_HEAD_INIT,
    "myModule",
    "Test Module",
    -1,
    myMethods
};

// Initializes our module using our above struct
PyMODINIT_FUNC PyInit_myModule(void)
{
    return PyModule_Create(&myModule);
}

我想像调用 helloworld func 时那样修改该代码,例如 helloworld("max") 它 returns Hello max 在 C 中,但我不知道如何使用 PyObject* args:/ 有什么办法可以做到这一点(在 C 中)吗?

您应该阅读 PyArg_ParseTuple 文档。基本上这应该有效:

static PyObject* helloworld(PyObject* self, PyObject* args) 
{   
    const char *name;

    if (!PyArg_ParseTuple(args, "s", &name)) {
        return NULL;
    }

    printf("Hello %s\n", name);
    Py_RETURN_NONE;
}

您需要将table中的方法定义更改为

{ "helloworld", helloworld, METH_VARARGS, "Prints Hello <name>" },

当然,因为它现在需要参数。描述 s 说参数元组必须恰好包含一项并且它应该是 str 类型;它被转换为 UTF-8(每个 CPython 字符串对象可以包含 UTF-8 中的字符串内容的缓存副本以供 C 使用),并且指向第一个字符的指针存储到指向的指针对象中通过可变参数列表中的相应参数(即 &name - 输出值为 const char *,并且相应的参数必须是指向此类对象的指针,即 const char **)。

如果PyArg_ParseTuple returns 为假值,则表示转换失败并设置了Python 异常。我们通过从函数返回 NULL 而不是 Py_None 来在 Python 端引发异常。

最后,

return Py_None; 

不正确 - 在返回任何此类值之前,你必须始终 增加 引用计数器 - 这就是 Py_RETURN_NONE 宏在其中所做的 - 它在功能上是等效的至

Py_INCREF(Py_None);
return Py_None;