[Python-checkins] [3.10] bpo-45578: add tests for `dis.distb` (GH-29332) (GH-29385)

ambv webhook-mailer at python.org
Wed Nov 3 11:53:54 EDT 2021


https://github.com/python/cpython/commit/fd6b70d6b715c2403a194a2b3eae3210e2e81742
commit: fd6b70d6b715c2403a194a2b3eae3210e2e81742
branch: 3.10
author: Łukasz Langa <lukasz at langa.pl>
committer: ambv <lukasz at langa.pl>
date: 2021-11-03T16:53:36+01:00
summary:

[3.10] bpo-45578: add tests for `dis.distb` (GH-29332) (GH-29385)

(cherry picked from commit e346f196819aeb02a8a94205ce3e1536c4c2f105)

Co-authored-by: Nikita Sobolev <mail at sobolevn.me>

files:
A Misc/NEWS.d/next/Tests/2021-10-30-19-00-25.bpo-45578.bvu6X2.rst
M Lib/test/test_dis.py

diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index edda967ce1e1c..2cb2eff9a6c3f 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -1244,5 +1244,46 @@ def test_assert_not_in_with_arg_in_bytecode(self):
         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()
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
index 0000000000000..3d0e0ca3f04a1
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2021-10-30-19-00-25.bpo-45578.bvu6X2.rst
@@ -0,0 +1 @@
+Add tests for :func:`dis.distb`



More information about the Python-checkins mailing list