Skip to content

Commit fd6b70d

Browse files
ambvsobolevn
andauthored
[3.10] bpo-45578: add tests for dis.distb (pythonGH-29332) (pythonGH-29385)
(cherry picked from commit e346f19) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent 0624706 commit fd6b70d

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
@@ -1244,5 +1244,46 @@ def test_assert_not_in_with_arg_in_bytecode(self):
12441244
with self.assertRaises(AssertionError):
12451245
self.assertNotInBytecode(code, "LOAD_CONST", 1)
12461246

1247+
1248+
class TestDisTraceback(unittest.TestCase):
1249+
def setUp(self) -> None:
1250+
try: # We need to clean up existing tracebacks
1251+
del sys.last_traceback
1252+
except AttributeError:
1253+
pass
1254+
return super().setUp()
1255+
1256+
def get_disassembly(self, tb):
1257+
output = io.StringIO()
1258+
with contextlib.redirect_stdout(output):
1259+
dis.distb(tb)
1260+
return output.getvalue()
1261+
1262+
def test_distb_empty(self):
1263+
with self.assertRaises(RuntimeError):
1264+
dis.distb()
1265+
1266+
def test_distb_last_traceback(self):
1267+
# We need to have an existing last traceback in `sys`:
1268+
tb = get_tb()
1269+
sys.last_traceback = tb
1270+
1271+
self.assertEqual(self.get_disassembly(None), dis_traceback)
1272+
1273+
def test_distb_explicit_arg(self):
1274+
tb = get_tb()
1275+
1276+
self.assertEqual(self.get_disassembly(tb), dis_traceback)
1277+
1278+
1279+
class TestDisTracebackWithFile(TestDisTraceback):
1280+
# Run the `distb` tests again, using the file arg instead of print
1281+
def get_disassembly(self, tb):
1282+
output = io.StringIO()
1283+
with contextlib.redirect_stdout(output):
1284+
dis.distb(tb, file=output)
1285+
return output.getvalue()
1286+
1287+
12471288
if __name__ == "__main__":
12481289
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)