Ok then. I've used the C API a lot before, and it's basically coding in C with a little gymnastics to pass values between C and python. I'm doing some project euler now to brush up on those skills now while I'm unemployed.
So this function takes int n and returns a list of all the factors of n (it's not complete yet, just the relevant code that's spitting out errors is here). isPrime returns True or False. I'm trying to check if n is prime before I try to factor it.
Code:
static PyObject *
euler_Factors(PyObject *self, PyObject *args){
long n, i;
int test;
PyObject *res;
if(!PyArg_Parse(args,"(l)",&n))
return NULL;
res = PyEval_CallFunction(euler_isPrime, "i", n);
if(!(test = PyObject_IsTrue(res)))
return NULL;
printf("%d\n",test);
return res;
}
Code:
static PyObject *
euler_isPrime(PyObject *self, PyObject *args){
unsigned long long n, i, r;
if(!PyArg_Parse(args,"(l)",&n))
return NULL;
r = (unsigned long long) (sqrt((double) (n))+1.0);
if(n<2){
Py_RETURN_FALSE;
}
else{
for(i=3;i<r;i++){
if(n%i == 0)
Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
}
}
The problem is in
Code:
res = PyEval_CallFunction(euler_isPrime, "i", n);
I don't know what it's returning and can't figure out how to. It's obviously not returning Py_True or Py_False. When I compile the lib I get this warning message
Code:
euler.c:71: warning: passing argument 1 of ‘PyEval_CallFunction’ from incompatible pointer type
/usr/include/python2.6/ceval.h:21: note: expected ‘struct PyObject *’ but argument is of type ‘struct PyObject * (*)(struct PyObject *, struct PyObject *)’
It's obviously the wrong type, but I don't get how you're supposed to call a function without arguments.