Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,19 @@ def set_data(self, path, data, *, _mode=0o666):
_bootstrap._verbose_message('could not create {!r}: {!r}',
parent, exc)
return

if part == _PYCACHE:
gitignore = _path_join(parent, '.gitignore')
try:
_path_stat(gitignore)
except FileNotFoundError:
gitignore_content = b'# Created by CPython\n*\n'
try:
_write_atomic(gitignore, gitignore_content, _mode)
except OSError:
pass
except OSError:
pass
try:
_write_atomic(path, data, _mode)
_bootstrap._verbose_message('created {!r}', path)
Expand Down
8 changes: 8 additions & 0 deletions Lib/py_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1,
dirname = os.path.dirname(cfile)
if dirname:
os.makedirs(dirname)
if os.path.basename(dirname) == '__pycache__':
gitignore = os.path.join(dirname, '.gitignore')
if not os.path.exists(gitignore):
try:
with open(gitignore, 'wb') as f:
f.write(b'# Created by CPython\n*\n')
except OSError:
pass
except FileExistsError:
pass
if invalidation_mode == PycInvalidationMode.TIMESTAMP:
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@ def f(self, ext=ext, switch=switch):
['-m', 'compileall', '-q', self.pkgdir]))
# Verify the __pycache__ directory contents.
self.assertTrue(os.path.exists(self.pkgdir_cachedir))
expected = sorted(base.format(sys.implementation.cache_tag, ext)
expected = ['.gitignore'] + \
sorted(base.format(sys.implementation.cache_tag, ext)
for base in ('__init__.{}.{}', 'bar.{}.{}'))
self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected)
# Make sure there are no .pyc files in the source directory.
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_importlib/source/test_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,21 @@ def test_overridden_unchecked_hash_based_pyc(self):
data[8:16],
)

@util.writes_bytecode_files
def test_gitignore_in_pycache(self):
with util.create_modules('_temp') as mapping:
source = mapping['_temp']
loader = self.machinery.SourceFileLoader('_temp', source)
mod = types.ModuleType('_temp')
mod.__spec__ = self.util.spec_from_loader('_temp', loader)
loader.exec_module(mod)
pyc = os.path.dirname(self.util.cache_from_source(source))
gitignore = os.path.join(pyc, '.gitignore')
self.assertTrue(os.path.exists(gitignore))
with open(gitignore, 'rb') as f:
t = f.read()
self.assertEqual(t, b'# Created by CPython\n*\n')


(Frozen_SimpleTest,
Source_SimpleTest
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_py_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ def test_quiet(self):
with self.assertRaises(py_compile.PyCompileError):
py_compile.compile(bad_coding, doraise=True, quiet=1)

def test_gitignore_created(self):
py_compile.compile(self.source_path)
self.assertTrue(os.path.exists(self.cache_path))
pyc = os.path.dirname(self.cache_path)
gitignore = os.path.join(pyc, '.gitignore')
self.assertTrue(os.path.exists(gitignore))
with open(gitignore, 'rb') as f:
text = f.read()
self.assertEqual(text, b'# Created by CPython\n*\n')


class PyCompileTestsWithSourceEpoch(PyCompileTestsBase,
unittest.TestCase,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
When ``__pycache__`` directories are created, they now contain a
``.gitignore`` file that ignores their contents.
Loading