]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-127637: add tests for `dis` command-line interface (#127759) (#127780)
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sat, 18 Jan 2025 11:01:56 +0000 (12:01 +0100)
committerGitHub <noreply@github.com>
Sat, 18 Jan 2025 11:01:56 +0000 (12:01 +0100)
Lib/dis.py
Lib/test/test_dis.py
Misc/NEWS.d/next/Tests/2024-12-09-12-35-44.gh-issue-127637.KLx-9I.rst [new file with mode: 0644]

index 320dec03d25b0cebe2f27547b79d380f09fe8443..b1069c825289cf929a8e0e23709f914b1e491ed7 100644 (file)
@@ -790,12 +790,12 @@ class Bytecode:
             return output.getvalue()
 
 
-def main():
+def main(args=None):
     import argparse
 
     parser = argparse.ArgumentParser()
     parser.add_argument('infile', type=argparse.FileType('rb'), nargs='?', default='-')
-    args = parser.parse_args()
+    args = parser.parse_args(args=args)
     with args.infile as infile:
         source = infile.read()
     code = compile(source, args.infile.name, "exec")
index c90702a408eb336f424467a4e9d9a221e1d2bb5f..de7def164187e42d73c93ad47ad7bedb9aa29b5a 100644 (file)
@@ -3,16 +3,17 @@
 import contextlib
 import dis
 import io
+import opcode
 import re
 import sys
+import tempfile
 import types
 import unittest
 from test.support import (captured_stdout, requires_debug_ranges,
-                          requires_specialization, cpython_only)
+                          requires_specialization, cpython_only,
+                          os_helper)
 from test.support.bytecode_helper import BytecodeTestCase
 
-import opcode
-
 
 def get_tb():
     def _error():
@@ -2069,5 +2070,18 @@ class TestDisTracebackWithFile(TestDisTraceback):
         return output.getvalue()
 
 
+class TestDisCLI(unittest.TestCase):
+
+    def setUp(self):
+        self.filename = tempfile.mktemp()
+        self.addCleanup(os_helper.unlink, self.filename)
+
+    def test_invocation(self):
+        with self.assertRaises(SystemExit):
+            # suppress argparse error message
+            with contextlib.redirect_stderr(io.StringIO()):
+                dis.main(args=['--unknown', self.filename])
+
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Tests/2024-12-09-12-35-44.gh-issue-127637.KLx-9I.rst b/Misc/NEWS.d/next/Tests/2024-12-09-12-35-44.gh-issue-127637.KLx-9I.rst
new file mode 100644 (file)
index 0000000..ac5d982
--- /dev/null
@@ -0,0 +1 @@
+Add tests for the :mod:`dis` command-line interface. Patch by Bénédikt Tran.