2 meng zy meng_zy 于 2016.02.28 19:25 提问

distutils Extension TypeError

最近在学习在Python中扩展C,在 创建setup.py文件的过程中出现了问题

在终端运行“python setup.py build ”命令是出现错误,错误提示为:
[m@localhost CPython]$ python setup.py build
Traceback (most recent call last):
File "setup.py", line 4, in
extest_module = Extension(name = 'Extest', sourses = ['Extest.c'], include_dirs = ['/usr/local/include'])
TypeError: init() takes at least 3 arguments (3 given)

我的 setup.py文件内容:

 #!/usr/bin/env python
from distutils.core import setup, Extension
extest_module = Extension(name = 'Extest', sourses = ['Extest.c'], include_dirs = ['/usr/local/include'])
setup(name = 'Extest', ext_modules = [extest_module])

我的demo主文件内容

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//#include"Python.h"

static PyObject * Extest_fac(PyObject *self, PyObject *args);
static PyObject * Extest_doppel(PyObject *self, PyObject *args);
int fac(int n);
char *reverse(char *s);

static PyMethodDef ExtesMethods[] = { {"fac", Extest_fac, METH_VARARGS}, 
                                      {"doppel", Extest_doppel, METH_VARAGRS}, 
                                      {NULL, NULL} 
                                               };
void initExtest()
{
    Py_InitModule("Extest", ExtestMethods);
}

int main()
{
    int n;
    char s[] = "abcdef"; 
    //scanf("%d,&n");
    //printf("%d! == %d\n",n,fac(n));
    printf("4! == %d\n",fac(4));
    printf("reversing 'abcdef', we get '%s'\n", reverse(s));
    return 0;
}



static PyObject * Extest_fac(PyObject *self, PyObject *args){
    int res;          //parse result
    int  num;        //arg for fac()
    PyObject* retval;// return value

    res = PyArg_ParseTuple(args, "i", &num);
    if(!res) return NULL; // TypeError
    res = fac(num);
    retval = (PyObject*)Py_BuildValue("i", res);
    return retval;
}

static PyObject * Extest_doppel(PyObject *self, PyObject *args){
    char *orig_str, dupe_str;
    PyObeject *retval;
    if ( !PyArg_ParseTuple(args, "s", &orig_str)) return NULL;
    retval = (PyObject*) Py_BuildValue("ss", orig_str, dupe_str = reverse(strdup(orig_str)) );
    free(dupe_str);
    return retval;
}

int fac(int n)
{
    if (n < 2) return 1;
    return n * fac(n-1);
}

char *reverse(char *s)
{
    register char t, 
            *p = s , /* s 的头指针*/
            *q = (s + (strlen(s) - 1) ); //s的尾指针
   while (p < q)
    {
       t = *p;
       *p++ = *q;
       *q-- = t;
    }
    return  s;
}

1个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2016.02.29 10:48
已采纳

没发现什么大问题,难道是路径错了么?楼主再看看路径和文件都放对这么

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!