Skip to content

Commit 5c3b0d4

Browse files
Commit
1 parent e95e783 commit 5c3b0d4

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,19 @@ def set_data(self, path, data, *, _mode=0o666):
999999
_bootstrap._verbose_message('could not create {!r}: {!r}',
10001000
parent, exc)
10011001
return
1002+
1003+
if part == _PYCACHE:
1004+
gitignore = _path_join(parent, '.gitignore')
1005+
try:
1006+
_path_stat(gitignore)
1007+
except FileNotFoundError:
1008+
gitignore_content = b'# Created by CPython\n*\n'
1009+
try:
1010+
_write_atomic(gitignore, gitignore_content, _mode)
1011+
except OSError:
1012+
pass
1013+
except OSError:
1014+
pass
10021015
try:
10031016
_write_atomic(path, data, _mode)
10041017
_bootstrap._verbose_message('created {!r}', path)

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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When ``__pycache__`` directories are created, they now contain a
2+
``.gitignore`` file that ignores their contents.

0 commit comments

Comments
 (0)