Skip to content

Commit fddf365

Browse files
author
Johannes Holmberg
committed
gh-132933: Allow zipapp target to overwrite filtered source file
1 parent 1a70f66 commit fddf365

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Lib/test/test_zipapp.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ def test_target_overwrites_source_file(self):
113113
with self.assertRaises(zipapp.ZipAppError):
114114
zipapp.create_archive(source, target)
115115

116+
def test_target_overwrites_filtered_source_file(self):
117+
# The target cannot be one of the files to add.
118+
source = self.tmpdir
119+
(source / '__main__.py').touch()
120+
target = source / 'target.pyz'
121+
target.touch()
122+
pyz_filter = lambda p: not p.match('*.pyz')
123+
zipapp.create_archive(source, target, filter=pyz_filter)
124+
with zipfile.ZipFile(target, 'r') as z:
125+
self.assertEqual(len(z.namelist()), 1)
126+
self.assertIn('__main__.py', z.namelist())
127+
116128
def test_create_archive_filter_exclude_dir(self):
117129
# Test packing a directory and using a filter to exclude a
118130
# subdirectory (ensures that the path supplied to include

Lib/zipapp.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ def create_archive(source, target=None, interpreter=None, main=None,
151151
# equal to any of the entries in files_to_add, so there's no need
152152
# to add a special check for that.
153153
if target in files_to_add:
154-
raise ZipAppError(
155-
f"The target archive {target} overwrites one of the source files.")
154+
if filter:
155+
arcname = target.relative_to(source)
156+
not_filtered = filter(arcname)
157+
if not filter or not_filtered:
158+
raise ZipAppError(f"The target archive {target} overwrites "
159+
"one of the source files.")
156160

157161
with _maybe_open(target, 'wb') as fd:
158162
_write_file_prefix(fd, interpreter)

0 commit comments

Comments
 (0)