Helloworld with Python C extension

Christian Gollwitzer auriocus at gmx.de
Tue Aug 30 03:12:26 EDT 2016


Am 30.08.16 um 08:06 schrieb Ganesh Pal:
>>
>>
>>
>> Py_BuildValue with an "s" expects a C string - that is, a pointer to
>> char, not just a single character. You'd need to do something like
>> this:
>>
>> char buf[2] = {char1, 0};
>> return Py_BuildValue("s", buf);
>>
>> ChrisA
>
>
> Thanks  Chris for the clue's   it worked,  I  was just wondering  how
> could the
> C extension   be debugged ?
>
>  We have pdb at python side and gdb for C  , can we run gdb on python side
> ?  if there is a crash like the one we saw in the above diff we are
> clueless of what's happening? any idea or recommendation on how we handle
> such cases

1. Write your Python code into a file (e.g. test.py)
2. Run gdb --args python test.py

Then press "r" to start your program. At the crash, gdb should stop your 
program, maybe inside of Py_BuildValue. You must compile your extension 
with debug symbols (-g switch to the compiler) enabled to see line numbers.

In case of memory errors like the above, they can sometimes go unnoticed 
for a while, which makes them hard to debug. If you are on Linux, 
valgrind is the most powerful tool to find these. Run

	valgrind python test.py

It'll show out-of-bounds accesses for arrays immediately.

	Christian




More information about the Python-list mailing list