473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Seg fault in python extension module

Hello,

I am working on my first python module based on a c program. The
module builds and installs OK using dist-utils, and imports fine into
python. However, when I try and use the one wrapper
("modgl.glVertex4f(1, 2, 3, 1)") in the module, it seg faults.

Can anyone spot why this isn't working, or recommend a way to debug
these things.

Thanks,
Luke

#include <Python.h>

static PyObject *_wrap_glVertex4f(PyObject *self, PyObject *args) {
PyObject *resultobj = NULL;
float arg1 ;
float arg2 ;
float arg3 ;
float arg4 ;
PyObject * obj0 = 0 ;
PyObject * obj1 = 0 ;
PyObject * obj2 = 0 ;
PyObject * obj3 = 0 ;

if(!PyArg_ParseTuple(args,(char
*)"OOOO:glVertex4f",&obj0,&obj1,&obj2,&obj3)) goto fail;
{
arg1 = (float)(PyFloat_AsDouble(obj0));
}
{
arg2 = (float)(PyFloat_AsDouble(obj1));
}
{
arg3 = (float)(PyFloat_AsDouble(obj2));
}
{
arg4 = (float)(PyFloat_AsDouble(obj3));
}
glVertex4f(arg1,arg2,arg3,arg4);

Py_INCREF(Py_None); resultobj = Py_None;
return resultobj;
fail:
return NULL;
};
static PyMethodDef modglMethods[] = {
{ (char *)"glVertex4f", _wrap_glVertex4f, METH_VARARGS, NULL},
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC modgl(void)
{
(void) Py_InitModule("modgl", modglMethods);
};
int
main(int argc, char *argv[])
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);

/* Initialize the Python interpreter. Required. */
Py_Initialize();

/* Add a static module */
initmodgl();
return 0;
};
Jun 2 '06 #1
4 1862
On 3/06/2006 9:51 AM, Luke Miller wrote:
Hello,

I am working on my first python module based on a c program. The
module builds and installs OK using dist-utils, and imports fine into
python. However, when I try and use the one wrapper
("modgl.glVertex4f(1, 2, 3, 1)") in the module, it seg faults.

Can anyone spot why this isn't working, or recommend a way to debug
these things.
Thanks,
Luke

#include <Python.h>

static PyObject *_wrap_glVertex4f(PyObject *self, PyObject *args) {
PyObject *resultobj = NULL;
float arg1 ;
float arg2 ;
float arg3 ;
float arg4 ;
PyObject * obj0 = 0 ;
PyObject * obj1 = 0 ;
PyObject * obj2 = 0 ;
PyObject * obj3 = 0 ;

if(!PyArg_ParseTuple(args,(char
*)"OOOO:glVertex4f",&obj0,&obj1,&obj2,&obj3)) goto fail;
{
arg1 = (float)(PyFloat_AsDouble(obj0));
Not testing for an error after an API call is just asking for trouble.

}
{
arg2 = (float)(PyFloat_AsDouble(obj1));
}
{
arg3 = (float)(PyFloat_AsDouble(obj2));
}
{
arg4 = (float)(PyFloat_AsDouble(obj3));
}

You are making it really hard for yourself. Try this:
if(!PyArg_ParseTuple(args,"ffff:glVertex4f",&arg1, &arg2 etc etc

The batteries are already included. Using the code for the precise type
that you want gives you checking for type mismatches, a with sensible
error message.

glVertex4f(arg1,arg2,arg3,arg4);
You don't declare this function before calling it. That's dangerous.

Py_INCREF(Py_None); resultobj = Py_None;
return resultobj;
fail:
return NULL;
};
static PyMethodDef modglMethods[] = {
{ (char *)"glVertex4f", _wrap_glVertex4f, METH_VARARGS, NULL},
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC modgl(void)
I can't see how you were able to import the module. For "import modgl"
to work, your initialisation function should be called "initmodgl".
{
(void) Py_InitModule("modgl", modglMethods);
};


You are writing an extension, not embedding Python. You don't need a
main(). Lose it.
int
main(int argc, char *argv[])
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);

/* Initialize the Python interpreter. Required. */
Py_Initialize();

/* Add a static module */
initmodgl();
return 0;
};


Below is a cutdown version of your module, that returns a value so that
you can see that it is working. You might like to use that as a base.
Here it is working:

|>>> import modgl
|>>> modgl.glVertex4f(1, 2, 3, 4)
10.0
|>>> modgl.glVertex4f(1, 2, "three", 4)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: a float is required
|>>>

HTH,
John

8<---
#include <Python.h>

static PyObject *
_wrap_glVertex4f(PyObject *self, PyObject *args) {
PyObject *resultobj = NULL;
float arg1, arg2, arg3, arg4;
if(!PyArg_ParseTuple(args,"ffff:glVertex4f",&arg1, &arg2,&arg3,&arg4))
goto fail;
/* glVertex4f(arg1,arg2,arg3,arg4); */

/* Py_INCREF(Py_None); resultobj = Py_None; */

resultobj = Py_BuildValue("d", (double)(arg1 + arg2 + arg3 + arg4));

return resultobj;
fail:
return NULL;

};

static PyMethodDef modglMethods[] = {
{ (char *)"glVertex4f", _wrap_glVertex4f, METH_VARARGS, NULL},
{ NULL, NULL, 0, NULL }
};

PyMODINIT_FUNC
initmodgl(void)
{
(void) Py_InitModule("modgl", modglMethods);
};
8<---
Jun 3 '06 #2
sam
I recommend that you also replace the NULL after the METH_VARARGS with
a valid documentations string such as:

static PyMethodDef modglMethods[] =
{
{ (char *)"glVertex4f", _wrap_glVertex4f, METH_VARARGS, "My
Doc String"},
{ NULL, NULL, 0, NULL }

};

Sam Schulenburg

Jun 3 '06 #3
On 3/06/2006 1:38 PM, sam wrote:
I recommend that you also replace the NULL after the METH_VARARGS with
a valid documentations string such as:

static PyMethodDef modglMethods[] =
{
{ (char *)"glVertex4f", _wrap_glVertex4f, METH_VARARGS, "My
Doc String"},
{ NULL, NULL, 0, NULL }

};


Lack of one is unlikely to have anything to do with the OP's segfault.

|>>> repr(modgl.glVertex4f.__doc__)
'None'
|>>>

As to style, etiquette, and good citizenship in module extension
writing, it might be better to give him some references, rather than
mention just one point.

Cheers,
John
Jun 3 '06 #4
sam
Sorry, From MethodObject.h the 4th parameter is usually documented as
having a NULL, but is intended to be used for a documentation string
that will be available to the user under the various GUI IDE's such as
IDLE or PyWin32. I just wanted to point that out..

struct PyMethodDef {
const char *ml_name; /* The name of the built-in function/method */
PyCFunction ml_meth; /* The C function that implements it */
int ml_flags; /* Combination of METH_xxx flags, which mostly
describe the args expected by the C func */
const char *ml_doc; /* The __doc__ attribute, or NULL */
};

Sam Schulenburg

John Machin wrote:
On 3/06/2006 1:38 PM, sam wrote:
I recommend that you also replace the NULL after the METH_VARARGS with
a valid documentations string such as:

static PyMethodDef modglMethods[] =
{
{ (char *)"glVertex4f", _wrap_glVertex4f, METH_VARARGS, "My
Doc String"},
{ NULL, NULL, 0, NULL }

};


Lack of one is unlikely to have anything to do with the OP's segfault.

|>>> repr(modgl.glVertex4f.__doc__)
'None'
|>>>

As to style, etiquette, and good citizenship in module extension
writing, it might be better to give him some references, rather than
mention just one point.

Cheers,
John


Jun 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
2
by: Rolf Wester | last post by:
Hi, I' trying to make an extension module that passes Numeric arrays. The wrapper function is (swig generated and modified by myself): static PyObject *_wrap_my_func(PyObject *self, PyObject...
1
by: Kirill Simonov | last post by:
Hi, Could someone tell me why my extension module works under Python 2.4, but fails with Segmentation Fault under Python 2.3? Here is the stripped version: ...
1
by: Petr Prikryl | last post by:
Do you think that the following could became PEP (pre PEP). Please, read it, comment it, reformulate it,... Abstract Introduction of the mechanism for language extensions via modules...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.