]> granicus.if.org Git - python/commitdiff
Bug 8563 - compileall.compile_file() creates empty __pycache__ directories in
authorBarry Warsaw <barry@python.org>
Thu, 29 Apr 2010 18:43:10 +0000 (18:43 +0000)
committerBarry Warsaw <barry@python.org>
Thu, 29 Apr 2010 18:43:10 +0000 (18:43 +0000)
data directories where there is no source.

Fix by: Arfrever Frehtes Taifersar Arahesis (Arfrever)
Test by: Barry

Lib/compileall.py
Lib/test/test_compileall.py

index be9e2ad05f0b6e7f69159c8c5c8d37b6a90e4c3b..ea2ee7fbb6c48e6e3d657ab255d31be28609d55b 100644 (file)
@@ -91,13 +91,14 @@ def compile_file(fullname, ddir=None, force=0, rx=None, quiet=False,
         else:
             cfile = imp.cache_from_source(fullname)
             cache_dir = os.path.dirname(cfile)
-            try:
-                os.mkdir(cache_dir)
-            except OSError as error:
-                if error.errno != errno.EEXIST:
-                    raise
         head, tail = name[:-3], name[-3:]
         if tail == '.py':
+            if not legacy:
+                try:
+                    os.mkdir(cache_dir)
+                except OSError as error:
+                    if error.errno != errno.EEXIST:
+                        raise
             if not force:
                 try:
                     mtime = int(os.stat(fullname).st_mtime)
index fe26026bd7717e2762e564b96f670072af608f5a..4ad0061f4316cec8664377e0291e3ea8907d9c3a 100644 (file)
@@ -75,6 +75,18 @@ class CompileallTests(unittest.TestCase):
         os.unlink(self.bc_path)
         os.unlink(self.bc_path2)
 
+    def test_no_pycache_in_non_package(self):
+        # Bug 8563 reported that __pycache__ directories got created by
+        # compile_file() for non-.py files.
+        data_dir = os.path.join(self.directory, 'data')
+        data_file = os.path.join(data_dir, 'file')
+        os.mkdir(data_dir)
+        # touch data/file
+        with open(data_file, 'w'):
+            pass
+        compileall.compile_file(data_file)
+        self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__')))
+
 
 class EncodingTest(unittest.TestCase):
     """Issue 6716: compileall should escape source code when printing errors
@@ -98,6 +110,7 @@ class EncodingTest(unittest.TestCase):
         finally:
             sys.stdout = orig_stdout
 
+
 class CommandLineTests(unittest.TestCase):
     """Test some aspects of compileall's CLI."""