Skip to content

Instantly share code, notes, and snippets.

@Bogdanp
Created October 14, 2020 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bogdanp/cc59e34fdd60b9d163df0a43e4621dae to your computer and use it in GitHub Desktop.
Save Bogdanp/cc59e34fdd60b9d163df0a43e4621dae to your computer and use it in GitHub Desktop.
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