PyRun_String
Matthias
nitrogenycs at web.de
Thu Mar 9 14:18:13 EST 2006
Am Thu, 09 Mar 2006 20:06:54 +0100 hat Duncan Booth
<duncan.booth at invalid.invalid> geschrieben:
> Matthias wrote:
>
>> Log("Marker 1");
>> Py_XDECREF( PyRun_String( "print 'Hi!'", Py_single_input, Dict, Dict) );
>> Log("Marker 2");
>>
>> The output looks like
>>
>> Marker 1
>> Hi!
>> Hi!
>> Marker 2
>>
>> Obviously Hi! is printed twice.
>
> Py_XDECREF is a C macro. If you check its definition you will see that it
> does indeed evaluate the argument twice. This is a not uncommon problem
> when using macros in C: if in doubt, do not call a macro with anything
> as a parameter which has side effects.
>
> #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Arrrghh, I've seen the evil side today; C macros are surely devil-sent. I
spent hours tracking down this bug, I didn't even consider the Py_XDECREF
and concentrated on PyRun_String. It's a pity C doesn't support inline
functions. Thanks a lot for pointing this out before I lost my head,
Duncan!
Maybe Py_XDECREF(op) could be replaced with
#define Py_XDECREF(op) {PyObject* obj = (op); if (obj == NULL) ; else
Py_DECREF(obj)}
but I guess this eats one or two clock cycles too much for other people
who use Py_XDECREF.
Thanks again!
-Matthias
More information about the Python-list
mailing list