Skip to content

Commit fbbef60

Browse files
authored
[3.12] gh-127637: add tests for dis command-line interface (#127759) (#127780)
1 parent 405f6d7 commit fbbef60

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

Lib/dis.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -790,12 +790,12 @@ def dis(self):
790790
return output.getvalue()
791791

792792

793-
def main():
793+
def main(args=None):
794794
import argparse
795795

796796
parser = argparse.ArgumentParser()
797797
parser.add_argument('infile', type=argparse.FileType('rb'), nargs='?', default='-')
798-
args = parser.parse_args()
798+
args = parser.parse_args(args=args)
799799
with args.infile as infile:
800800
source = infile.read()
801801
code = compile(source, args.infile.name, "exec")

Lib/test/test_dis.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
import contextlib
44
import dis
55
import io
6+
import opcode
67
import re
78
import sys
9+
import tempfile
810
import types
911
import unittest
1012
from test.support import (captured_stdout, requires_debug_ranges,
11-
requires_specialization, cpython_only)
13+
requires_specialization, cpython_only,
14+
os_helper)
1215
from test.support.bytecode_helper import BytecodeTestCase
1316

14-
import opcode
15-
1617

1718
def get_tb():
1819
def _error():
@@ -2069,5 +2070,18 @@ def get_disassembly(self, tb):
20692070
return output.getvalue()
20702071

20712072

2073+
class TestDisCLI(unittest.TestCase):
2074+
2075+
def setUp(self):
2076+
self.filename = tempfile.mktemp()
2077+
self.addCleanup(os_helper.unlink, self.filename)
2078+
2079+
def test_invocation(self):
2080+
with self.assertRaises(SystemExit):
2081+
# suppress argparse error message
2082+
with contextlib.redirect_stderr(io.StringIO()):
2083+
dis.main(args=['--unknown', self.filename])
2084+
2085+
20722086
if __name__ == "__main__":
20732087
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tests for the :mod:`dis` command-line interface. Patch by Bénédikt Tran.

0 commit comments

Comments
 (0)