Skip to content

Commit 4607f51

Browse files
sobolevnambv
authored andcommitted
[3.9] bpo-45578: add tests for dis.distb (pythonGH-29332).
(cherry picked from commit e346f19) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent 3a93c66 commit 4607f51

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Lib/test/test_dis.py

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

1205+
class TestBytecodeTestCase(BytecodeTestCase):
1206+
def test_assert_not_in_with_op_not_in_bytecode(self):
1207+
code = compile("a = 1", "<string>", "exec")
1208+
self.assertInBytecode(code, "LOAD_CONST", 1)
1209+
self.assertNotInBytecode(code, "LOAD_NAME")
1210+
self.assertNotInBytecode(code, "LOAD_NAME", "a")
1211+
1212+
def test_assert_not_in_with_arg_not_in_bytecode(self):
1213+
code = compile("a = 1", "<string>", "exec")
1214+
self.assertInBytecode(code, "LOAD_CONST")
1215+
self.assertInBytecode(code, "LOAD_CONST", 1)
1216+
self.assertNotInBytecode(code, "LOAD_CONST", 2)
1217+
1218+
def test_assert_not_in_with_arg_in_bytecode(self):
1219+
code = compile("a = 1", "<string>", "exec")
1220+
with self.assertRaises(AssertionError):
1221+
self.assertNotInBytecode(code, "LOAD_CONST", 1)
1222+
1223+
1224+
class TestDisTraceback(unittest.TestCase):
1225+
def setUp(self) -> None:
1226+
try: # We need to clean up existing tracebacks
1227+
del sys.last_traceback
1228+
except AttributeError:
1229+
pass
1230+
return super().setUp()
1231+
1232+
def get_disassembly(self, tb):
1233+
output = io.StringIO()
1234+
with contextlib.redirect_stdout(output):
1235+
dis.distb(tb)
1236+
return output.getvalue()
1237+
1238+
def test_distb_empty(self):
1239+
with self.assertRaises(RuntimeError):
1240+
dis.distb()
1241+
1242+
def test_distb_last_traceback(self):
1243+
# We need to have an existing last traceback in `sys`:
1244+
tb = get_tb()
1245+
sys.last_traceback = tb
1246+
1247+
self.assertEqual(self.get_disassembly(None), dis_traceback)
1248+
1249+
def test_distb_explicit_arg(self):
1250+
tb = get_tb()
1251+
1252+
self.assertEqual(self.get_disassembly(tb), dis_traceback)
1253+
1254+
1255+
class TestDisTracebackWithFile(TestDisTraceback):
1256+
# Run the `distb` tests again, using the file arg instead of print
1257+
def get_disassembly(self, tb):
1258+
output = io.StringIO()
1259+
with contextlib.redirect_stdout(output):
1260+
dis.distb(tb, file=output)
1261+
return output.getvalue()
1262+
1263+
12051264
if __name__ == "__main__":
12061265
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)