Merge 5c3b0d478827a7397cc4264d6ce2d3ad0ba2502e into ed73c909f278a1eb5… · python/cpython@1457554 · GitHub
Skip to content

Commit 1457554

Browse files
Merge 5c3b0d4 into ed73c90
2 parents ed73c90 + 5c3b0d4 commit 1457554

6 files changed

Lines changed: 50 additions & 1 deletion

File tree

Lib/importlib/_bootstrap_external.py

Lines changed: 13 additions & 0 deletions

Lib/py_compile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1,
155155
dirname = os.path.dirname(cfile)
156156
if dirname:
157157
os.makedirs(dirname)
158+
if os.path.basename(dirname) == '__pycache__':
159+
gitignore = os.path.join(dirname, '.gitignore')
160+
if not os.path.exists(gitignore):
161+
try:
162+
with open(gitignore, 'wb') as f:
163+
f.write(b'# Created by CPython\n*\n')
164+
except OSError:
165+
pass
158166
except FileExistsError:
159167
pass
160168
if invalidation_mode == PycInvalidationMode.TIMESTAMP:

Lib/test/test_compileall.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ def f(self, ext=ext, switch=switch):
625625
['-m', 'compileall', '-q', self.pkgdir]))
626626
# Verify the __pycache__ directory contents.
627627
self.assertTrue(os.path.exists(self.pkgdir_cachedir))
628-
expected = sorted(base.format(sys.implementation.cache_tag, ext)
628+
expected = ['.gitignore'] + \
629+
sorted(base.format(sys.implementation.cache_tag, ext)
629630
for base in ('__init__.{}.{}', 'bar.{}.{}'))
630631
self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected)
631632
# Make sure there are no .pyc files in the source directory.

Lib/test/test_importlib/source/test_file_loader.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,21 @@ def test_overridden_unchecked_hash_based_pyc(self):
353353
data[8:16],
354354
)
355355

356+
@util.writes_bytecode_files
357+
def test_gitignore_in_pycache(self):
358+
with util.create_modules('_temp') as mapping:
359+
source = mapping['_temp']
360+
loader = self.machinery.SourceFileLoader('_temp', source)
361+
mod = types.ModuleType('_temp')
362+
mod.__spec__ = self.util.spec_from_loader('_temp', loader)
363+
loader.exec_module(mod)
364+
pyc = os.path.dirname(self.util.cache_from_source(source))
365+
gitignore = os.path.join(pyc, '.gitignore')
366+
self.assertTrue(os.path.exists(gitignore))
367+
with open(gitignore, 'rb') as f:
368+
t = f.read()
369+
self.assertEqual(t, b'# Created by CPython\n*\n')
370+
356371

357372
(Frozen_SimpleTest,
358373
Source_SimpleTest

Lib/test/test_py_compile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ def test_quiet(self):
207207
with self.assertRaises(py_compile.PyCompileError):
208208
py_compile.compile(bad_coding, doraise=True, quiet=1)
209209

210+
def test_gitignore_created(self):
211+
py_compile.compile(self.source_path)
212+
self.assertTrue(os.path.exists(self.cache_path))
213+
pyc = os.path.dirname(self.cache_path)
214+
gitignore = os.path.join(pyc, '.gitignore')
215+
self.assertTrue(os.path.exists(gitignore))
216+
with open(gitignore, 'rb') as f:
217+
text = f.read()
218+
self.assertEqual(text, b'# Created by CPython\n*\n')
219+
210220

211221
class PyCompileTestsWithSourceEpoch(PyCompileTestsBase,
212222
unittest.TestCase,
Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)