[issue45555] Object stays alive for weak reference if an exception happens in constructor

2022-01-28 Thread Irit Katriel
Irit Katriel added the comment: I don't think there's any suitable place in the documentation to describe this. Like Pablo says, it is one of the many ways in which you can create a reference to an object. -- nosy: +iritkatriel stage: -> resolved status: open -> closed

[issue45555] Object stays alive for weak reference if an exception happens in constructor

2021-11-22 Thread Andrei Kulakov
Andrei Kulakov added the comment: Note also that in addition to not being related to weakref as Pablo said, it's also not related to the __init__() -- the exception can be moved to any method of the object with the same result. It may be good to update the title so that it's not misleading?

[issue45555] Object stays alive for weak reference if an exception happens in constructor

2021-10-22 Thread Pablo Galindo Salgado
Pablo Galindo Salgado added the comment: I concur, this is not a bug. The contract of the weakref is that the callback will be triggered when the object is destroyed, but here the object is not destroyed because is owned by the frame in the traceback. As Mark correctly mentions, the object

[issue45555] Object stays alive for weak reference if an exception happens in constructor

2021-10-22 Thread Matias G
Matias G added the comment: About the reference in exception: I thought that Python 3 didn't have the need for `sys.exc_clear()` (which has been removed), specifically for this kind of problem. The code I use is in fact more complex than the small snippet I posted here to reproduce the

[issue45555] Object stays alive for weak reference if an exception happens in constructor

2021-10-22 Thread Mark Dickinson
Mark Dickinson added the comment: I don't think this is a bug: there's still a reference to the `A` instance in `sys.exc_info()` (specifically, in the exception traceback) in this case, so that instance is still alive. If you add an `except: pass` clause to your `try / finally`, you should

[issue45555] Object stays alive for weak reference if an exception happens in constructor

2021-10-22 Thread Joannah Nanjekye
Joannah Nanjekye added the comment: I can reproduce the described scenario. I will nosy Pablo and Neil for another eye. I suggest that if it's not a bug, then maybe the docs should be updated to explain this behavior. The only part that talks about exceptions in the documentation for

[issue45555] Object stays alive for weak reference if an exception happens in constructor

2021-10-21 Thread Matias G
New submission from Matias G : Hi Python developers, I stumbled on a weird behavior, which might be a bug actually. I am surprised by the output of the following piece of code: ``` import weakref refs = [] class A: def __init__(self): refs.append(weakref.ref(self))