Python C/API simple debugging

Diez B. Roggisch deets at nospam.web.de
Wed Nov 26 06:43:21 EST 2008


k3xji wrote:

> On Nov 26, 1:34 pm, Stefan Behnel <stefan... at behnel.de> wrote:
>> k3xji wrote:
>> > I am new to Python C API and finding it difficult to debug C
>> > extensions. So, basically I want to see the value of an integer value
>> > during the C API. Here is the code:
>>
>> > #define LAST_MIX_VAL 0xDEADBEEF
>>
>> > static PyObject *
>> > chash(PyObject *self, PyObject *args)
>> > {
>> > unsigned int key,result; //treat key like an usinged int.
>> > unsigned char a,b,c,d;
>>
>> > key = result = 0;
>> > if (!PyArg_ParseTuple(args, "i", &key))
>> > return NULL;
>>
>> > printf("Key:%i\n",Py_BuildValue("i", key));
>> > .
>> > .
>> > [...]
>> > - What is the preffered approach for these kind simple-debugging
>> > issue?
>>
>> If you want to simplify this kind of debugging as well as the general
>> code writing itself, consider using Cython.
>>
>> http://cython.org/
>>
>> Apart from that, I'd use gdb for debugging these things. It does have a
>> learning curve, but in change gives you a lot more than just
>> "print-debugging".
>>
>> Stefan
> 
> OK.
> 
> How to use gdb? I am compiling the extension to a "pyd" file and
> importing it right now. So, I assume we need to, somehow load the
> extension dynamically for debugging? But how?

You write a testscript that exposes the error you want to observe in the
C-layer.

Then you do

$ gdb python
# set args testscript.py
# break <some breakpoint, see how to enter that in gdb-docs>
# run

That pretty much is it. You can't single-step python code this way, but you
can walk through C/C++-code.

Diez



More information about the Python-list mailing list