By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

[Q]An error with my module in C

P: n/a
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:
>>>import Roka
Roka.msg()
Roka Python lib. Version 1.0
Segmentation fault

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

thanks.

Oct 3 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Jia,Lu wrote:
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

Oct 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.