Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb17094

Browse files
committed
disable the garbage collector while collecting traces, so that __del__s don't get caught
1 parent fc49f2a commit cb17094

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

Lib/test/test_sys_setprofile.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gc
12
import pprint
23
import sys
34
import unittest
@@ -354,9 +355,17 @@ def protect(f, p):
354355
def capture_events(callable, p=None):
355356
if p is None:
356357
p = HookWatcher()
357-
sys.setprofile(p.callback)
358-
protect(callable, p)
359-
sys.setprofile(None)
358+
# Disable the garbage collector. This prevents __del__s from showing up in
359+
# traces.
360+
old_gc = gc.isenabled()
361+
gc.disable()
362+
try:
363+
sys.setprofile(p.callback)
364+
protect(callable, p)
365+
sys.setprofile(None)
366+
finally:
367+
if old_gc:
368+
gc.enable()
360369
return p.get_events()[1:-1]
361370

362371

0 commit comments

Comments
 (0)