This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author guijarro
Recipients guijarro
Date 2021-10-21.14:54:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1634828082.31.0.276861942935.issue45555@roundup.psfhosted.org>
In-reply-to
Content
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))
        #raise RuntimeError() <<< enable this line of code and be surprised!

try:
    A()
finally:
    print(refs[0]())
```

The code prints None ; but, if the RuntimeError exception is raised
in the constructor, we find the object in the final print. It is not
dereferenced properly, as it seems:

```
<__main__.A object at 0x7f4b6cf23ed0>
Traceback (most recent call last):
  File "/tmp/test.py", line 11, in <module>
    A()
  File "/tmp/test.py", line 8, in __init__
    raise RuntimeError()
RuntimeError
```

I tried adding `gc.collect()` with no luck.

Am I doing something wrong ?

Thanks in advance
History
Date User Action Args
2021-10-21 14:54:42guijarrosetrecipients: + guijarro
2021-10-21 14:54:42guijarrosetmessageid: <1634828082.31.0.276861942935.issue45555@roundup.psfhosted.org>
2021-10-21 14:54:42guijarrolinkissue45555 messages
2021-10-21 14:54:42guijarrocreate