Python C扩展问题:“TypeError:function接受0个参数(给定1个)”

时间:2015-07-08 19:21:16

标签: python c typeerror

我正在为Python编写C扩展,我遇到了在Python中运行扩展的问题。我有下面的代码作为C扩展的模块,它说该函数没有参数,但它应该认识到这一点。我不确定我是否遗漏了什么。

C代码:

static PyObject*
clusterext(PyObject* self, PyObject* args)
{

    PyObject *fileList;

    if(!PyArg_ParseTuple(args, "O", &fileList))
    {
        return NULL;
    }

    long size = PyList_Size(fileList);
    int count = 0;

    char *arraynew[size];

    PyObject *iter = PyObject_GetIter(fileList);
    if(!iter)
    {
        return NULL;
    }

    while(1)
    {
        PyObject *next = PyIter_Next(iter);
        if(!next)
        {
            break;
        }

        char *fileName = PyString_AsString(next);
        arraynew[count] = fileName;
        count++;
    }


    char *inFilename1; //File to be looked at
    char *inFilename2;
    double rmsdTemp;
    int rmsdArrayLength = ((size * (size - 1)) / 2);
    struct filePair rmsdArray[rmsdArrayLength]; 
    int rcount = 0;
    int count1;
    int count2;
    double threshold = 10;

    for (int i = 0; i < size; i++)
    {
        int w = i + 1;
        for (int z = w; z < size; z++)
        {
            inFilename1 = arraynew[i];
            inFilename2 = arraynew[z];

            count1 = filelength(inFilename1);
            count2 = filelength(inFilename2);

            double coords[count1][3];
            double coords2[count2][3];
            extractCoords(inFilename1, count1, coords);
            extractCoords(inFilename2, count2, coords2);


            rmsdTemp = rmsd(count1, count2, coords, coords2);
            struct filePair tempPair = newPair(inFilename1, inFilename2, rmsdTemp);
            rmsdArray[rcount] = tempPair;

            rcount++;
        }


    }

    int fileNeighbours[size][size-1];
    int oncount = 0;
    int prevFileNum = 0;
    int index = 0;

    for(int a =0; a < rmsdArrayLength; a++)
    {
        struct filePair testPair = rmsdArray[a];
        if(testPair.rmsd < threshold)
        {
            int tempFileNum1 = getFileNumber(testPair.filename1);
            int tempFileNum2 = getFileNumber(testPair.filename2);
            if(prevFileNum != tempFileNum1)
            {
                oncount = 0;
            }

            index = tempFileNum1 -1;
            fileNeighbours[index][oncount] = tempFileNum2;
            oncount++;
            prevFileNum = tempFileNum1;

        }
    }

    int clustersArray[size][size];
    int counter2 = 0;
    int counter3 = 0;

    for(int u = 0; u < index; u++)
    {
        oncount = u + 1;
        counter2 = 0;
        counter3 = 1;

        clustersArray[u][0] = oncount;

        while (fileNeighbours[u][counter2] > 0)
        {
            clustersArray[u][counter3] = fileNeighbours[u][counter2];
            counter2++;
            oncount++;
            counter3++;

            if(counter2 >= size || counter3 >= size)
            {
                break;
            }
        }
    }

    return Py_BuildValue("%d \n", prevFileNum);
}


static PyMethodDef ClusterMethods[] = 
{
    {"clusterext", clusterext, METH_VARARGS, "clustering"},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initclusterext(void)
{
    (void) Py_InitModule("clusterext", ClusterMethods);
}

Python安装代码:

#!/usr/bin/env python

from distutils.core import setup, Extension

module1 = Extension('clusterext', sources = ['clusterextmodule.c'])

setup(name = 'PackageName',
      version = '1.0',
      description = 'This is a demo package',
      ext_modules = [module1])

运行扩展代码的Python文件:

#! /usr/bin/python

import clusterext
import os, os.path


rootdir = "/Users/simranrai/Documents/SummerWork/Clustering"

targets = [f for f in os.listdir(rootdir) if f.endswith('.pdb')]

clusterext.clusterext(targets)

编辑:这是完整的追溯。 clustertest.py是上面的最后一段代码

Traceback (most recent call last):
  File "clustertest.py", line 11, in <module>
    clusterext.clusterext(targets)
TypeError: function takes exactly 0 arguments (1 given)

我不确定我在哪里出错,以及为什么我会收到错误。任何帮助将不胜感激!!

0 个答案:

没有答案