From 0b929a715aeb26f1d235cc81d68e44995bbc2ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 9 Dec 2024 19:02:22 +0100 Subject: [PATCH 1/4] gh-127637: add tests for `dis` command-line interface (#127759) --- Lib/dis.py | 4 ++-- Lib/test/test_dis.py | 17 ++++++++++++++++- ...24-12-09-12-35-44.gh-issue-127637.KLx-9I.rst | 1 + 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2024-12-09-12-35-44.gh-issue-127637.KLx-9I.rst diff --git a/Lib/dis.py b/Lib/dis.py index 320dec03d25b0c..b1069c825289cf 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -790,12 +790,12 @@ def dis(self): 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 c90702a408eb33..397a4029b4b05c 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -5,10 +5,12 @@ import io 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 @@ -2069,5 +2071,18 @@ def get_disassembly(self, tb): 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 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. From f07710bd55a499686c2cbf9a4e7592796bb1d4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 10 Dec 2024 11:12:35 +0100 Subject: [PATCH 2/4] backport isort --- Lib/test/test_dis.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 397a4029b4b05c..de7def164187e4 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -3,6 +3,7 @@ import contextlib import dis import io +import opcode import re import sys import tempfile @@ -13,8 +14,6 @@ os_helper) from test.support.bytecode_helper import BytecodeTestCase -import opcode - def get_tb(): def _error(): From 68a33f3c6687698969ff5ea45dadf291a156f49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:14:49 +0100 Subject: [PATCH 3/4] fix --- Lib/test/test_embed.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index 13713cf37b83a4..c931d160350545 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -348,12 +348,12 @@ def test_simple_initialization_api(self): @support.requires_specialization def test_specialized_static_code_gets_unspecialized_at_Py_FINALIZE(self): # https://github.com/python/cpython/issues/92031 + from test.test_dis import ADAPTIVE_WARMUP_DELAY - code = textwrap.dedent("""\ + code = textwrap.dedent(f"""\ import dis import importlib._bootstrap import opcode - import test.test_dis def is_specialized(f): for instruction in dis.get_instructions(f, adaptive=True): @@ -373,7 +373,7 @@ def is_specialized(f): assert not is_specialized(func), "specialized instructions found" - for i in range(test.test_dis.ADAPTIVE_WARMUP_DELAY): + for i in range({ADAPTIVE_WARMUP_DELAY}): func(importlib._bootstrap, ["x"], lambda *args: None) assert is_specialized(func), "no specialized instructions found" From 019dbb5aa1a787d1bdb138c9b56967fe6d70ce6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:51:18 +0100 Subject: [PATCH 4/4] Revert "fix" This reverts commit 68a33f3c6687698969ff5ea45dadf291a156f49f. --- Lib/test/test_embed.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index c931d160350545..13713cf37b83a4 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -348,12 +348,12 @@ def test_simple_initialization_api(self): @support.requires_specialization def test_specialized_static_code_gets_unspecialized_at_Py_FINALIZE(self): # https://github.com/python/cpython/issues/92031 - from test.test_dis import ADAPTIVE_WARMUP_DELAY - code = textwrap.dedent(f"""\ + code = textwrap.dedent("""\ import dis import importlib._bootstrap import opcode + import test.test_dis def is_specialized(f): for instruction in dis.get_instructions(f, adaptive=True): @@ -373,7 +373,7 @@ def is_specialized(f): assert not is_specialized(func), "specialized instructions found" - for i in range({ADAPTIVE_WARMUP_DELAY}): + for i in range(test.test_dis.ADAPTIVE_WARMUP_DELAY): func(importlib._bootstrap, ["x"], lambda *args: None) assert is_specialized(func), "no specialized instructions found"