]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-45578: add tests for `dis.distb` (GH-29332) (#29386)
authorŁukasz Langa <lukasz@langa.pl>
Wed, 3 Nov 2021 16:47:04 +0000 (17:47 +0100)
committerGitHub <noreply@github.com>
Wed, 3 Nov 2021 16:47:04 +0000 (17:47 +0100)
(cherry picked from commit e346f196819aeb02a8a94205ce3e1536c4c2f105)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/test/test_dis.py
Misc/NEWS.d/next/Tests/2021-10-30-19-00-25.bpo-45578.bvu6X2.rst [new file with mode: 0644]

index ac5836d288978ceed69fad68e4537ca11c062ed9..54bab568ab8a307d0e39b8f9171fae79e16c221f 100644 (file)
@@ -1202,5 +1202,46 @@ class BytecodeTests(unittest.TestCase):
         b = dis.Bytecode.from_traceback(tb)
         self.assertEqual(b.dis(), dis_traceback)
 
+
+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()
diff --git a/Misc/NEWS.d/next/Tests/2021-10-30-19-00-25.bpo-45578.bvu6X2.rst b/Misc/NEWS.d/next/Tests/2021-10-30-19-00-25.bpo-45578.bvu6X2.rst
new file mode 100644 (file)
index 0000000..3d0e0ca
--- /dev/null
@@ -0,0 +1 @@
+Add tests for :func:`dis.distb`