From: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Date: Sat, 18 Jan 2025 11:01:56 +0000 (+0100) Subject: [3.12] gh-127637: add tests for `dis` command-line interface (#127759) (#127780) X-Git-Tag: v3.12.9~65 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fbbef609d18fffd87ea1c16f3f6cd0bb08803489;p=thirdparty%2FPython%2Fcpython.git [3.12] gh-127637: add tests for `dis` command-line interface (#127759) (#127780) --- diff --git a/Lib/dis.py b/Lib/dis.py index 320dec03d25b..b1069c825289 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -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") diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index c90702a408eb..de7def164187 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -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 index 000000000000..ac5d9827b071 --- /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.