I'm writing a C extension for Python and I'm having an issue with running the extension in Python. I have the code below as the module for the C extension, and it is saying the function has no arguments, but it should recognize this. I am not sure if I am missing anything.
C code:
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 Setup code:
#!/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 file to run extension code:
#! /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)
EDIT: Here is the full traceback. clustertest.py is the last block of code above
Traceback (most recent call last):
File "clustertest.py", line 11, in <module>
clusterext.clusterext(targets)
TypeError: function takes exactly 0 arguments (1 given)
I'm not really sure where I'm going wrong here and why I'm getting the error. Any help would be appreciated!!