Bug in Python/C API?

Paul Duffin pduffin at hursley.ibm.com
Tue Sep 12 04:19:27 EDT 2000


Rainer Deyke wrote:
> 
> "Alex" <cut_me_out at hotmail.com> wrote in message
> news:etd8zsysssw.fsf at oliver.mit.edu...
> >
> > >>>>> "Rainer" == Rainer Deyke <root at rainerdeyke.com> writes:
> >
> >     Rainer> I noticed something very strange: when I use
> >     Rainer> PyObject_CallMethod to call a method on a class instance,
> >     Rainer> the method gets called twice.  I am using Python version
> >     Rainer> 1.5.2.
> >
> > Could you post some code that does this?
> 
> Actually, I think I found the problem.  I was doing this:
> 
> Py_XDECREF(PyObject_CallMethod(...));
> 
> This of course executes the PyObject_CallMethod(...) twice if it did not
> return 0 the first time.
> 
> I really should know better by now.
> 
> This sort of problem could be prevented if Py_XDECREF was defined like this:
> 
> #define Py_XDECREF(A) if (tmp = (A)) Py_DECREF(A)
> 
> where tmp is a global PyObject *.
> 

You probably mean 
	#define Py_XDECREF(A) if (tmp = (A)) Py_DECREF(tmp)
a global is not a good idea because if it is used by multiple threads
then you will have severe problems.

	#define Py_XDECREF(A) {PyObject *tmp = (A); if (tmp) Py_DECREF(tmp);}



More information about the Python-list mailing list