Python/C API - PyArg_ParseTuple problem ?

"Jérôme "Jérôme
Fri Aug 22 17:05:46 EDT 2003


On Fri, 22 Aug 2003 18:23:28 +0000, Michael Hudson wrote:

>> If I do this:
>> 
>> >> s = 'hi'
>> >> t = NewType(0, 42, s)
>> 
>> everything is well... 
> 
> Even after a 'del s'?

Just tried it, even after a 'del s' everything's right.

>> Precisions: I'm running Python 2.2.3 on Linux, and I don't free() the
>> buffer retrieved via PyArg_ParseTuple or PyString_AsString :)
> 
> No, but I bet you're stuffing it into your data structure somewhere
> along the line.  I think you need to copy the string data.

That's the whole point, I took a great care to copy it, and never keep a
reference to the original buffer. As a precision, the binding is for fltk2
(http://www.fltk.org/).

I still haven't been able to reproduce it with a minimal source code, but
if you want to try it the source should be available at

http://fraca7.homeunix.net/pyfltk2-0.0.0.tar.gz

at least if I didn't screw up with the whole NAT thingies.

As you can see, the string buffer is passed to
WidgetWrapper::WidgetWrapper, which copies it by using
fltk::Widget::copy_label. I've checked the fltk code to ensure the buffer
was copied via strdup().

What really puzzles me is that difference between the "ii|s" and
"ii|O"/PyString_AsString stuff. Wouldn't one expect to have the same
result, right or wrong, from both techniques ?

-- 
Jérôme Laheurte <fraca7 at free.fr>
The human mind treats a new idea the way the body treats a strange
protein -- it rejects it.
                -- P. Medawar





More information about the Python-list mailing list