[Python-checkins] cpython (merge 3.4 -> default): - Issue #22966: Fix __pycache__ pyc file name clobber when pyc_compile is

barry.warsaw python-checkins at python.org
Tue Dec 2 00:17:49 CET 2014


https://hg.python.org/cpython/rev/25113281d543
changeset: 93692:25113281d543
parent: 93688:a1aa0a4371eb
parent: 93691:3b3ba38d503a
user: Barry Warsaw <barry at python.org>
date: Mon Dec 01 17:52:43 2014 -0500
summary:
 - Issue #22966: Fix __pycache__ pyc file name clobber when pyc_compile is
 asked to compile a source file containing multiple dots in the source file
 name.
files:
 Lib/importlib/_bootstrap.py | 4 +-
 Lib/test/test_py_compile.py | 17 +
 Misc/NEWS | 4 +
 Python/importlib.h | 7013 +++++++++++-----------
 4 files changed, 3530 insertions(+), 3508 deletions(-)
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -454,11 +454,11 @@
 else:
 suffixes = OPTIMIZED_BYTECODE_SUFFIXES
 head, tail = _path_split(path)
- base_filename, sep, _ = tail.partition('.')
+ base, sep, rest = tail.rpartition('.')
 tag = sys.implementation.cache_tag
 if tag is None:
 raise NotImplementedError('sys.implementation.cache_tag is None')
- filename = ''.join([base_filename, sep, tag, suffixes[0]])
+ filename = ''.join([(base if base else rest), sep, tag, suffixes[0]])
 return _path_join(head, _PYCACHE, filename)
 
 
diff --git a/Lib/test/test_py_compile.py b/Lib/test/test_py_compile.py
--- a/Lib/test/test_py_compile.py
+++ b/Lib/test/test_py_compile.py
@@ -3,6 +3,7 @@
 import py_compile
 import shutil
 import stat
+import sys
 import tempfile
 import unittest
 
@@ -99,5 +100,21 @@
 self.assertFalse(os.path.exists(
 importlib.util.cache_from_source(bad_coding)))
 
+ def test_double_dot_no_clobber(self):
+ # http://bugs.python.org/issue22966
+ # py_compile foo.bar.py -> __pycache__/foo.cpython-34.pyc
+ weird_path = os.path.join(self.directory, 'foo.bar.py')
+ cache_path = importlib.util.cache_from_source(weird_path)
+ pyc_path = weird_path + 'c'
+ self.assertEqual(
+ '/'.join(cache_path.split('/')[-2:]),
+ '__pycache__/foo.bar.{}.pyc'.format(sys.implementation.cache_tag))
+ with open(weird_path, 'w') as file:
+ file.write('x = 123\n')
+ py_compile.compile(weird_path)
+ self.assertTrue(os.path.exists(cache_path))
+ self.assertFalse(os.path.exists(pyc_path))
+
+
 if __name__ == "__main__":
 unittest.main()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -191,6 +191,10 @@
 Library
 -------
 
+- Issue #22966: Fix __pycache__ pyc file name clobber when pyc_compile is
+ asked to compile a source file containing multiple dots in the source file
+ name.
+
 - Issue #21971: Update turtledemo doc and add module to the index.
 
 - Issue #21032. Fixed socket leak if HTTPConnection.getresponse() fails.
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /