扩展TypeError:函数正好接受0个参数(给定1个)

时间:2019-05-31 03:06:20

标签: python python-3.6 extension-methods

当我在python中扩展C语言函数<!DOCTYPE html> <html> <head> <title>Checkout page</title> </head> <body> <h1>Checkout page</h1> <form action="Confirmationpage.html" method="POST"> <input type="text" name="fName" placeholder="First Name"><br> <input type="text" name="lName" placeholder="Last Name"><br> <input type="email" name="email" placeholder="Email"><br> <input type="number" name="phoneNumber" placeholder="Phone Number"> <br> <input type="submit" value="Confirm" name="submit"> </form> </body> </html> 方法时,发生了异常。

fibonacci

setup.py

#include <Python.h>

long long fibonacci (unsigned int n){
    if (n < 2) {
        return 1;
    } else {
        return fibonacci(n -2) + fibonacci(n - 1);
    }

}

static PyObject* fibonacci_py(PyObject* self, PyObject* args){
    PyObject *result = NULL;
    long n;
    if(PyArg_ParseTuple(args, "1", &n)){
        if(n < 0){
            n = -n;
        }
        result = Py_BuildValue("L", fibonacci((unsigned int)n));
    }
    return result;
}

static char fibonacci_docs[] = "fibonacci(n): Return nth Fibonacci sequence number "
                                "computed recursively\n";

static PyMethodDef fibonacci_module_methods[] = {
    {"fibonacci", (PyCFunction)fibonacci_py,
    METH_VARARGS, fibonacci_docs},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef fibonacci_module_definition = {
    PyModuleDef_HEAD_INIT,
    "fibonacci",
    "Extension module that provides fibonacci sequence function",
    -1,
    fibonacci_module_methods
};

PyMODINIT_FUNC PyInit_fibonacci(void) {
    Py_Initialize();
    return PyModule_Create(&fibonacci_module_definition);

然后我运行pip命令:from setuptools import setup, Extension setup( name='fibonacci', ext_modules=[ Extension('fibonacci', ['fibonacci.c']), ] )

输出如下:

pip3 install -e

当我运行此代码时:

  Obtaining file:///home/wzhang/code/learn
  Installing collected packages: fibonacci
    Running setup.py develop for fibonacci
  Successfully installed fibonacci

发生错误:-

from fibonacci import fibonacci
fibonacci(3)

0 个答案:

没有答案