Python/C API - PyArg_ParseTuple problem ?

Poire Williams poire.williams at nomore.org
Fri Aug 22 13:38:23 EDT 2003


Hi, everybody.

I'm writing a Python binding for a C++ library and I just found something
that puzzles me. In some initializing function for a new type, I parse
arguments using PyArg_ParseTuple as usual. One of the optional arguments
is a string, so it looks like this

if (!PyArg_ParseTuple(args, "ii|s", &a, &b, &s))
    return NULL

// go on...

Whenever I instantiate the new type with a value for the optional string
argument, and try to quit the Python interpreter, it seems to enter an
infinite loop (top(1) shows it consumes all CPU resource), with the
following traceback according to gdb:

#0  0x400d2797 in chunk_free (ar_ptr=0x4017d6a0, p=0x8129ea8) at malloc.c:3251
#1  0x400d2513 in __libc_free (mem=0x8129f60) at malloc.c:3153
#2  0x08057d32 in _PyObject_Del ()
#3  0x0805d2ec in string_dealloc ()
#4  0x080c9740 in PyDict_DelItem ()
#5  0x0805cc8e in PyString_Fini ()
#6  0x080955e7 in Py_Finalize ()
#7  0x080535bb in Py_Main ()
#8  0x08052df6 in main ()
#9  0x4007b17d in __libc_start_main (main=0x8052de0 <main>, argc=1, ubp_av=0xbffff654, init=0x80521d0 <_init>, fini=0x80d0b10 <_fini>, 

What is really strange is that is only happens if I instantiate the new
type like this:

>> t = NewType(0, 42, 'hi')

If I do this:

>> s = 'hi'
>> t = NewType(0, 42, s)

everything is well... Moreover, if I change my C code to something like
this:

if (!PyArg_ParseTuple(args, "ii|O", &a, &b, &o)
    return NULL;
s = PyString_AsString(o);

everything's fine again, in both cases...

I guess it's some kind of twisted reference count thing, and I'm still
trying to get some minimal C code to reproduce this. Meanwhile, does
anybody have a clue ?

Thanks in advance.

Precisions: I'm running Python 2.2.3 on Linux, and I don't free() the
buffer retrieved via PyArg_ParseTuple or PyString_AsString :)

-- 
Jérôme Laheurte <fraca7 at free.fr>
If you didn't get caught, did you really do it?





More information about the Python-list mailing list