]> granicus.if.org Git - python/commitdiff
- Issue #8233: When run as a script, py_compile.py optionally takes a single
authorBarry Warsaw <barry@python.org>
Wed, 31 Mar 2010 21:36:22 +0000 (21:36 +0000)
committerBarry Warsaw <barry@python.org>
Wed, 31 Mar 2010 21:36:22 +0000 (21:36 +0000)
  argument `-` which tells it to read files to compile from stdin.  Each line
  is read on demand and the named file is compiled immediately.  (Original
  patch by Piotr Ożarowski).

Lib/py_compile.py
Misc/NEWS

index 9f34a0b38ed30f6849a09a26e09a37c701eab586..936187584631b1b86469ecd24e8944b00120d4ac 100644 (file)
@@ -138,19 +138,35 @@ def main(args=None):
     not specified) are compiled and the resulting bytecode is cached
     in the normal manner.  This function does not search a directory
     structure to locate source files; it only compiles files named
-    explicitly.
+    explicitly.  If '-' is the only parameter in args, the list of
+    files is taken from standard input.
 
     """
     if args is None:
         args = sys.argv[1:]
     rv = 0
-    for filename in args:
-        try:
-            compile(filename, doraise=True)
-        except PyCompileError as err:
-            # return value to indicate at least one failure
-            rv = 1
-            sys.stderr.write(err.msg)
+    if args == ['-']:
+        while True:
+            filename = sys.stdin.readline()
+            if not filename:
+                break
+            filename = filename.rstrip('\n')
+            try:
+                compile(filename, doraise=True)
+            except PyCompileError as error:
+                rv = 1
+                sys.stderr.write("%s\n" % error.msg)
+            except IOError as error:
+                rv = 1
+                sys.stderr.write("%s\n" % error)
+    else:
+        for filename in args:
+            try:
+                compile(filename, doraise=True)
+            except PyCompileError as err:
+                # return value to indicate at least one failure
+                rv = 1
+                sys.stderr.write(error.msg)
     return rv
 
 if __name__ == "__main__":
index e6bc50b2c194870043f44d50198872c8b19a9e62..6b645c8889f48ac43e0d095a9c77b5c994d6c35d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -293,6 +293,11 @@ C-API
 Library
 -------
 
+- Issue #8233: When run as a script, py_compile.py optionally takes a single
+  argument `-` which tells it to read files to compile from stdin.  Each line
+  is read on demand and the named file is compiled immediately.  (Original
+  patch by Piotr Ożarowski).
+
 - Backwards incompatible change: Unicode codepoints line tabulation (0x0B) and
   form feed (0x0C) are now considered linebreaks, as specified in Unicode
   Standard Annex #14.  See issue #7643.