]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix compileall.py so that it fails on SyntaxErrors
authorJeremy Hylton <jeremy@alum.mit.edu>
Wed, 18 Apr 2001 01:20:21 +0000 (01:20 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Wed, 18 Apr 2001 01:20:21 +0000 (01:20 +0000)
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.

Lib/compileall.py
Makefile.pre.in

index 93ec7adc73d62c7673e83739d97b204e4295008a..cb4a3e94ac134b7962ce82d12e1c23ec93a2bfa8 100644 (file)
@@ -19,7 +19,7 @@ import py_compile
 
 __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.
 
     Arguments (only dir is required):
@@ -45,6 +45,10 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=0):
             dfile = os.path.join(ddir, name)
         else:
             dfile = None
+        if rx:
+            mo = rx.search(fullname)
+            if mo:
+                continue
         if os.path.isfile(fullname):
             head, tail = name[:-3], name[-3:]
             if tail == '.py':
@@ -55,21 +59,26 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=0):
                 if (ctime > ftime) and not force: continue
                 print 'Compiling', fullname, '...'
                 try:
-                    py_compile.compile(fullname, None, dfile)
+                    ok = py_compile.compile(fullname, None, dfile)
                 except KeyboardInterrupt:
                     raise KeyboardInterrupt
                 except:
+                    # XXX py_compile catches SyntaxErrors
                     if type(sys.exc_type) == type(''):
                         exc_type_name = sys.exc_type
                     else: exc_type_name = sys.exc_type.__name__
                     print 'Sorry:', exc_type_name + ':',
                     print sys.exc_value
                     success = 0
+                else:
+                    if ok == 0:
+                        success = 0
         elif maxlevels > 0 and \
              name != os.curdir and name != os.pardir and \
              os.path.isdir(fullname) and \
              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
 
 def compile_path(skip_curdir=1, maxlevels=0, force=0):
@@ -94,22 +103,29 @@ def main():
     """Script main program."""
     import getopt
     try:
-        opts, args = getopt.getopt(sys.argv[1:], 'lfd:')
+        opts, args = getopt.getopt(sys.argv[1:], 'lfd:x:')
     except getopt.error, 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 "-f: force rebuild even if timestamps are up-to-date"
         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)
     maxlevels = 10
     ddir = None
     force = 0
+    rx = None
     for o, a in opts:
         if o == '-l': maxlevels = 0
         if o == '-d': ddir = a
         if o == '-f': force = 1
+        if o == '-x':
+            import re
+            rx = re.compile(a)
     if ddir:
         if len(args) != 1:
             print "-d destdir require exactly one directory argument"
@@ -118,7 +134,8 @@ def main():
     try:
         if 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:
             success = compile_path()
     except KeyboardInterrupt:
@@ -127,4 +144,5 @@ def main():
     return success
 
 if __name__ == '__main__':
-    sys.exit(not main())
+    exit_status = not main()
+    sys.exit(exit_status)
index f81702b4509d9163181fff86f056a4c5be8b0149..f2f33362355d2803c9c44a1edf1e46840bf5d6a9 100644 (file)
@@ -616,9 +616,10 @@ libinstall:        $(PYTHON) $(srcdir)/Lib/$(PLATDIR)
        done
        $(INSTALL_DATA) $(srcdir)/LICENSE $(LIBDEST)/LICENSE.txt
        PYTHONPATH=$(LIBDEST) \
-               ./$(PYTHON) -tt $(LIBDEST)/compileall.py $(LIBDEST)
+               ./$(PYTHON) -tt $(LIBDEST)/compileall.py -x badsyntax \
+               $(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..
 $(srcdir)/Lib/$(PLATDIR):