[capi-sig] Extension Problem
Ali Alhakim
alika at spray.se
Tue Aug 14 23:27:15 CEST 2007
Thank you Anthony and Joe. I thought that all arguments should be passed into pointers. The introductary example in the python reference documentation about Python extension confused me. I hope that the mentioned example will be changed to something simpler.
On Tue, 14 Aug 2007 17:34:28 +0200, Anthony Tuininga <anthony.tuininga at gmail.com> wrote:
> I suspect the problem is the following:
>
> double *min_eps;
>
> if (!PyArg_ParseTuple(args, "d", &min_eps))
> return NULL;
>
> That should be
>
> double min_eps;
>
> as Python is expecting a double, not a pointer to a double! And since
> the difference is about 4 bytes, that would explain the segfault. :-)
>
> Anthony
>
> On 8/14/07, Ali Alhakim <alika at spray.se> wrote:
>> Hello!
>>
>> I'm quite new to Python and definitely a beginner in implementing Python
>> extensions in C/C++. I've followed the structure given in the formal
>> Python
>> documentation to write the following code block:
>> ===========
>> //cmatmod.c
>>
>> #include
>>
>> static unsigned int eigenvect_calc(double *min_eps)
>> {
>> return 5;
>> }
>>
>> static PyObject *cmat_eigv(PyObject *self, PyObject *args)
>> {
>> double *min_eps;
>> unsigned int m;
>> if(!PyArg_ParseTuple(args, "d", &min_eps))
>> return NULL;
>> m=eigenvect_calc(min_eps);
>> return Py_BuildValue("I", m);
>> }
>> static PyMethodDef cmat_methods[]=
>> {
>> { "eigenvect", cmat_eigv, METH_VARARGS, "Comment"},{NULL, NULL, 0,
>> NULL}
>> };
>>
>> void initcmat(void)
>> {
>> (void) Py_InitModule("cmat", cmat_methods);
>> }
>>
>> ===========
>>
>> I have succeeded to build and install this extension using disutils
>> package
>> in the setup.py file below:
>> ===========
>> from distutils.core import setup
>> from distutils.extension import Extension
>> setup(name='eigenvect',
>> version='1.0',
>> ext_modules=[Extension('cmat', ['cmatmod.c'])],
>> )
>>
>> ==========
>> But when I try to call eigenvect(3.0) from Python I would receive a core
>> dump:
>> ==========
>>
>> 6 [main] python 2336 _cygtls::handle_exceptions: Error while dumping
>> state
>> (probably corrupted stack)
>> Segmentation fault (core dumped)
>>
>> ==========
>> My question is what is wrong with the extension code above? Is it
>> something
>> with reference counting?
>> I don't know which method is suitable for debugging extension codes.
>> I've
>> tried gdb but I didn't understand the debug information:
>> ==========
>>
>> (gdb) >>> eigenvect(4.0)
>>
>> Program received signal SIGSEGV, Segmentation fault.
>> ---Type to continue, or q to quit---
>> 0x6abb248e in libpython2!PyEval_EvalFrameEx () from
>> /usr/bin/libpython2.5dll
>> (gdb)
>>
>> ==========
>> /best regards
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> capi-sig mailing list
>> capi-sig at python.org
>> http://mail.python.org/mailman/listinfo/capi-sig
>>
>>
>
>
> __________ NOD32 2460 (20070814) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>
Spray Webbhotell. Ingen startkostnad, 3 månader gratis och fri support. Perfekt för dig som ska starta en egen webbplats. http://www.spray.se/kampanj
More information about the capi-sig
mailing list