Re: [Python-Dev] Py_CLEAR and assigning values

2008-08-06 Thread Martin v. Löwis
Guido van Rossum wrote: > I don't see a way that __del__ could be invoked and access the NULL > between the Py_CLEAR() call and the Py_INCREF() call in the second > version. Consider this: class Bad: def __del__(self): obj.some_method() obj.set_foo(Bad()) obj.set_foo(None) Now,

Re: [Python-Dev] Py_CLEAR and assigning values

2008-08-05 Thread Daniel Stutzbach
On Tue, Aug 5, 2008 at 4:30 PM, Paul Pogonyshev <[EMAIL PROTECTED]> wrote: > > Generally, I end up storing all the objects to be Py_DECREF'd in > temporary > > variables and doing the Py_DECREF's just before returning. That way, > "self" > > is never in an inconsistent state. > > Right. But woul

Re: [Python-Dev] Py_CLEAR and assigning values

2008-08-05 Thread Greg Ewing
Paul Pogonyshev wrote: del obj.x obj.x = y though I'm not sure if for Python code x.__del__ will see obj.x as non-set attribute (I guess so, but I'm not sure). A quick experiment suggests that it does: Python 2.5 (r25:51908, Apr 8 2007, 22:22:18) [GCC 3.3 20030304 (Apple Com

Re: [Python-Dev] Py_CLEAR and assigning values

2008-08-05 Thread Antoine Pitrou
Paul Pogonyshev gmx.net> writes: > > Right. But wouldn't it be easier if there was a standard Python macro > for this, sort of like proposed Py_ASSIGN? I proposed something similar in http://bugs.python.org/issue3081. ___ Python-Dev mailing list Py

Re: [Python-Dev] Py_CLEAR and assigning values

2008-08-05 Thread Paul Pogonyshev
Daniel Stutzbach wrote: > On Tue, Aug 5, 2008 at 3:38 PM, Paul Pogonyshev <[EMAIL PROTECTED]> wrote: > > > Py_CLEAR way: > > > >Py_CLEAR (self->x); > >/* But __del__ can now in principle trigger access to NULL. */ > >self->x = y; > >Py_INCREF (self->x); > > The Py_

Re: [Python-Dev] Py_CLEAR and assigning values

2008-08-05 Thread Daniel Stutzbach
On Tue, Aug 5, 2008 at 3:38 PM, Paul Pogonyshev <[EMAIL PROTECTED]> wrote: > Py_CLEAR way: > >Py_CLEAR (self->x); >/* But __del__ can now in principle trigger access to NULL. */ >self->x = y; >Py_INCREF (self->x); The Py_DECREF inside the Py_CLEAR may call arbitra

Re: [Python-Dev] Py_CLEAR and assigning values

2008-08-05 Thread Guido van Rossum
I don't see a way that __del__ could be invoked and access the NULL between the Py_CLEAR() call and the Py_INCREF() call in the second version. On Tue, Aug 5, 2008 at 1:38 PM, Paul Pogonyshev <[EMAIL PROTECTED]> wrote: > Hi, > > Sorry if this topic was discussed before, but I couldn't find. > > Py

[Python-Dev] Py_CLEAR and assigning values

2008-08-05 Thread Paul Pogonyshev
Hi, Sorry if this topic was discussed before, but I couldn't find. Py_CLEAR documentation explains why it is better than Py_DECREF and NULL assignment. However, I don't understand why there is no similar macro for the case you want to replace one object with another? I.e. 'naive' way: