[Python-Dev] Re: Adding a scarier warning to object.__del__?

2020-01-04 Thread Nick Coghlan
On Fri., 3 Jan. 2020, 3:52 am Yonatan Zunger,  wrote:

> weakref.finalize always executes during the ordinary Python flow, IIUC --
> e.g., it happens before the interpreter is stopping. I guess it does still
> have the "arbitrary thread" limitation -- which is a good point, I may have
> some code bugs of my own to fix. But it's a huge difference in terms of
> predictability of the interpreter state, and in particular both the issues
> around deleted modules and the ones around threading no longer working
> don't apply to it.
>
> atexit is a pretty corner-case thing -- I agree that it shouldn't be the
> common thing at all, and the recommendation should be specifically for
> those use cases where it makes sense.
>

And in the context of this particular discussion, has the limitation that
atexit handlers run *after* the interpreter waits for non-daemon threads to
stop.

Cheers,
Nick.



>>
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/FLQJEIDZQFJMUIWRD66XDYWQNLZUYKHY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Adding a scarier warning to object.__del__?

2020-01-04 Thread Greg Ewing

On 5/01/20 11:41 am, Antoine Pitrou wrote:


+1 for pointing to `weakref.finalize` as a better
alternative for things that may survive until interpreter shutdown.


I'm not sure that weakref.finalize is much better. It can still be
called at an arbitrary time from an arbitrary thread with the rest
of the system in an unknown state, so it needs to be written very
defensively to cope with that.

The main thing I can see weakref.finalize having over __del__ is
that it doesn't give you access to the destroyed object, so you
can't accidentally rely on some part of it that's no longer valid.
But that's only a subset of the ways it can get you into trouble.

I don't think a blanket warning against using __del__ would be
very helpful, because many of its pitfalls also apply to the
suggested alternatives. This is an area where there's no
substitute for an in-depth understanding of the issues.

--
Greg
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/DHH6QWTHSD4TCQ2V2BEMHYG6K36KRSHY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Adding a scarier warning to object.__del__?

2020-01-04 Thread Antoine Pitrou
On Wed, 1 Jan 2020 11:51:41 -0800
"Gregory P. Smith"  wrote:
> On Wed, Jan 1, 2020 at 6:40 AM Andrew Svetlov 
> wrote:
> 
> > __del__ is very useful not for interpreter shutdown but as a regular
> > destructor in object's lifecycle.
> >  
> 
> The reason we should warn people against ever implementing __del__ is that
> people rarely actually understand object lifecycle.  Its presence
> guarantees delayed garbage collection and that its code will code execute
> in a context way outside of normal control flow that all other methods are
> invoked from.

For the latter (executes in an arbitrary context): agreed.
For the former (guarantees delayed garbage collection): what do you
mean?

Regardless, +1 for pointing to `weakref.finalize` as a better
alternative for things that may survive until interpreter shutdown.
Most people don't know about this kind of detail, and it can bite hard
when working on complex systems.

Regards

Antoine.

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/3KP3YPMR2HX33QTBV7D5TBHIJBPL6FNW/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Adding a scarier warning to object.__del__?

2020-01-04 Thread Andrew Svetlov
On Sat, Jan 4, 2020 at 5:43 AM Inada Naoki  wrote:
>
> I don't want to recommend atexit or weakref because they have their pitfall.
>
> I think the context manager should be considered at first when people want
> to use __del__.
>

I support this proposal :)

> --
> Inada Naoki  
> ___
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-dev@python.org/message/RSG6DHNG7ZTJCNYSAMVOFFQSD7HFSGKE/
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
Thanks,
Andrew Svetlov
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/YA6574IBBBIMAJESCFKJXLISFQXXQ475/
Code of Conduct: http://python.org/psf/codeofconduct/