cpython: 82fd95c2851b (original) (raw)
Mercurial > cpython
changeset 93259:82fd95c2851b
Issue #22217: Implemented reprs of classes in the zipfile module. [#22217]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Wed, 29 Oct 2014 22:42:06 +0200 |
parents | 708b2e656c1d |
children | f4f5b942e5e0 |
files | Lib/test/test_zipfile.py Lib/zipfile.py Misc/NEWS |
diffstat | 3 files changed, 83 insertions(+), 0 deletions(-)[+] [-] Lib/test/test_zipfile.py 31 Lib/zipfile.py 50 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -326,6 +326,37 @@ class AbstractTestsWithSourceFile: while zipopen.read1(100): pass
- def test_repr(self):
fname = 'file.name'[](#l1.8)
for f in get_files(self):[](#l1.9)
with zipfile.ZipFile(f, 'w', self.compression) as zipfp:[](#l1.10)
zipfp.write(TESTFN, fname)[](#l1.11)
r = repr(zipfp)[](#l1.12)
self.assertIn("mode='w'", r)[](#l1.13)
with zipfile.ZipFile(f, 'r') as zipfp:[](#l1.15)
r = repr(zipfp)[](#l1.16)
if isinstance(f, str):[](#l1.17)
self.assertIn('filename=%r' % f, r)[](#l1.18)
else:[](#l1.19)
self.assertIn('file=%r' % f, r)[](#l1.20)
self.assertIn("mode='r'", r)[](#l1.21)
r = repr(zipfp.getinfo(fname))[](#l1.22)
self.assertIn('filename=%r' % fname, r)[](#l1.23)
self.assertIn('filemode=', r)[](#l1.24)
self.assertIn('file_size=', r)[](#l1.25)
if self.compression != zipfile.ZIP_STORED:[](#l1.26)
self.assertIn('compress_type=', r)[](#l1.27)
self.assertIn('compress_size=', r)[](#l1.28)
with zipfp.open(fname) as zipopen:[](#l1.29)
r = repr(zipopen)[](#l1.30)
self.assertIn('name=%r' % fname, r)[](#l1.31)
self.assertIn("mode='r'", r)[](#l1.32)
if self.compression != zipfile.ZIP_STORED:[](#l1.33)
self.assertIn('compress_type=', r)[](#l1.34)
self.assertIn('[closed]', repr(zipopen))[](#l1.35)
self.assertIn('[closed]', repr(zipfp))[](#l1.36)
+ def tearDown(self): unlink(TESTFN) unlink(TESTFN2)
--- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -355,6 +355,28 @@ class ZipInfo (object): # compress_size Size of the compressed file # file_size Size of the uncompressed file
- def repr(self):
result = ['<%s filename=%r' % (self.__class__.__name__, self.filename)][](#l2.8)
if self.compress_type != ZIP_STORED:[](#l2.9)
result.append(' compress_type=%s' %[](#l2.10)
compressor_names.get(self.compress_type,[](#l2.11)
self.compress_type))[](#l2.12)
hi = self.external_attr >> 16[](#l2.13)
lo = self.external_attr & 0xFFFF[](#l2.14)
if hi:[](#l2.15)
result.append(' filemode=%r' % stat.filemode(hi))[](#l2.16)
if lo:[](#l2.17)
result.append(' external_attr=%#x' % lo)[](#l2.18)
isdir = self.filename[-1:] == '/'[](#l2.19)
if not isdir or self.file_size:[](#l2.20)
result.append(' file_size=%r' % self.file_size)[](#l2.21)
if ((not isdir or self.compress_size) and[](#l2.22)
(self.compress_type != ZIP_STORED or[](#l2.23)
self.file_size != self.compress_size)):[](#l2.24)
result.append(' compress_size=%r' % self.compress_size)[](#l2.25)
result.append('>')[](#l2.26)
return ''.join(result)[](#l2.27)
+ def FileHeader(self, zip64=None): """Return the per-file header as a string.""" dt = self.date_time @@ -671,6 +693,20 @@ class ZipExtFile(io.BufferedIOBase): else: self._expected_crc = None
- def repr(self):
result = ['<%s.%s' % (self.__class__.__module__,[](#l2.37)
self.__class__.__qualname__)][](#l2.38)
if not self.closed:[](#l2.39)
result.append(' name=%r mode=%r' % (self.name, self.mode))[](#l2.40)
if self._compress_type != ZIP_STORED:[](#l2.41)
result.append(' compress_type=%s' %[](#l2.42)
compressor_names.get(self._compress_type,[](#l2.43)
self._compress_type))[](#l2.44)
else:[](#l2.45)
result.append(' [closed]')[](#l2.46)
result.append('>')[](#l2.47)
return ''.join(result)[](#l2.48)
+ def readline(self, limit=-1): """Read and return a line from the stream. @@ -967,6 +1003,20 @@ class ZipFile: def exit(self, type, value, traceback): self.close()
- def repr(self):
result = ['<%s.%s' % (self.__class__.__module__,[](#l2.58)
self.__class__.__qualname__)][](#l2.59)
if self.fp is not None:[](#l2.60)
if self._filePassed:[](#l2.61)
result.append(' file=%r' % self.fp)[](#l2.62)
elif self.filename is not None:[](#l2.63)
result.append(' filename=%r' % self.filename)[](#l2.64)
result.append(' mode=%r' % self.mode)[](#l2.65)
else:[](#l2.66)
result.append(' [closed]')[](#l2.67)
result.append('>')[](#l2.68)
return ''.join(result)[](#l2.69)
+ def _RealGetContents(self): """Read in the table of contents for the ZIP file.""" fp = self.fp
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -181,6 +181,8 @@ Core and Builtins Library ------- +- Issue #22217: Implemented reprs of classes in the zipfile module. +