Skip to content

Commit 73cda99

Browse files
[3.12] gh-112578: Fix RuntimeWarning when running zipfile (GH-112579) (GH-112646)
(cherry picked from commit 29e6c7b) Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
1 parent 6221482 commit 73cda99

File tree

3 files changed

+72
-77
lines changed

3 files changed

+72
-77
lines changed

Lib/zipfile/__init__.py

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,12 +2213,79 @@ def _compile(file, optimize=-1):
22132213
return (fname, archivename)
22142214

22152215

2216+
def main(args=None):
2217+
import argparse
2218+
2219+
description = 'A simple command-line interface for zipfile module.'
2220+
parser = argparse.ArgumentParser(description=description)
2221+
group = parser.add_mutually_exclusive_group(required=True)
2222+
group.add_argument('-l', '--list', metavar='<zipfile>',
2223+
help='Show listing of a zipfile')
2224+
group.add_argument('-e', '--extract', nargs=2,
2225+
metavar=('<zipfile>', '<output_dir>'),
2226+
help='Extract zipfile into target dir')
2227+
group.add_argument('-c', '--create', nargs='+',
2228+
metavar=('<name>', '<file>'),
2229+
help='Create zipfile from sources')
2230+
group.add_argument('-t', '--test', metavar='<zipfile>',
2231+
help='Test if a zipfile is valid')
2232+
parser.add_argument('--metadata-encoding', metavar='<encoding>',
2233+
help='Specify encoding of member names for -l, -e and -t')
2234+
args = parser.parse_args(args)
2235+
2236+
encoding = args.metadata_encoding
2237+
2238+
if args.test is not None:
2239+
src = args.test
2240+
with ZipFile(src, 'r', metadata_encoding=encoding) as zf:
2241+
badfile = zf.testzip()
2242+
if badfile:
2243+
print("The following enclosed file is corrupted: {!r}".format(badfile))
2244+
print("Done testing")
2245+
2246+
elif args.list is not None:
2247+
src = args.list
2248+
with ZipFile(src, 'r', metadata_encoding=encoding) as zf:
2249+
zf.printdir()
2250+
2251+
elif args.extract is not None:
2252+
src, curdir = args.extract
2253+
with ZipFile(src, 'r', metadata_encoding=encoding) as zf:
2254+
zf.extractall(curdir)
2255+
2256+
elif args.create is not None:
2257+
if encoding:
2258+
print("Non-conforming encodings not supported with -c.",
2259+
file=sys.stderr)
2260+
sys.exit(1)
2261+
2262+
zip_name = args.create.pop(0)
2263+
files = args.create
2264+
2265+
def addToZip(zf, path, zippath):
2266+
if os.path.isfile(path):
2267+
zf.write(path, zippath, ZIP_DEFLATED)
2268+
elif os.path.isdir(path):
2269+
if zippath:
2270+
zf.write(path, zippath)
2271+
for nm in sorted(os.listdir(path)):
2272+
addToZip(zf,
2273+
os.path.join(path, nm), os.path.join(zippath, nm))
2274+
# else: ignore
2275+
2276+
with ZipFile(zip_name, 'w') as zf:
2277+
for path in files:
2278+
zippath = os.path.basename(path)
2279+
if not zippath:
2280+
zippath = os.path.basename(os.path.dirname(path))
2281+
if zippath in ('', os.curdir, os.pardir):
2282+
zippath = ''
2283+
addToZip(zf, path, zippath)
2284+
2285+
22162286
from ._path import ( # noqa: E402
22172287
Path,
22182288

22192289
# used privately for tests
22202290
CompleteDirs, # noqa: F401
22212291
)
2222-
2223-
# used privately for tests
2224-
from .__main__ import main # noqa: F401, E402

Lib/zipfile/__main__.py

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,4 @@
1-
import sys
2-
import os
3-
from . import ZipFile, ZIP_DEFLATED
4-
5-
6-
def main(args=None):
7-
import argparse
8-
9-
description = 'A simple command-line interface for zipfile module.'
10-
parser = argparse.ArgumentParser(description=description)
11-
group = parser.add_mutually_exclusive_group(required=True)
12-
group.add_argument('-l', '--list', metavar='<zipfile>',
13-
help='Show listing of a zipfile')
14-
group.add_argument('-e', '--extract', nargs=2,
15-
metavar=('<zipfile>', '<output_dir>'),
16-
help='Extract zipfile into target dir')
17-
group.add_argument('-c', '--create', nargs='+',
18-
metavar=('<name>', '<file>'),
19-
help='Create zipfile from sources')
20-
group.add_argument('-t', '--test', metavar='<zipfile>',
21-
help='Test if a zipfile is valid')
22-
parser.add_argument('--metadata-encoding', metavar='<encoding>',
23-
help='Specify encoding of member names for -l, -e and -t')
24-
args = parser.parse_args(args)
25-
26-
encoding = args.metadata_encoding
27-
28-
if args.test is not None:
29-
src = args.test
30-
with ZipFile(src, 'r', metadata_encoding=encoding) as zf:
31-
badfile = zf.testzip()
32-
if badfile:
33-
print("The following enclosed file is corrupted: {!r}".format(badfile))
34-
print("Done testing")
35-
36-
elif args.list is not None:
37-
src = args.list
38-
with ZipFile(src, 'r', metadata_encoding=encoding) as zf:
39-
zf.printdir()
40-
41-
elif args.extract is not None:
42-
src, curdir = args.extract
43-
with ZipFile(src, 'r', metadata_encoding=encoding) as zf:
44-
zf.extractall(curdir)
45-
46-
elif args.create is not None:
47-
if encoding:
48-
print("Non-conforming encodings not supported with -c.",
49-
file=sys.stderr)
50-
sys.exit(1)
51-
52-
zip_name = args.create.pop(0)
53-
files = args.create
54-
55-
def addToZip(zf, path, zippath):
56-
if os.path.isfile(path):
57-
zf.write(path, zippath, ZIP_DEFLATED)
58-
elif os.path.isdir(path):
59-
if zippath:
60-
zf.write(path, zippath)
61-
for nm in sorted(os.listdir(path)):
62-
addToZip(zf,
63-
os.path.join(path, nm), os.path.join(zippath, nm))
64-
# else: ignore
65-
66-
with ZipFile(zip_name, 'w') as zf:
67-
for path in files:
68-
zippath = os.path.basename(path)
69-
if not zippath:
70-
zippath = os.path.basename(os.path.dirname(path))
71-
if zippath in ('', os.curdir, os.pardir):
72-
zippath = ''
73-
addToZip(zf, path, zippath)
74-
1+
from . import main
752

763
if __name__ == "__main__":
774
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a spurious :exc:`RuntimeWarning` when executing the :mod:`zipfile` module.

0 commit comments

Comments
 (0)