Skip to content

Commit daebaff

Browse files
committed
Add test for subinterpreters + tracemalloc
1 parent f602208 commit daebaff

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Lib/test/test__interpreters.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pickle
44
from textwrap import dedent
55
import threading
6+
import tracemalloc
67
import unittest
78

89
from test import support
@@ -1098,5 +1099,34 @@ def script(spam, /):
10981099
_interpreters.run_func(self.id, script)
10991100

11001101

1102+
class TestTracemallocWithInterpreters(TestBase):
1103+
def setUp(self):
1104+
if tracemalloc.is_tracing():
1105+
self.skipTest("tracemalloc must be stopped before the test")
1106+
1107+
tracemalloc.start(1)
1108+
1109+
def tearDown(self):
1110+
tracemalloc.stop()
1111+
super().tearDown()
1112+
1113+
def test_trace_interp_start_frames(self):
1114+
interpid = _interpreters.create()
1115+
# check that tracing captures frames created by the new interpreter
1116+
self.assertNotEqual(
1117+
tracemalloc.take_snapshot().statistics("filename"),
1118+
[]
1119+
)
1120+
_interpreters.destroy(interpid)
1121+
1122+
def test_trace_interp_frames(self):
1123+
interpid = _interpreters.create()
1124+
_interpreters.run_string(interpid, "def f(): ...")
1125+
stats = tracemalloc.take_snapshot().statistics("filename")
1126+
# check that the last frame is the string run in the interpreter
1127+
self.assertEqual(stats[-1].traceback._frames[0][0], "<string>")
1128+
_interpreters.destroy(interpid)
1129+
1130+
11011131
if __name__ == '__main__':
11021132
unittest.main()

0 commit comments

Comments
 (0)