Skip to content

Commit ccbf475

Browse files
author
Victor Stinner
committed
Fix imp.cache_from_source() if the directory name contains a dot
If the directory name contains a dot but not the filename, don't strip at the dot.
1 parent fe19d21 commit ccbf475

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

Lib/test/test_imp.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ def test_cache_from_source(self):
210210
self.assertEqual(
211211
imp.cache_from_source('/foo/bar/baz/qux.py', True),
212212
'/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag))
213+
# Directory with a dot, filename without dot
214+
self.assertEqual(
215+
imp.cache_from_source('/foo.bar/file', True),
216+
'/foo.bar/__pycache__/file{}.pyc'.format(self.tag))
213217

214218
def test_cache_from_source_optimized(self):
215219
# Given the path to a .py file, return the path to its PEP 3147

Python/import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,12 +943,12 @@ make_compiled_pathname(Py_UNICODE *pathname, int debug)
943943
Py_UNICODE_strcat(buf, CACHEDIR_UNICODE);
944944
i += Py_UNICODE_strlen(CACHEDIR_UNICODE) - 1;
945945
buf[i++] = sep;
946-
buf[i++] = '\0';
946+
buf[i] = '\0';
947947
/* Add the base filename, but remove the .py or .pyw extension, since
948948
the tag name must go before the extension.
949949
*/
950950
Py_UNICODE_strcat(buf, pathname + save);
951-
pos = Py_UNICODE_strrchr(buf, '.');
951+
pos = Py_UNICODE_strrchr(buf + i, '.');
952952
if (pos != NULL)
953953
*++pos = '\0';
954954
Py_UNICODE_strcat(buf, PYC_TAG_UNICODE);

0 commit comments

Comments
 (0)