Kaydet (Commit) 12b6457e authored tarafından Jeremy Hylton's avatar Jeremy Hylton

Fix compileall.py so that it fails on SyntaxErrors

The changes cause compilation failures in any file in the Python
installation lib directory to cause the install to fail.  It looks
like compileall.py intended to behave this way, but a change to
py_compile.py and a separate bug defeated it.

Fixes SF bug #412436

This change affects the test suite, which contains several files that
contain intentional errors.  The solution is to extend compileall.py
with the ability to skip compilation of selected files.

NB compileall.py is changed so that compile_dir() returns success only
if all recursive calls to compile_dir() also check success.
üst 30906940
...@@ -19,7 +19,7 @@ import py_compile ...@@ -19,7 +19,7 @@ import py_compile
__all__ = ["compile_dir","compile_path"] __all__ = ["compile_dir","compile_path"]
def compile_dir(dir, maxlevels=10, ddir=None, force=0): def compile_dir(dir, maxlevels=10, ddir=None, force=0, rx=None):
"""Byte-compile all modules in the given directory tree. """Byte-compile all modules in the given directory tree.
Arguments (only dir is required): Arguments (only dir is required):
...@@ -45,6 +45,10 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=0): ...@@ -45,6 +45,10 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=0):
dfile = os.path.join(ddir, name) dfile = os.path.join(ddir, name)
else: else:
dfile = None dfile = None
if rx:
mo = rx.search(fullname)
if mo:
continue
if os.path.isfile(fullname): if os.path.isfile(fullname):
head, tail = name[:-3], name[-3:] head, tail = name[:-3], name[-3:]
if tail == '.py': if tail == '.py':
...@@ -55,21 +59,26 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=0): ...@@ -55,21 +59,26 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=0):
if (ctime > ftime) and not force: continue if (ctime > ftime) and not force: continue
print 'Compiling', fullname, '...' print 'Compiling', fullname, '...'
try: try:
py_compile.compile(fullname, None, dfile) ok = py_compile.compile(fullname, None, dfile)
except KeyboardInterrupt: except KeyboardInterrupt:
raise KeyboardInterrupt raise KeyboardInterrupt
except: except:
# XXX py_compile catches SyntaxErrors
if type(sys.exc_type) == type(''): if type(sys.exc_type) == type(''):
exc_type_name = sys.exc_type exc_type_name = sys.exc_type
else: exc_type_name = sys.exc_type.__name__ else: exc_type_name = sys.exc_type.__name__
print 'Sorry:', exc_type_name + ':', print 'Sorry:', exc_type_name + ':',
print sys.exc_value print sys.exc_value
success = 0 success = 0
else:
if ok == 0:
success = 0
elif maxlevels > 0 and \ elif maxlevels > 0 and \
name != os.curdir and name != os.pardir and \ name != os.curdir and name != os.pardir and \
os.path.isdir(fullname) and \ os.path.isdir(fullname) and \
not os.path.islink(fullname): not os.path.islink(fullname):
compile_dir(fullname, maxlevels - 1, dfile, force) if not compile_dir(fullname, maxlevels - 1, dfile, force, rx):
success = 0
return success return success
def compile_path(skip_curdir=1, maxlevels=0, force=0): def compile_path(skip_curdir=1, maxlevels=0, force=0):
...@@ -94,22 +103,29 @@ def main(): ...@@ -94,22 +103,29 @@ def main():
"""Script main program.""" """Script main program."""
import getopt import getopt
try: try:
opts, args = getopt.getopt(sys.argv[1:], 'lfd:') opts, args = getopt.getopt(sys.argv[1:], 'lfd:x:')
except getopt.error, msg: except getopt.error, msg:
print msg print msg
print "usage: compileall [-l] [-f] [-d destdir] [directory ...]" print "usage: python compileall.py [-l] [-f] [-d destdir] " \
"[-s regexp] [directory ...]"
print "-l: don't recurse down" print "-l: don't recurse down"
print "-f: force rebuild even if timestamps are up-to-date" print "-f: force rebuild even if timestamps are up-to-date"
print "-d destdir: purported directory name for error messages" print "-d destdir: purported directory name for error messages"
print "if no directory arguments, -l sys.path is assumed" print " if no directory arguments, -l sys.path is assumed"
print "-x regexp: skip files matching the regular expression regexp"
print " the regexp is search for in the full path of the file"
sys.exit(2) sys.exit(2)
maxlevels = 10 maxlevels = 10
ddir = None ddir = None
force = 0 force = 0
rx = None
for o, a in opts: for o, a in opts:
if o == '-l': maxlevels = 0 if o == '-l': maxlevels = 0
if o == '-d': ddir = a if o == '-d': ddir = a
if o == '-f': force = 1 if o == '-f': force = 1
if o == '-x':
import re
rx = re.compile(a)
if ddir: if ddir:
if len(args) != 1: if len(args) != 1:
print "-d destdir require exactly one directory argument" print "-d destdir require exactly one directory argument"
...@@ -118,7 +134,8 @@ def main(): ...@@ -118,7 +134,8 @@ def main():
try: try:
if args: if args:
for dir in args: for dir in args:
success = success and compile_dir(dir, maxlevels, ddir, force) if not compile_dir(dir, maxlevels, ddir, force, rx):
success = 0
else: else:
success = compile_path() success = compile_path()
except KeyboardInterrupt: except KeyboardInterrupt:
...@@ -127,4 +144,5 @@ def main(): ...@@ -127,4 +144,5 @@ def main():
return success return success
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(not main()) exit_status = not main()
sys.exit(exit_status)
...@@ -616,9 +616,10 @@ libinstall: $(PYTHON) $(srcdir)/Lib/$(PLATDIR) ...@@ -616,9 +616,10 @@ libinstall: $(PYTHON) $(srcdir)/Lib/$(PLATDIR)
done done
$(INSTALL_DATA) $(srcdir)/LICENSE $(LIBDEST)/LICENSE.txt $(INSTALL_DATA) $(srcdir)/LICENSE $(LIBDEST)/LICENSE.txt
PYTHONPATH=$(LIBDEST) \ PYTHONPATH=$(LIBDEST) \
./$(PYTHON) -tt $(LIBDEST)/compileall.py $(LIBDEST) ./$(PYTHON) -tt $(LIBDEST)/compileall.py -x badsyntax \
$(LIBDEST)
PYTHONPATH=$(LIBDEST) \ PYTHONPATH=$(LIBDEST) \
./$(PYTHON) -O $(LIBDEST)/compileall.py $(LIBDEST) ./$(PYTHON) -O $(LIBDEST)/compileall.py -x badsyntax $(LIBDEST)
# Create the PLATDIR source directory, if one wasn't distributed.. # Create the PLATDIR source directory, if one wasn't distributed..
$(srcdir)/Lib/$(PLATDIR): $(srcdir)/Lib/$(PLATDIR):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment