Message404606
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 |
|
Date |
User |
Action |
Args |
2021-10-21 14:54:42 | guijarro | set | recipients:
+ guijarro |
2021-10-21 14:54:42 | guijarro | set | messageid: <1634828082.31.0.276861942935.issue45555@roundup.psfhosted.org> |
2021-10-21 14:54:42 | guijarro | link | issue45555 messages |
2021-10-21 14:54:42 | guijarro | create | |
|