[Checkins] SVN: zope.testrunner/trunk/src/zope/testrunner/find.py Fix stale bytecode cleanup logic.

Marius Gedminas cvs-admin at zope.org
Fri Feb 8 13:37:21 UTC 2013


Log message for revision 129225:
  Fix stale bytecode cleanup logic.
  
  The problem:
  
    pyfile = file[:-14] + '.py'
  
  works nicely when file is something like 'foo.cpython-33.pyc', but fails
  miserably when file is 'foo.pypy-19.pyc'.  Result: all pyc files are
  cleaned, even when they're not stale.
  
  Actually, this whole __pycache__ logic is unnecessary: PEP-3147 clearly
  states that sourceless pyc files are imported only when they appear in
  the regular source directory.  Experiments confirm that.  This leads us
  to...
  
  The solution:
  
    do not recurse into __pycache__ when cleaning stale bytecode files.

Changed:
  U   zope.testrunner/trunk/src/zope/testrunner/find.py

-=-
Modified: zope.testrunner/trunk/src/zope/testrunner/find.py
===================================================================
--- zope.testrunner/trunk/src/zope/testrunner/find.py	2013-02-08 13:20:45 UTC (rev 129224)
+++ zope.testrunner/trunk/src/zope/testrunner/find.py	2013-02-08 13:37:21 UTC (rev 129225)
@@ -353,18 +353,12 @@
     for (p, _) in options.test_path:
         for dirname, dirs, files in walk_with_symlinks(options, p):
             if '__pycache__' in dirs:
-                cached_files = os.listdir(os.path.join(dirname, '__pycache__'))
-                for file in cached_files:
-                    pyfile = file[:-14] + 'py'
-                    if pyfile not in files:
-                        fullname = os.path.join(dirname, '__pycache__', file)
-                        options.output.info("Removing stale bytecode file %s"
-                                            % fullname)
-                        os.unlink(fullname)
-            
-            if '__pycache__' in dirname:
-                # Already cleaned
-                continue
+                # Do not recurse in there: we would end up removing all pyc
+                # files because the main loop checks for py files in the same
+                # directory.  Besides, stale pyc files in __pycache__ are
+                # harmless, see PEP-3147 for details (sourceless imports
+                # work only when pyc lives in the source dir directly).
+                dirs.remove('__pycache__')
 
             for file in files:
                 if file[-4:] in compiled_suffixes and file[:-1] not in files:



More information about the checkins mailing list