[Python-checkins] r83887 - in python/branches/py3k: Misc/NEWS Tools/scripts/checkpyc.py

florent.xicluna python-checkins at python.org
Mon Aug 9 14:26:44 CEST 2010


Author: florent.xicluna
Date: Mon Aug  9 14:26:44 2010
New Revision: 83887

Log:
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.

Modified:
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Tools/scripts/checkpyc.py

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Mon Aug  9 14:26:44 2010
@@ -150,6 +150,8 @@
 Tools/Demos
 -----------
 
+- Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
+
 - Issue #8867: Fix ``Tools/scripts/serve.py`` to work with files containing
   non-ASCII content.
 

Modified: python/branches/py3k/Tools/scripts/checkpyc.py
==============================================================================
--- python/branches/py3k/Tools/scripts/checkpyc.py	(original)
+++ python/branches/py3k/Tools/scripts/checkpyc.py	Mon Aug  9 14:26:44 2010
@@ -7,14 +7,17 @@
 from stat import ST_MTIME
 import imp
 
+# PEP 3147 compatibility (PYC Repository Directories)
+cache_from_source = (imp.cache_from_source if hasattr(imp, 'get_tag') else
+                     lambda path: path + 'c')
+
+
 def main():
-    silent = 0
-    verbose = 0
-    if sys.argv[1:]:
-        if sys.argv[1] == '-v':
-            verbose = 1
-        elif sys.argv[1] == '-s':
-            silent = 1
+    if len(sys.argv) > 1:
+        verbose = (sys.argv[1] == '-v')
+        silent = (sys.argv[1] == '-s')
+    else:
+        verbose = silent = False
     MAGIC = imp.get_magic()
     if not silent:
         print('Using MAGIC word', repr(MAGIC))
@@ -26,9 +29,8 @@
             continue
         if not silent:
             print('Checking ', repr(dirname), '...')
-        names.sort()
-        for name in names:
-            if name[-3:] == '.py':
+        for name in sorted(names):
+            if name.endswith('.py'):
                 name = os.path.join(dirname, name)
                 try:
                     st = os.stat(name)
@@ -37,30 +39,31 @@
                     continue
                 if verbose:
                     print('Check', repr(name), '...')
-                name_c = name + 'c'
+                name_c = cache_from_source(name)
                 try:
-                    f = open(name_c, 'r')
+                    with open(name_c, 'rb') as f:
+                        magic_str = f.read(4)
+                        mtime_str = f.read(4)
                 except IOError:
                     print('Cannot open', repr(name_c))
                     continue
-                magic_str = f.read(4)
-                mtime_str = f.read(4)
-                f.close()
                 if magic_str != MAGIC:
                     print('Bad MAGIC word in ".pyc" file', end=' ')
                     print(repr(name_c))
                     continue
                 mtime = get_long(mtime_str)
-                if mtime == 0 or mtime == -1:
+                if mtime in {0, -1}:
                     print('Bad ".pyc" file', repr(name_c))
                 elif mtime != st[ST_MTIME]:
                     print('Out-of-date ".pyc" file', end=' ')
                     print(repr(name_c))
 
+
 def get_long(s):
     if len(s) != 4:
         return -1
-    return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
+    return s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24)
+
 
 if __name__ == '__main__':
     main()


More information about the Python-checkins mailing list