Skip to content

Commit 0b929a7

Browse files
committed
gh-127637: add tests for dis command-line interface (#127759)
1 parent 8b4e7f8 commit 0b929a7

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Lib/dis.py

Lines changed: 2 additions & 2 deletions
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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import io
66
import re
77
import sys
8+
import tempfile
89
import types
910
import unittest
1011
from test.support import (captured_stdout, requires_debug_ranges,
11-
requires_specialization, cpython_only)
12+
requires_specialization, cpython_only,
13+
os_helper)
1214
from test.support.bytecode_helper import BytecodeTestCase
1315

1416
import opcode
@@ -2069,5 +2071,18 @@ def get_disassembly(self, tb):
20692071
return output.getvalue()
20702072

20712073

2074+
class TestDisCLI(unittest.TestCase):
2075+
2076+
def setUp(self):
2077+
self.filename = tempfile.mktemp()
2078+
self.addCleanup(os_helper.unlink, self.filename)
2079+
2080+
def test_invocation(self):
2081+
with self.assertRaises(SystemExit):
2082+
# suppress argparse error message
2083+
with contextlib.redirect_stderr(io.StringIO()):
2084+
dis.main(args=['--unknown', self.filename])
2085+
2086+
20722087
if __name__ == "__main__":
20732088
unittest.main()
Lines changed: 1 addition & 0 deletions
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)