[Python-checkins] cpython: Close #18538: ``python -m dis`` now uses argparse.

nick.coghlan python-checkins at python.org
Sat Aug 24 16:48:32 CEST 2013


http://hg.python.org/cpython/rev/59f98b96607e
changeset:   85362:59f98b96607e
user:        Nick Coghlan <ncoghlan at gmail.com>
date:        Sun Aug 25 00:48:17 2013 +1000
summary:
  Close #18538: ``python -m dis`` now uses argparse.

Patch by Michele Orrù.

files:
  Lib/dis.py |  27 ++++++++-------------------
  Misc/NEWS  |   3 +++
  2 files changed, 11 insertions(+), 19 deletions(-)


diff --git a/Lib/dis.py b/Lib/dis.py
--- a/Lib/dis.py
+++ b/Lib/dis.py
@@ -436,25 +436,14 @@
 
 def _test():
     """Simple test program to disassemble a file."""
-    if sys.argv[1:]:
-        if sys.argv[2:]:
-            sys.stderr.write("usage: python dis.py [-|file]\n")
-            sys.exit(2)
-        fn = sys.argv[1]
-        if not fn or fn == "-":
-            fn = None
-    else:
-        fn = None
-    if fn is None:
-        f = sys.stdin
-    else:
-        f = open(fn)
-    source = f.read()
-    if fn is not None:
-        f.close()
-    else:
-        fn = "<stdin>"
-    code = compile(source, fn, "exec")
+    import argparse
+
+    parser = argparse.ArgumentParser()
+    parser.add_argument('infile', type=argparse.FileType(), nargs='?', default='-')
+    args = parser.parse_args()
+    with args.infile as infile:
+        source = infile.read()
+    code = compile(source, args.infile.name, "exec")
     dis(code)
 
 if __name__ == "__main__":
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -40,6 +40,9 @@
 Library
 -------
 
+- Issue #18538: ``python -m dis`` now uses argparse for argument processing.
+  Patch by Michele Orrù.
+
 - Issue #18394: Close cgi.FieldStorage's optional file.
 
 - Issue #17702: On error, os.environb now removes suppress the except context

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list