What's wrong?

K

kishkin

Hello everyone!

I wanted to write python script with defined function. From that script
I wanted to call C module, which in it's turn call defined python
function. Sounds simple. But I've got some troubles in realization. I'm
beginner at python so it's normal, I think. :)

Here's the code. I'll apreciate if anyone can answer me what's wrong
here.

-------------------------------------------------------------------
power.c:

#include "Python.h"

static PyObject *
power(PyObject *self, PyObject *args)
{
PyObject *result, *temp;

int num;

if (PyArg_ParseTuple(args, "iO", &num, &temp))
{
if (!PyCallable_Chack(temp)) //here's error, but I don't know
why...
{
return NULL;
}
}

result = Py_BuildValue("i", num*num);

PyEval_CallObject(temp, result);
Py_DECREF(result);

Py_INCREF(Py_None);
retrun Py_None;
}

static struct PyMethodDef power_methods[] = {
{"power", power, METH_VARARGS, "powering number"},
{NULL, NULL, 0, NULL}
};

void initpower()
{
(void) Py_InitModule("power", power_methods);
}
-------------------------------------------------------------------

-------------------------------------------------------------------
power.py:

import power

def sst(some):
print some

power.power(9, sst)
-------------------------------------------------------------------
 
S

Steven D'Aprano

kishkin said:
Hello everyone!

I wanted to write python script with defined function. From that script
I wanted to call C module, which in it's turn call defined python
function. Sounds simple. But I've got some troubles in realization. I'm
beginner at python so it's normal, I think. :)

Here's the code. I'll apreciate if anyone can answer me what's wrong
here.
[snip]

> if (!PyCallable_Chack(temp)) //here's error,
but > I don't know why...

Er, do we have to guess what error you are getting?

No, no, I love to guess... should the function be spelt
PyCallable_Chock with an o instead of an a?
 
F

Fredrik Lundh

kishkin said:
I wanted to write python script with defined function. From that script
I wanted to call C module, which in it's turn call defined python
function. Sounds simple. But I've got some troubles in realization. I'm
beginner at python so it's normal, I think. :)

Here's the code. I'll apreciate if anyone can answer me what's wrong
here.

instead of letting everyone guess, maybe you could explain *why* you
think something's wrong with your code ?

(I suspect that if you try to write down what happens when you try
to compile and run this code, you'll figure out how to fix it all by your-
self)

-------------------------------------------------------------------
power.c:

#include "Python.h"

static PyObject *
power(PyObject *self, PyObject *args)
{
PyObject *result, *temp;

int num;

if (PyArg_ParseTuple(args, "iO", &num, &temp))
{
if (!PyCallable_Chack(temp)) //here's error, but I don't know
why...
{
return NULL;
}
}

</F>
 
K

kishkin

:)) Oа course it PyCallable_Check there!!! :)) And error is that
PyCallable_Check returned 0.

power.c compiles without errors.

error is here:
-----------------------------------
[kishkin@linuxa-miho 1]$ make -f makefile.power
gcc power.c -g -I/usr/local/include/python2.4 -fpic -shared -o power.so
[kishkin@linuxa-miho 1]$ python power.py
Exception exceptions.TypeError: 'argument list must be a tuple' in
'garbage collection' ignored
Fatal Python error: enexpected exception during garbage collection
Aborted
-----------------------------------
 
F

Fredrik Lundh

kishkin said:
Thank you for fast response! And I'm sorry for bad style of asking
questions!

did you (with the help of your compiler) figure out what was wrong?

</F>
 
F

Fredrik Lundh

kishkin said:
:)) O? course it PyCallable_Check there!!! :)) And error is that
PyCallable_Check returned 0.

power.c compiles without errors.

error is here:
-----------------------------------
[kishkin@linuxa-miho 1]$ make -f makefile.power
gcc power.c -g -I/usr/local/include/python2.4 -fpic -shared -o power.so
[kishkin@linuxa-miho 1]$ python power.py
Exception exceptions.TypeError: 'argument list must be a tuple' in
'garbage collection' ignored
Fatal Python error: enexpected exception during garbage collection

enexpected? are you typing things in by hand, or does your computer
have a bad RAM chip ?
Aborted
-----------------------------------

try changing

result = Py_BuildValue("i", num*num);
PyEval_CallObject(temp, result);
Py_DECREF(result);

to

result = Py_BuildValue("(i)", num*num);
PyEval_CallObject(temp, result);
Py_DECREF(result);

or, more convenient

result = PyObject_CallFunction(temp, "i", num*num);
if (!result)
return NULL;

(note that you don't really need to check for callables; the CallFunction
API will do that for you, and generate a proper exception if you pass in
the wrong thing).

</F>
 
K

kishkin

Man! It's alive! :)

Now next question: where are you living? I must know that for buy you
some beer!!

Thanks a lot!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top