Skip to content

Commit 8198617

Browse files
ambvsobolevn
andauthored
[3.9] bpo-45578: add tests for dis.distb (GH-29332) (#29386)
(cherry picked from commit e346f19) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent 3a93c66 commit 8198617

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Lib/test/test_dis.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,5 +1202,46 @@ def test_from_traceback_dis(self):
12021202
b = dis.Bytecode.from_traceback(tb)
12031203
self.assertEqual(b.dis(), dis_traceback)
12041204

1205+
1206+
class TestDisTraceback(unittest.TestCase):
1207+
def setUp(self) -> None:
1208+
try: # We need to clean up existing tracebacks
1209+
del sys.last_traceback
1210+
except AttributeError:
1211+
pass
1212+
return super().setUp()
1213+
1214+
def get_disassembly(self, tb):
1215+
output = io.StringIO()
1216+
with contextlib.redirect_stdout(output):
1217+
dis.distb(tb)
1218+
return output.getvalue()
1219+
1220+
def test_distb_empty(self):
1221+
with self.assertRaises(RuntimeError):
1222+
dis.distb()
1223+
1224+
def test_distb_last_traceback(self):
1225+
# We need to have an existing last traceback in `sys`:
1226+
tb = get_tb()
1227+
sys.last_traceback = tb
1228+
1229+
self.assertEqual(self.get_disassembly(None), dis_traceback)
1230+
1231+
def test_distb_explicit_arg(self):
1232+
tb = get_tb()
1233+
1234+
self.assertEqual(self.get_disassembly(tb), dis_traceback)
1235+
1236+
1237+
class TestDisTracebackWithFile(TestDisTraceback):
1238+
# Run the `distb` tests again, using the file arg instead of print
1239+
def get_disassembly(self, tb):
1240+
output = io.StringIO()
1241+
with contextlib.redirect_stdout(output):
1242+
dis.distb(tb, file=output)
1243+
return output.getvalue()
1244+
1245+
12051246
if __name__ == "__main__":
12061247
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tests for :func:`dis.distb`

0 commit comments

Comments
 (0)