Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12b6457

Browse files
committed
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.
1 parent 3090694 commit 12b6457

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

Lib/compileall.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
__all__ = ["compile_dir","compile_path"]
2121

22-
def compile_dir(dir, maxlevels=10, ddir=None, force=0):
22+
def compile_dir(dir, maxlevels=10, ddir=None, force=0, rx=None):
2323
"""Byte-compile all modules in the given directory tree.
2424
2525
Arguments (only dir is required):
@@ -45,6 +45,10 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=0):
4545
dfile = os.path.join(ddir, name)
4646
else:
4747
dfile = None
48+
if rx:
49+
mo = rx.search(fullname)
50+
if mo:
51+
continue
4852
if os.path.isfile(fullname):
4953
head, tail = name[:-3], name[-3:]
5054
if tail == '.py':
@@ -55,21 +59,26 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=0):
5559
if (ctime > ftime) and not force: continue
5660
print 'Compiling', fullname, '...'
5761
try:
58-
py_compile.compile(fullname, None, dfile)
62+
ok = py_compile.compile(fullname, None, dfile)
5963
except KeyboardInterrupt:
6064
raise KeyboardInterrupt
6165
except:
66+
# XXX py_compile catches SyntaxErrors
6267
if type(sys.exc_type) == type(''):
6368
exc_type_name = sys.exc_type
6469
else: exc_type_name = sys.exc_type.__name__
6570
print 'Sorry:', exc_type_name + ':',
6671
print sys.exc_value
6772
success = 0
73+
else:
74+
if ok == 0:
75+
success = 0
6876
elif maxlevels > 0 and \
6977
name != os.curdir and name != os.pardir and \
7078
os.path.isdir(fullname) and \
7179
not os.path.islink(fullname):
72-
compile_dir(fullname, maxlevels - 1, dfile, force)
80+
if not compile_dir(fullname, maxlevels - 1, dfile, force, rx):
81+
success = 0
7382
return success
7483

7584
def compile_path(skip_curdir=1, maxlevels=0, force=0):
@@ -94,22 +103,29 @@ def main():
94103
"""Script main program."""
95104
import getopt
96105
try:
97-
opts, args = getopt.getopt(sys.argv[1:], 'lfd:')
106+
opts, args = getopt.getopt(sys.argv[1:], 'lfd:x:')
98107
except getopt.error, msg:
99108
print msg
100-
print "usage: compileall [-l] [-f] [-d destdir] [directory ...]"
109+
print "usage: python compileall.py [-l] [-f] [-d destdir] " \
110+
"[-s regexp] [directory ...]"
101111
print "-l: don't recurse down"
102112
print "-f: force rebuild even if timestamps are up-to-date"
103113
print "-d destdir: purported directory name for error messages"
104-
print "if no directory arguments, -l sys.path is assumed"
114+
print " if no directory arguments, -l sys.path is assumed"
115+
print "-x regexp: skip files matching the regular expression regexp"
116+
print " the regexp is search for in the full path of the file"
105117
sys.exit(2)
106118
maxlevels = 10
107119
ddir = None
108120
force = 0
121+
rx = None
109122
for o, a in opts:
110123
if o == '-l': maxlevels = 0
111124
if o == '-d': ddir = a
112125
if o == '-f': force = 1
126+
if o == '-x':
127+
import re
128+
rx = re.compile(a)
113129
if ddir:
114130
if len(args) != 1:
115131
print "-d destdir require exactly one directory argument"
@@ -118,7 +134,8 @@ def main():
118134
try:
119135
if args:
120136
for dir in args:
121-
success = success and compile_dir(dir, maxlevels, ddir, force)
137+
if not compile_dir(dir, maxlevels, ddir, force, rx):
138+
success = 0
122139
else:
123140
success = compile_path()
124141
except KeyboardInterrupt:
@@ -127,4 +144,5 @@ def main():
127144
return success
128145

129146
if __name__ == '__main__':
130-
sys.exit(not main())
147+
exit_status = not main()
148+
sys.exit(exit_status)

Makefile.pre.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,10 @@ libinstall: $(PYTHON) $(srcdir)/Lib/$(PLATDIR)
616616
done
617617
$(INSTALL_DATA) $(srcdir)/LICENSE $(LIBDEST)/LICENSE.txt
618618
PYTHONPATH=$(LIBDEST) \
619-
./$(PYTHON) -tt $(LIBDEST)/compileall.py $(LIBDEST)
619+
./$(PYTHON) -tt $(LIBDEST)/compileall.py -x badsyntax \
620+
$(LIBDEST)
620621
PYTHONPATH=$(LIBDEST) \
621-
./$(PYTHON) -O $(LIBDEST)/compileall.py $(LIBDEST)
622+
./$(PYTHON) -O $(LIBDEST)/compileall.py -x badsyntax $(LIBDEST)
622623

623624
# Create the PLATDIR source directory, if one wasn't distributed..
624625
$(srcdir)/Lib/$(PLATDIR):

0 commit comments

Comments
 (0)