When trying to decide whether or not a given file needs to be recompiled, the magic number and timestamp stored with the file should be checked for equalty. This resmbles the way python internally identifies a need for recompilation. This helps to avoid problems when files get unpacked from some kind of archive so that their mtimes are probably older than the mtimes of compiled files already in place, even though the compiled files are outdated. Downgrades are handled gracefully by this method as well. References: http://bugs.python.org/issue5128 http://bugs.gentoo.org/256953 2009-02-02 Martin von Gagern Index: Lib/compileall.py =================================================================== --- Lib/compileall.py (revision 69204) +++ Lib/compileall.py (working copy) @@ -15,6 +15,8 @@ import os import sys import py_compile +import struct +import imp __all__ = ["compile_dir","compile_path"] @@ -54,11 +56,18 @@ if os.path.isfile(fullname): head, tail = name[:-3], name[-3:] if tail == '.py': - cfile = fullname + (__debug__ and 'c' or 'o') - ftime = os.stat(fullname).st_mtime - try: ctime = os.stat(cfile).st_mtime - except os.error: ctime = 0 - if (ctime > ftime) and not force: continue + if not force: + try: + mtime = os.stat(fullname).st_mtime + expect = struct.pack('<4sl', imp.get_magic(), mtime) + cfile = fullname + (__debug__ and 'c' or 'o') + chandle = open(cfile, 'rb') + actual = chandle.read(8) + chandle.close() + if expect == actual: + continue + except IOError: + pass if not quiet: print 'Compiling', fullname, '...' try: