Commit 2a413853 authored by Barry Warsaw's avatar Barry Warsaw

- 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.
parent b491e052
...@@ -453,11 +453,11 @@ def cache_from_source(path, debug_override=None): ...@@ -453,11 +453,11 @@ def cache_from_source(path, debug_override=None):
else: else:
suffixes = OPTIMIZED_BYTECODE_SUFFIXES suffixes = OPTIMIZED_BYTECODE_SUFFIXES
head, tail = _path_split(path) head, tail = _path_split(path)
base_filename, sep, _ = tail.partition('.') base, sep, rest = tail.rpartition('.')
tag = sys.implementation.cache_tag tag = sys.implementation.cache_tag
if tag is None: if tag is None:
raise NotImplementedError('sys.implementation.cache_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) return _path_join(head, _PYCACHE, filename)
......
...@@ -99,5 +99,21 @@ class PyCompileTests(unittest.TestCase): ...@@ -99,5 +99,21 @@ class PyCompileTests(unittest.TestCase):
self.assertFalse(os.path.exists( self.assertFalse(os.path.exists(
importlib.util.cache_from_source(bad_coding))) 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.cpython-34.pyc')
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__": if __name__ == "__main__":
unittest.main() unittest.main()
...@@ -36,6 +36,10 @@ Core and Builtins ...@@ -36,6 +36,10 @@ Core and Builtins
Library 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 #21032. Fixed socket leak if HTTPConnection.getresponse() fails. - Issue #21032. Fixed socket leak if HTTPConnection.getresponse() fails.
Original patch by Martin Panter. Original patch by Martin Panter.
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment