一、问题背景
原始问题的介绍见此issue。
import weakref
class A: pass
ref = None
def x():
global ref
cool_var = A()
ref = weakref.ref(cool_var)
assert ref()
try:
1/0
except Exception as e:
ee = e
try:
x()
except Exception:
pass
print(ref())
assert ref() is None
在python2中执行下面的示例代码不会有异常,输出结果如下所示。
None
但是在python3中执行就会发现断言错误,即ref()
引用不为空。
<__main__.A object at 0x7ff97cc9bfd0>
Traceback (most recent call last):
File "/home/xxx/test_error_leak.py", line 27, in <module>
assert ref() is None
AssertionError