Skip to content

Instantly share code, notes, and snippets.

@Bogdanp
Created Oct 14, 2020
Embed
What would you like to do?
import gc
import time
class Message:
def __init__(self):
self._exception = None
def __del__(self):
print("running!")
def f():
raise RuntimeError("test")
def g():
m = Message()
d = {"a": m} # putting something else on the stack with a reference to m
try:
f()
except Exception as e:
m._exception = e
g()
print("pre sleep 1")
time.sleep(1)
gc.collect()
print("after sleep 1")
time.sleep(1)
print("after sleep 2")
@Bogdanp
Copy link
Author

Bogdanp commented Oct 14, 2020

Yields:

python test.py
pre sleep 1
running!
after sleep 1
after sleep 2

on Python 3.8 on macOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment