https://github.com/python/cpython/commit/8bf5b89212f37efc47416e941ced19359c779f95
commit: 8bf5b89212f37efc47416e941ced19359c779f95
branch: 3.13
author: Bénédikt Tran <[email protected]>
committer: iritkatriel <[email protected]>
date: 2024-12-10T12:32:32Z
summary:
[3.13] gh-127637: add tests for `dis` command-line interface (#127759) (#127781)
files:
A Misc/NEWS.d/next/Tests/2024-12-09-12-35-44.gh-issue-127637.KLx-9I.rst
M Lib/dis.py
M Lib/test/test_dis.py
diff --git a/Lib/dis.py b/Lib/dis.py
index 76934eb00e63f0..797e0f8a0888d0 100644
--- a/Lib/dis.py
+++ b/Lib/dis.py
@@ -1051,7 +1051,7 @@ def dis(self):
return output.getvalue()
-def main():
+def main(args=None):
import argparse
parser = argparse.ArgumentParser()
@@ -1060,7 +1060,7 @@ def main():
parser.add_argument('-O', '--show-offsets', action='store_true',
help='show instruction offsets')
parser.add_argument('infile', nargs='?', default='-')
- args = parser.parse_args()
+ args = parser.parse_args(args=args)
if args.infile == '-':
name = '<stdin>'
source = sys.stdin.buffer.read()
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index a7f6919655b94a..73807d7fa95fbd 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -4,15 +4,19 @@
import dis
import functools
import io
+import itertools
+import opcode
import re
import sys
+import tempfile
+import textwrap
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
CACHE = dis.opmap["CACHE"]
@@ -2281,5 +2285,91 @@ def _unroll_caches_as_Instructions(instrs,
show_caches=False):
False, None, None, instr.positions)
+class TestDisCLI(unittest.TestCase):
+
+ def setUp(self):
+ self.filename = tempfile.mktemp()
+ self.addCleanup(os_helper.unlink, self.filename)
+
+ @staticmethod
+ def text_normalize(string):
+ """Dedent *string* and strip it from its surrounding whitespaces.
+
+ This method is used by the other utility functions so that any
+ string to write or to match against can be freely indented.
+ """
+ return textwrap.dedent(string).strip()
+
+ def set_source(self, content):
+ with open(self.filename, 'w') as fp:
+ fp.write(self.text_normalize(content))
+
+ def invoke_dis(self, *flags):
+ output = io.StringIO()
+ with contextlib.redirect_stdout(output):
+ dis.main(args=[*flags, self.filename])
+ return self.text_normalize(output.getvalue())
+
+ def check_output(self, source, expect, *flags):
+ with self.subTest(source=source, flags=flags):
+ self.set_source(source)
+ res = self.invoke_dis(*flags)
+ expect = self.text_normalize(expect)
+ self.assertListEqual(res.splitlines(), expect.splitlines())
+
+ def test_invocation(self):
+ # test various combinations of parameters
+ base_flags = [
+ ('-C', '--show-caches'),
+ ('-O', '--show-offsets'),
+ ]
+
+ self.set_source('''
+ def f():
+ print(x)
+ return None
+ ''')
+
+ for r in range(1, len(base_flags) + 1):
+ for choices in itertools.combinations(base_flags, r=r):
+ for args in itertools.product(*choices):
+ with self.subTest(args=args[1:]):
+ _ = self.invoke_dis(*args)
+
+ with self.assertRaises(SystemExit):
+ # suppress argparse error message
+ with contextlib.redirect_stderr(io.StringIO()):
+ _ = self.invoke_dis('--unknown')
+
+ def test_show_cache(self):
+ # test 'python -m dis -C/--show-caches'
+ source = 'print()'
+ expect = '''
+ 0 RESUME 0
+
+ 1 LOAD_NAME 0 (print)
+ PUSH_NULL
+ CALL 0
+ CACHE 0 (counter: 0)
+ CACHE 0 (func_version: 0)
+ CACHE 0
+ POP_TOP
+ RETURN_CONST 0 (None)
+ '''
+ for flag in ['-C', '--show-caches']:
+ self.check_output(source, expect, flag)
+
+ def test_show_offsets(self):
+ # test 'python -m dis -O/--show-offsets'
+ source = 'pass'
+ expect = '''
+ 0 0 RESUME 0
+
+ 1 2 RETURN_CONST 0 (None)
+ '''
+ for flag in ['-O', '--show-offsets']:
+ self.check_output(source, expect, flag)
+
+
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
index 00000000000000..ac5d9827b07199
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2024-12-09-12-35-44.gh-issue-127637.KLx-9I.rst
@@ -0,0 +1 @@
+Add tests for the :mod:`dis` command-line interface. Patch by Bénédikt Tran.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]