diff options
author | Ćukasz Langa <lukasz@langa.pl> | 2021-11-03 16:53:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-03 16:53:36 +0100 |
commit | fd6b70d6b715c2403a194a2b3eae3210e2e81742 (patch) | |
tree | c210dee89724286a13bc6d280c85fbd02c685b16 /Lib/test/test_dis.py | |
parent | 06247061798a1ac402940d6ec04604ffa0be6c7a (diff) | |
download | cpython-git-fd6b70d6b715c2403a194a2b3eae3210e2e81742.tar.gz |
[3.10] bpo-45578: add tests for `dis.distb` (GH-29332) (GH-29385)
(cherry picked from commit e346f196819aeb02a8a94205ce3e1536c4c2f105)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Lib/test/test_dis.py')
-rw-r--r-- | Lib/test/test_dis.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index edda967ce1..2cb2eff9a6 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1244,5 +1244,46 @@ class TestBytecodeTestCase(BytecodeTestCase): with self.assertRaises(AssertionError): self.assertNotInBytecode(code, "LOAD_CONST", 1) + +class TestDisTraceback(unittest.TestCase): + def setUp(self) -> None: + try: # We need to clean up existing tracebacks + del sys.last_traceback + except AttributeError: + pass + return super().setUp() + + def get_disassembly(self, tb): + output = io.StringIO() + with contextlib.redirect_stdout(output): + dis.distb(tb) + return output.getvalue() + + def test_distb_empty(self): + with self.assertRaises(RuntimeError): + dis.distb() + + def test_distb_last_traceback(self): + # We need to have an existing last traceback in `sys`: + tb = get_tb() + sys.last_traceback = tb + + self.assertEqual(self.get_disassembly(None), dis_traceback) + + def test_distb_explicit_arg(self): + tb = get_tb() + + self.assertEqual(self.get_disassembly(tb), dis_traceback) + + +class TestDisTracebackWithFile(TestDisTraceback): + # Run the `distb` tests again, using the file arg instead of print + def get_disassembly(self, tb): + output = io.StringIO() + with contextlib.redirect_stdout(output): + dis.distb(tb, file=output) + return output.getvalue() + + if __name__ == "__main__": unittest.main() |