Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6)

Side by Side Diff: Lib/zipfile.py

Issue 22217: Reprs for zipfile classes
Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_zipfile.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """ 1 """
2 Read and write ZIP files. 2 Read and write ZIP files.
3 3
4 XXX references to utf-8 need further investigation. 4 XXX references to utf-8 need further investigation.
5 """ 5 """
6 import io 6 import io
7 import os 7 import os
8 import re 8 import re
9 import importlib.util 9 import importlib.util
10 import sys 10 import sys
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 self.reserved = 0 # Must be zero 347 self.reserved = 0 # Must be zero
348 self.flag_bits = 0 # ZIP flag bits 348 self.flag_bits = 0 # ZIP flag bits
349 self.volume = 0 # Volume number of file header 349 self.volume = 0 # Volume number of file header
350 self.internal_attr = 0 # Internal attributes 350 self.internal_attr = 0 # Internal attributes
351 self.external_attr = 0 # External file attributes 351 self.external_attr = 0 # External file attributes
352 # Other attributes are set by class ZipFile: 352 # Other attributes are set by class ZipFile:
353 # header_offset Byte offset to the file header 353 # header_offset Byte offset to the file header
354 # CRC CRC-32 of the uncompressed file 354 # CRC CRC-32 of the uncompressed file
355 # compress_size Size of the compressed file 355 # compress_size Size of the compressed file
356 # file_size Size of the uncompressed file 356 # file_size Size of the uncompressed file
357
358 def __repr__(self):
359 result = ['<%s filename=%r' % (self.__class__.__name__, self.filename)]
360 if self.compress_type != ZIP_STORED:
361 result.append(' compress_type=%s' %
362 (compressor_names.get(self.compress_type,
363 self.compress_type),))
364 hi = self.external_attr >> 16
365 lo = self.external_attr & 0xFFFF
366 if hi:
367 result.append(' filemode=%r' % (stat.filemode(hi),))
berkerpeksag 2014/10/01 01:40:47 Passing a tuple is not necessary here. ' file
storchaka 2014/10/08 19:42:09 I were too paranoid here. When right argument of t
368 if lo:
369 result.append(' external_attr=%#x' % (lo,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
370 isdir = self.filename[-1:] == '/'
371 if not isdir or self.file_size:
372 result.append(' file_size=%r' % (self.file_size,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
373 if ((not isdir or self.compress_size) and
374 (self.compress_type != ZIP_STORED or
375 self.file_size != self.compress_size)):
376 result.append(' compress_size=%r' % (self.compress_size,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
377 result.append('>')
378 return ''.join(result)
357 379
358 def FileHeader(self, zip64=None): 380 def FileHeader(self, zip64=None):
359 """Return the per-file header as a string.""" 381 """Return the per-file header as a string."""
360 dt = self.date_time 382 dt = self.date_time
361 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2] 383 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
362 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2) 384 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
363 if self.flag_bits & 0x08: 385 if self.flag_bits & 0x08:
364 # Set these to zero because we write them after the file data 386 # Set these to zero because we write them after the file data
365 CRC = compress_size = file_size = 0 387 CRC = compress_size = file_size = 0
366 else: 388 else:
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 self._compress_left -= 12 685 self._compress_left -= 12
664 686
665 self.mode = mode 687 self.mode = mode
666 self.name = zipinfo.filename 688 self.name = zipinfo.filename
667 689
668 if hasattr(zipinfo, 'CRC'): 690 if hasattr(zipinfo, 'CRC'):
669 self._expected_crc = zipinfo.CRC 691 self._expected_crc = zipinfo.CRC
670 self._running_crc = crc32(b'') & 0xffffffff 692 self._running_crc = crc32(b'') & 0xffffffff
671 else: 693 else:
672 self._expected_crc = None 694 self._expected_crc = None
695
696 def __repr__(self):
697 result = ['<%s.%s' % (self.__class__.__module__,
698 self.__class__.__qualname__)]
699 if not self.closed:
700 result.append(' name=%r mode=%r' % (self.name, self.mode))
701 if self._compress_type != ZIP_STORED:
702 result.append(' compress_type=%s' %
703 (compressor_names.get(self._compress_type,
704 self._compress_type),))
berkerpeksag 2014/10/01 01:40:47 Same as above.
705 else:
706 result.append(' [closed]')
707 result.append('>')
708 return ''.join(result)
673 709
674 def readline(self, limit=-1): 710 def readline(self, limit=-1):
675 """Read and return a line from the stream. 711 """Read and return a line from the stream.
676 712
677 If limit is specified, at most limit bytes will be read. 713 If limit is specified, at most limit bytes will be read.
678 """ 714 """
679 715
680 if not self._universal and limit < 0: 716 if not self._universal and limit < 0:
681 # Shortcut common case - newline found in buffer. 717 # Shortcut common case - newline found in buffer.
682 i = self._readbuffer.find(b'\n', self._offset) + 1 718 i = self._readbuffer.find(b'\n', self._offset) + 1
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 self.fp = None 995 self.fp = None
960 if not self._filePassed: 996 if not self._filePassed:
961 fp.close() 997 fp.close()
962 raise 998 raise
963 999
964 def __enter__(self): 1000 def __enter__(self):
965 return self 1001 return self
966 1002
967 def __exit__(self, type, value, traceback): 1003 def __exit__(self, type, value, traceback):
968 self.close() 1004 self.close()
1005
1006 def __repr__(self):
1007 result = ['<%s.%s' % (self.__class__.__module__,
1008 self.__class__.__qualname__)]
1009 if self.fp is not None:
1010 if self._filePassed:
1011 result.append(' file=%r' % (self.fp,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
1012 elif self.filename is not None:
1013 result.append(' filename=%r' % (self.filename,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
1014 result.append(' mode=%r' % (self.mode,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
1015 else:
1016 result.append(' [closed]')
1017 result.append('>')
1018 return ''.join(result)
969 1019
970 def _RealGetContents(self): 1020 def _RealGetContents(self):
971 """Read in the table of contents for the ZIP file.""" 1021 """Read in the table of contents for the ZIP file."""
972 fp = self.fp 1022 fp = self.fp
973 try: 1023 try:
974 endrec = _EndRecData(fp) 1024 endrec = _EndRecData(fp)
975 except OSError: 1025 except OSError:
976 raise BadZipFile("File is not a zip file") 1026 raise BadZipFile("File is not a zip file")
977 if not endrec: 1027 if not endrec:
978 raise BadZipFile("File is not a zip file") 1028 raise BadZipFile("File is not a zip file")
(...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after
1779 addToZip(zf, 1829 addToZip(zf,
1780 os.path.join(path, nm), os.path.join(zippath, nm)) 1830 os.path.join(path, nm), os.path.join(zippath, nm))
1781 # else: ignore 1831 # else: ignore
1782 1832
1783 with ZipFile(args[1], 'w') as zf: 1833 with ZipFile(args[1], 'w') as zf:
1784 for src in args[2:]: 1834 for src in args[2:]:
1785 addToZip(zf, src, os.path.basename(src)) 1835 addToZip(zf, src, os.path.basename(src))
1786 1836
1787 if __name__ == "__main__": 1837 if __name__ == "__main__":
1788 main() 1838 main()
OLDNEW
« no previous file with comments | « Lib/test/test_zipfile.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+