Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 0c2c9d4

Browse files
committed
Fix zip file opening.
ZipFile.open has very different meaning from TarFile.open. ZipFile.__init__ and TarFile.__init__ are fairly similar, but the latter does not have the higher-level features of TarFile.open, namely transparent handling of compression. This is a bit hacky, but it allows SageTarFile.__init__ to take full advantage of the TarFile.open classmethod.
1 parent 66d67dc commit 0c2c9d4

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

build/bin/sage-uncompress-spkg

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,14 @@ class SageTarFile(tarfile.TarFile):
8585
See http://trac.sagemath.org/ticket/20218#comment:16 for more background.
8686
"""
8787

88-
def __init__(self, *args, **kwargs):
89-
super(SageTarFile, self).__init__(*args, **kwargs)
88+
def __new__(cls, *args, **kwargs):
89+
# This is is that SageTarFile() is equivalent to TarFile.open() which
90+
# is more flexible than the basic TarFile.__init__
91+
inst = tarfile.TarFile.open(*args, **kwargs)
92+
inst.__class__ = cls
93+
return inst
9094

95+
def __init__(self, *args, **kwargs):
9196
# Unfortunately the only way to get the current umask is to set it
9297
# and then restore it
9398
self.umask = os.umask(0o777)
@@ -185,7 +190,7 @@ ARCHIVE_TYPES = [SageTarFile, SageZipFile]
185190

186191
def main(argv=None):
187192
parser = argparse.ArgumentParser()
188-
parser.add_argument('-d', dest='dir', nargs=1, metavar='DIR',
193+
parser.add_argument('-d', dest='dir', metavar='DIR',
189194
help='directory to extract archive contents into')
190195
parser.add_argument('pkg', nargs=1, metavar='PKG',
191196
help='the archive to extract')
@@ -196,7 +201,7 @@ def main(argv=None):
196201
args = parser.parse_args(argv)
197202

198203
filename = args.pkg[0]
199-
dirname = args.dir and args.dir[0]
204+
dirname = args.dir
200205

201206
for cls in ARCHIVE_TYPES:
202207
if cls.can_read(filename):
@@ -208,7 +213,7 @@ def main(argv=None):
208213

209214
# For now ZipFile and TarFile both have default open modes that are
210215
# acceptable
211-
archive = cls.open(filename)
216+
archive = cls(filename)
212217

213218
if args.file:
214219
contents = archive.extractbytes(args.file)

0 commit comments

Comments
 (0)