summaryrefslogtreecommitdiff
path: root/Lib/py_compile.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2010-03-31 21:07:16 +0000
committerBarry Warsaw <barry@python.org>2010-03-31 21:07:16 +0000
commitf7f2d6f51e8ea429b9db44eed0dcc6a4e22708df (patch)
treeef4e34660c88bdc29d03af37775caf376dffdb65 /Lib/py_compile.py
parentfe4900c0bc477479b24c5bb374984d6a873a3db7 (diff)
downloadcpython-git-f7f2d6f51e8ea429b9db44eed0dcc6a4e22708df.tar.gz
- 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).
Diffstat (limited to 'Lib/py_compile.py')
-rw-r--r--Lib/py_compile.py32
1 files changed, 24 insertions, 8 deletions
diff --git a/Lib/py_compile.py b/Lib/py_compile.py
index 24b58374c0..f88cfbb5af 100644
--- a/Lib/py_compile.py
+++ b/Lib/py_compile.py
@@ -135,19 +135,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, 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__":