[Q]An error with my module in C

J

Jia,Lu

hello,all.

I wrote a module in C as below, BUT msg() method cannot work
allright.

#include <stdio.h>
#include <python2.4/Python.h>

static PyObject *Roka_msg(PyObject *self,PyObject *args)
{
printf("Roka Python lib. Version 1.0\n");
}

static PyObject *Roka_func(PyObject *self,PyObject *args)
{
long arg;
if(!PyArg_ParseTuple(args,"l",&arg)){
return NULL;
}
return Py_BuildValue("l",arg*2);
}

//----------------------------------------------------------

static struct PyMethodDef functions[]=
{
{"msg",Roka_msg,METH_VARARGS},
{"func",Roka_func,METH_VARARGS},
{NULL,NULL,0},
};


void initRoka(void)
{
(void)Py_InitModule("Roka",functions);
}
-------------------------------------------------------------------------
python result:Roka Python lib. Version 1.0
Segmentation fault

I throw out a Segmentation fault after display my message.
Can anyone tell me why?

thanks.
 
R

Robert Kern

Jia said:
hello,all.

I wrote a module in C as below, BUT msg() method cannot work
allright.

#include <stdio.h>
#include <python2.4/Python.h>

This isn't related to your error, but you should include Python.h before other
headers, and it should be just this:

#include "Python.h"

distutils will make sure the include path is correct.
static PyObject *Roka_msg(PyObject *self,PyObject *args)
{
printf("Roka Python lib. Version 1.0\n");
}

You actually need to return a PyObject* here. Specifically, you want to return
None after incrementing its reference count:

Py_INCREF(Py_None);
return Py_None;

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

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

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top