Skip to content

Commit

Permalink
pythongh-127637: add tests for dis command-line interface (python#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
picnixz committed Dec 10, 2024
1 parent 8b4e7f8 commit 0b929a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 16 additions & 1 deletion Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add tests for the :mod:`dis` command-line interface. Patch by Bénédikt Tran.

0 comments on commit 0b929a7

Please sign in to comment.