Skip to content

Commit 29e6c7b

Browse files
authored
gh-112578: Fix RuntimeWarning when running zipfile (GH-112579)
1 parent fc9e24b commit 29e6c7b

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
@@ -2227,12 +2227,79 @@ def _compile(file, optimize=-1):
22272227
return (fname, archivename)
22282228

22292229

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

22332303
# used privately for tests
22342304
CompleteDirs, # noqa: F401
22352305
)
2236-
2237-
# used privately for tests
2238-
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)