-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-112578: Fix RuntimeWarning when running zipfile #112579
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,4 @@ | ||
import sys | ||
import os | ||
from . import ZipFile, ZIP_DEFLATED | ||
|
||
|
||
def main(args=None): | ||
import argparse | ||
|
||
description = 'A simple command-line interface for zipfile module.' | ||
parser = argparse.ArgumentParser(description=description) | ||
group = parser.add_mutually_exclusive_group(required=True) | ||
group.add_argument('-l', '--list', metavar='<zipfile>', | ||
help='Show listing of a zipfile') | ||
group.add_argument('-e', '--extract', nargs=2, | ||
metavar=('<zipfile>', '<output_dir>'), | ||
help='Extract zipfile into target dir') | ||
group.add_argument('-c', '--create', nargs='+', | ||
metavar=('<name>', '<file>'), | ||
help='Create zipfile from sources') | ||
group.add_argument('-t', '--test', metavar='<zipfile>', | ||
help='Test if a zipfile is valid') | ||
parser.add_argument('--metadata-encoding', metavar='<encoding>', | ||
help='Specify encoding of member names for -l, -e and -t') | ||
args = parser.parse_args(args) | ||
|
||
encoding = args.metadata_encoding | ||
|
||
if args.test is not None: | ||
src = args.test | ||
with ZipFile(src, 'r', metadata_encoding=encoding) as zf: | ||
badfile = zf.testzip() | ||
if badfile: | ||
print("The following enclosed file is corrupted: {!r}".format(badfile)) | ||
print("Done testing") | ||
|
||
elif args.list is not None: | ||
src = args.list | ||
with ZipFile(src, 'r', metadata_encoding=encoding) as zf: | ||
zf.printdir() | ||
|
||
elif args.extract is not None: | ||
src, curdir = args.extract | ||
with ZipFile(src, 'r', metadata_encoding=encoding) as zf: | ||
zf.extractall(curdir) | ||
|
||
elif args.create is not None: | ||
if encoding: | ||
print("Non-conforming encodings not supported with -c.", | ||
file=sys.stderr) | ||
sys.exit(1) | ||
|
||
zip_name = args.create.pop(0) | ||
files = args.create | ||
|
||
def addToZip(zf, path, zippath): | ||
if os.path.isfile(path): | ||
zf.write(path, zippath, ZIP_DEFLATED) | ||
elif os.path.isdir(path): | ||
if zippath: | ||
zf.write(path, zippath) | ||
for nm in sorted(os.listdir(path)): | ||
addToZip(zf, | ||
os.path.join(path, nm), os.path.join(zippath, nm)) | ||
# else: ignore | ||
|
||
with ZipFile(zip_name, 'w') as zf: | ||
for path in files: | ||
zippath = os.path.basename(path) | ||
if not zippath: | ||
zippath = os.path.basename(os.path.dirname(path)) | ||
if zippath in ('', os.curdir, os.pardir): | ||
zippath = '' | ||
addToZip(zf, path, zippath) | ||
|
||
from . import main | ||
|
||
if __name__ == "__main__": | ||
main() |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2023-12-01-08-28-09.gh-issue-112578.bfNbfi.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a spurious :exc:`RuntimeWarning` when executing the :mod:`zipfile` module. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe simply remove this import?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what I did initially (see first commit). But I want to backport this change and I got worried because
zipfile.main
has existed for 15 years and that comment has only existed for one year.I found one use in the wild too: https://github.com/ThoreBor/Anki_Leaderboard/blob/master/tools/ankiaddon.py