Skip to content
Merged
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
9 changes: 8 additions & 1 deletion Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0,
if root_dir is not None:
base_dir = os.path.join(root_dir, base_dir)
base_dir = os.path.normpath(base_dir)
zip_stat = os.stat(zip_filename)
if arcname != os.curdir:
zf.write(base_dir, arcname)
if logger is not None:
Expand All @@ -1119,7 +1120,13 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0,
for name in filenames:
path = os.path.join(dirpath, name)
path = os.path.normpath(path)
if os.path.isfile(path):
try:
st = os.stat(path)
except OSError:
continue
# Do not add the archive to itself.
if (stat.S_ISREG(st.st_mode) and
not os.path.samestat(st, zip_stat)):
arcname = os.path.join(arcdirpath, name)
zf.write(path, arcname)
if logger is not None:
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,21 @@ def test_make_zipfile_in_curdir(self):
# root_dir must be None, so the archive path is relative.
self.assertEqual(make_archive('test', 'zip'), 'test.zip')
self.assertTrue(os.path.isfile('test.zip'))
# The archive does not contain itself.
with zipfile.ZipFile('test.zip') as zf:
self.assertEqual(zf.namelist(), [])

@support.requires_zlib()
@os_helper.skip_unless_symlink
def test_make_zipfile_symlink_to_archive(self):
root_dir, base_dir = self._create_files('')
os.symlink('archive.zip', os.path.join(root_dir, 'link'))
base_name = os.path.join(root_dir, 'archive')
with no_chdir:
archive = make_archive(base_name, 'zip', root_dir)
with zipfile.ZipFile(archive) as zf:
self.assertCountEqual(zf.namelist(),
['sub/', 'sub2/', 'file1', 'file2', 'sub/file3'])

def test_make_archive_pathlike_cwd_default(self):
called_args = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :func:`shutil.make_archive` adding the zip archive being created to itself
when it is written inside the directory being archived or a symbolic link in
that directory refers to it.
Loading