Skip to content

gh-132933: Allow zipapp target to overwrite filtered source file #132934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions Lib/test/test_zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ def test_target_overwrites_source_file(self):
with self.assertRaises(zipapp.ZipAppError):
zipapp.create_archive(source, target)

def test_target_overwrites_filtered_source_file(self):
# The target cannot be one of the files to add.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# The target cannot be one of the files to add.
# If there's a filter that excludes the target,
# the overwrite check shouldn't trigger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, copy/paste error!

source = self.tmpdir
(source / '__main__.py').touch()
target = source / 'target.pyz'
target.touch()
pyz_filter = lambda p: not p.match('*.pyz')
zipapp.create_archive(source, target, filter=pyz_filter)
with zipfile.ZipFile(target, 'r') as z:
self.assertEqual(len(z.namelist()), 1)
self.assertIn('__main__.py', z.namelist())

def test_create_archive_filter_exclude_dir(self):
# Test packing a directory and using a filter to exclude a
# subdirectory (ensures that the path supplied to include
Expand Down
12 changes: 8 additions & 4 deletions Lib/zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ def create_archive(source, target=None, interpreter=None, main=None,
# the target is being created in the source directory - we
# don't want the target being added to itself
files_to_add = sorted(source.rglob('*'))
if filter:
files_to_add = {f: f2 for f in files_to_add
if filter(f2 := f.relative_to(source))}
else:
files_to_add = {f: f.relative_to(source) for f in files_to_add}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comprehension doesn't add to readability, IMO. I suggest something more like:

files_to_add = []
for file in sorted(source.rglob("*")):
    relative_name = file.relative_to(source)
    if filter is None or filter(relative_name):
        files_to_add.append((file, relative_name))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do away with the comprehension, but building files_to_add as a list of tuples makes it more difficult to search for target in it. A simple target in files_to_add will no longer work.
One option is to again use a dictionary for files_to_add. Another is to do the check for target as part of this loop, like this:

files_to_add = []
for file in sorted(source.rglob("*")):
    relative_name = file.relative_to(source)
    if filter is None or filter(relative_name):
        if file == target:
            raise ZipAppError(...)
        files_to_add.append((file, relative_name))

I have no strong opinion. Doing the target check separately as target in files_to_add is more idiomatic but doing it as part of the loop (as above) is probably a tiny bit more efficient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I have no particular opinion between using a dictionary or checking in the loop - do whatever you prefer. But let's use an explicit loop, readability is important.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I went with the dictionary. It occurred to me that when files_to_add is a dictionary, the target in files_to_add check should be basically free, and more importantly, this means I don't have to split the exception message string over two lines. :)


# The target cannot be in the list of files to add. If it were, we'd
# end up overwriting the source file and writing the archive into
Expand All @@ -154,15 +159,14 @@ def create_archive(source, target=None, interpreter=None, main=None,
raise ZipAppError(
f"The target archive {target} overwrites one of the source files.")


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unintended blank line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will fix!

with _maybe_open(target, 'wb') as fd:
_write_file_prefix(fd, interpreter)
compression = (zipfile.ZIP_DEFLATED if compressed else
zipfile.ZIP_STORED)
with zipfile.ZipFile(fd, 'w', compression=compression) as z:
for child in files_to_add:
arcname = child.relative_to(source)
if filter is None or filter(arcname):
z.write(child, arcname.as_posix())
for child, arcname in files_to_add.items():
z.write(child, arcname.as_posix())
if main_py:
z.writestr('__main__.py', main_py.encode('utf-8'))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The zipapp module now applies the provided filter to the list of files to be added before determining whether the target file overwrites a source file. This allows the target file to be written inside the source directory provided it is excluded by the filter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree strongly with this news item, as it suggests that writing the target into the source directory is intended usage.

Maybe

The zipapp module now applies the filter when creating the list of files to add, rather than waiting until the file is being added to the archive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I wanted to get a bit of rationale in there, but I agree that it could come off as a recommendation, and it got long-winded too. I'll change it.

Loading