[tor-commits] [stem/master] Clear orphaned pyc files for python3

atagar at torproject.org atagar at torproject.org
Mon Mar 2 17:25:12 UTC 2015


commit b8411f0dafab408f42f2b00ab6622fa64c6d120d
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Mar 2 09:23:07 2015 -0800

    Clear orphaned pyc files for python3
    
    Task to ensure we don't test against bytecode for deleted or renamed files.
    Initial patch thanks to dumindux.
    
      https://trac.torproject.org/projects/tor/ticket/14628
---
 stem/util/test_tools.py |   17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/stem/util/test_tools.py b/stem/util/test_tools.py
index 2e4dd0b..c8fe76a 100644
--- a/stem/util/test_tools.py
+++ b/stem/util/test_tools.py
@@ -53,17 +53,22 @@ def clean_orphaned_pyc(paths):
 
   for path in paths:
     for pyc_path in stem.util.system.files_with_suffix(path, '.pyc'):
+      py_path = pyc_path[:-1]
+
       # If we're running python 3 then the *.pyc files are no longer bundled
       # with the *.py. Rather, they're in a __pycache__ directory.
 
-      # TODO: At the moment there's no point in checking for orphaned bytecode
-      # with python 3 because it's an exported copy of the python 2 codebase,
-      # so skipping. However, we might want to address this for other callers.
+      pycache = '%s__pycache__%s' % (os.path.sep, os.path.sep)
+
+      if pycache in pyc_path:
+        directory, pycache_filename = pyc_path.split(pycache, 1)
+
+        if not pycache_filename.endswith('.pyc'):
+          continue  # should look like 'test_tools.cpython-32.pyc'
 
-      if '__pycache__' in pyc_path:
-        continue
+        py_path = os.path.join(directory, pycache_filename.split('.')[0] + '.py')
 
-      if not os.path.exists(pyc_path[:-1]):
+      if not os.path.exists(py_path):
         orphaned_pyc.append(pyc_path)
         os.remove(pyc_path)
 



More information about the tor-commits mailing list