Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes
zipfile.ZipExtFile Class Reference
Inheritance diagram for zipfile.ZipExtFile:
Inheritance graph
[legend]
Collaboration diagram for zipfile.ZipExtFile:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def readline
def peek
def readable
def read
def read1
def close
def __new__
def register
def __instancecheck__
def __subclasscheck__

Public Attributes

 newlines
 mode
 name

Static Public Attributes

int MAX_N = 1
int MIN_READ_SIZE = 4096
tuple PATTERN = re.compile(br'^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)')

Private Member Functions

def _update_crc

Private Attributes

 _fileobj
 _decrypter
 _close_fileobj
 _compress_type
 _compress_size
 _compress_left
 _decompressor
 _unconsumed
 _readbuffer
 _offset
 _universal
 _expected_crc
 _running_crc

Detailed Description

File-like object for reading an archive member.
   Is returned by ZipFile.open().

Definition at line 460 of file zipfile.py.


Constructor & Destructor Documentation

def zipfile.ZipExtFile.__init__ (   self,
  fileobj,
  mode,
  zipinfo,
  decrypter = None,
  close_fileobj = False 
)

Definition at line 475 of file zipfile.py.

00475 
00476                  close_fileobj=False):
00477         self._fileobj = fileobj
00478         self._decrypter = decrypter
00479         self._close_fileobj = close_fileobj
00480 
00481         self._compress_type = zipinfo.compress_type
00482         self._compress_size = zipinfo.compress_size
00483         self._compress_left = zipinfo.compress_size
00484 
00485         if self._compress_type == ZIP_DEFLATED:
00486             self._decompressor = zlib.decompressobj(-15)
00487         self._unconsumed = b''
00488 
00489         self._readbuffer = b''
00490         self._offset = 0
00491 
00492         self._universal = 'U' in mode
00493         self.newlines = None
00494 
00495         # Adjust read size for encrypted files since the first 12 bytes
00496         # are for the encryption/password information.
00497         if self._decrypter is not None:
00498             self._compress_left -= 12
00499 
00500         self.mode = mode
00501         self.name = zipinfo.filename
00502 
00503         if hasattr(zipinfo, 'CRC'):
00504             self._expected_crc = zipinfo.CRC
00505             self._running_crc = crc32(b'') & 0xffffffff
00506         else:
00507             self._expected_crc = None

Here is the caller graph for this function:


Member Function Documentation

def abc.ABCMeta.__instancecheck__ (   cls,
  instance 
) [inherited]
Override for isinstance(instance, cls).

Definition at line 158 of file abc.py.

00158 
00159     def __instancecheck__(cls, instance):
00160         """Override for isinstance(instance, cls)."""
00161         # Inline the cache checking
00162         subclass = instance.__class__
00163         if subclass in cls._abc_cache:
00164             return True
00165         subtype = type(instance)
00166         if subtype is subclass:
00167             if (cls._abc_negative_cache_version ==
00168                 ABCMeta._abc_invalidation_counter and
00169                 subclass in cls._abc_negative_cache):
00170                 return False
00171             # Fall back to the subclass check.
00172             return cls.__subclasscheck__(subclass)
00173         return any(cls.__subclasscheck__(c) for c in {subclass, subtype})

Here is the call graph for this function:

def abc.ABCMeta.__new__ (   mcls,
  name,
  bases,
  namespace 
) [inherited]

Definition at line 116 of file abc.py.

00116 
00117     def __new__(mcls, name, bases, namespace):
00118         cls = super().__new__(mcls, name, bases, namespace)
00119         # Compute set of abstract method names
00120         abstracts = {name
00121                      for name, value in namespace.items()
00122                      if getattr(value, "__isabstractmethod__", False)}
00123         for base in bases:
00124             for name in getattr(base, "__abstractmethods__", set()):
00125                 value = getattr(cls, name, None)
00126                 if getattr(value, "__isabstractmethod__", False):
00127                     abstracts.add(name)
00128         cls.__abstractmethods__ = frozenset(abstracts)
00129         # Set up inheritance registry
00130         cls._abc_registry = WeakSet()
00131         cls._abc_cache = WeakSet()
00132         cls._abc_negative_cache = WeakSet()
00133         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00134         return cls

Here is the call graph for this function:

def abc.ABCMeta.__subclasscheck__ (   cls,
  subclass 
) [inherited]
Override for issubclass(subclass, cls).

Definition at line 174 of file abc.py.

00174 
00175     def __subclasscheck__(cls, subclass):
00176         """Override for issubclass(subclass, cls)."""
00177         # Check cache
00178         if subclass in cls._abc_cache:
00179             return True
00180         # Check negative cache; may have to invalidate
00181         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
00182             # Invalidate the negative cache
00183             cls._abc_negative_cache = WeakSet()
00184             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00185         elif subclass in cls._abc_negative_cache:
00186             return False
00187         # Check the subclass hook
00188         ok = cls.__subclasshook__(subclass)
00189         if ok is not NotImplemented:
00190             assert isinstance(ok, bool)
00191             if ok:
00192                 cls._abc_cache.add(subclass)
00193             else:
00194                 cls._abc_negative_cache.add(subclass)
00195             return ok
00196         # Check if it's a direct subclass
00197         if cls in getattr(subclass, '__mro__', ()):
00198             cls._abc_cache.add(subclass)
00199             return True
00200         # Check if it's a subclass of a registered class (recursive)
00201         for rcls in cls._abc_registry:
00202             if issubclass(subclass, rcls):
00203                 cls._abc_cache.add(subclass)
00204                 return True
00205         # Check if it's a subclass of a subclass (recursive)
00206         for scls in cls.__subclasses__():
00207             if issubclass(subclass, scls):
00208                 cls._abc_cache.add(subclass)
00209                 return True
00210         # No dice; update negative cache
00211         cls._abc_negative_cache.add(subclass)
00212         return False

Here is the call graph for this function:

def zipfile.ZipExtFile._update_crc (   self,
  newdata,
  eof 
) [private]

Definition at line 588 of file zipfile.py.

00588 
00589     def _update_crc(self, newdata, eof):
00590         # Update the CRC using the given data.
00591         if self._expected_crc is None:
00592             # No need to compute the CRC if we don't have a reference value
00593             return
00594         self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff
00595         # Check the CRC if we're at the end of the file
00596         if eof and self._running_crc != self._expected_crc:
00597             raise BadZipFile("Bad CRC-32 for file %r" % self.name)

Here is the caller graph for this function:

def zipfile.ZipExtFile.close (   self)

Definition at line 650 of file zipfile.py.

00650 
00651     def close(self):
00652         try:
00653             if self._close_fileobj:
00654                 self._fileobj.close()
00655         finally:
00656             super().close()
00657 

Here is the caller graph for this function:

def zipfile.ZipExtFile.peek (   self,
  n = 1 
)
Returns buffered bytes without advancing the position.

Definition at line 558 of file zipfile.py.

00558 
00559     def peek(self, n=1):
00560         """Returns buffered bytes without advancing the position."""
00561         if n > len(self._readbuffer) - self._offset:
00562             chunk = self.read(n)
00563             self._offset -= len(chunk)
00564 
00565         # Return up to 512 bytes to reduce allocation overhead for tight loops.
00566         return self._readbuffer[self._offset: self._offset + 512]

Here is the caller graph for this function:

def zipfile.ZipExtFile.read (   self,
  n = -1 
)
Read and return up to n bytes.
If the argument is omitted, None, or negative, data is read and returned until EOF is reached..

Definition at line 570 of file zipfile.py.

00570 
00571     def read(self, n=-1):
00572         """Read and return up to n bytes.
00573         If the argument is omitted, None, or negative, data is read and returned until EOF is reached..
00574         """
00575         buf = b''
00576         if n is None:
00577             n = -1
00578         while True:
00579             if n < 0:
00580                 data = self.read1(n)
00581             elif n > len(buf):
00582                 data = self.read1(n - len(buf))
00583             else:
00584                 return buf
00585             if len(data) == 0:
00586                 return buf
00587             buf += data

Here is the call graph for this function:

Here is the caller graph for this function:

def zipfile.ZipExtFile.read1 (   self,
  n 
)
Read up to n bytes with at most one read() system call.

Definition at line 598 of file zipfile.py.

00598 
00599     def read1(self, n):
00600         """Read up to n bytes with at most one read() system call."""
00601 
00602         # Simplify algorithm (branching) by transforming negative n to large n.
00603         if n < 0 or n is None:
00604             n = self.MAX_N
00605 
00606         # Bytes available in read buffer.
00607         len_readbuffer = len(self._readbuffer) - self._offset
00608 
00609         # Read from file.
00610         if self._compress_left > 0 and n > len_readbuffer + len(self._unconsumed):
00611             nbytes = n - len_readbuffer - len(self._unconsumed)
00612             nbytes = max(nbytes, self.MIN_READ_SIZE)
00613             nbytes = min(nbytes, self._compress_left)
00614 
00615             data = self._fileobj.read(nbytes)
00616             self._compress_left -= len(data)
00617 
00618             if data and self._decrypter is not None:
00619                 data = bytes(map(self._decrypter, data))
00620 
00621             if self._compress_type == ZIP_STORED:
00622                 self._update_crc(data, eof=(self._compress_left==0))
00623                 self._readbuffer = self._readbuffer[self._offset:] + data
00624                 self._offset = 0
00625             else:
00626                 # Prepare deflated bytes for decompression.
00627                 self._unconsumed += data
00628 
00629         # Handle unconsumed data.
00630         if (len(self._unconsumed) > 0 and n > len_readbuffer and
00631             self._compress_type == ZIP_DEFLATED):
00632             data = self._decompressor.decompress(
00633                 self._unconsumed,
00634                 max(n - len_readbuffer, self.MIN_READ_SIZE)
00635             )
00636 
00637             self._unconsumed = self._decompressor.unconsumed_tail
00638             eof = len(self._unconsumed) == 0 and self._compress_left == 0
00639             if eof:
00640                 data += self._decompressor.flush()
00641 
00642             self._update_crc(data, eof=eof)
00643             self._readbuffer = self._readbuffer[self._offset:] + data
00644             self._offset = 0
00645 
00646         # Read from buffer.
00647         data = self._readbuffer[self._offset: self._offset + n]
00648         self._offset += len(data)
00649         return data

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 567 of file zipfile.py.

00567 
00568     def readable(self):
00569         return True

def zipfile.ZipExtFile.readline (   self,
  limit = -1 
)
Read and return a line from the stream.

If limit is specified, at most limit bytes will be read.

Definition at line 508 of file zipfile.py.

00508 
00509     def readline(self, limit=-1):
00510         """Read and return a line from the stream.
00511 
00512         If limit is specified, at most limit bytes will be read.
00513         """
00514 
00515         if not self._universal and limit < 0:
00516             # Shortcut common case - newline found in buffer.
00517             i = self._readbuffer.find(b'\n', self._offset) + 1
00518             if i > 0:
00519                 line = self._readbuffer[self._offset: i]
00520                 self._offset = i
00521                 return line
00522 
00523         if not self._universal:
00524             return io.BufferedIOBase.readline(self, limit)
00525 
00526         line = b''
00527         while limit < 0 or len(line) < limit:
00528             readahead = self.peek(2)
00529             if readahead == b'':
00530                 return line
00531 
00532             #
00533             # Search for universal newlines or line chunks.
00534             #
00535             # The pattern returns either a line chunk or a newline, but not
00536             # both. Combined with peek(2), we are assured that the sequence
00537             # '\r\n' is always retrieved completely and never split into
00538             # separate newlines - '\r', '\n' due to coincidental readaheads.
00539             #
00540             match = self.PATTERN.search(readahead)
00541             newline = match.group('newline')
00542             if newline is not None:
00543                 if self.newlines is None:
00544                     self.newlines = []
00545                 if newline not in self.newlines:
00546                     self.newlines.append(newline)
00547                 self._offset += len(newline)
00548                 return line + b'\n'
00549 
00550             chunk = match.group('chunk')
00551             if limit >= 0:
00552                 chunk = chunk[: limit - len(line)]
00553 
00554             self._offset += len(chunk)
00555             line += chunk
00556 
00557         return line

Here is the call graph for this function:

Here is the caller graph for this function:

def abc.ABCMeta.register (   cls,
  subclass 
) [inherited]
Register a virtual subclass of an ABC.

Definition at line 135 of file abc.py.

00135 
00136     def register(cls, subclass):
00137         """Register a virtual subclass of an ABC."""
00138         if not isinstance(subclass, type):
00139             raise TypeError("Can only register classes")
00140         if issubclass(subclass, cls):
00141             return  # Already a subclass
00142         # Subtle: test for cycles *after* testing for "already a subclass";
00143         # this means we allow X.register(X) and interpret it as a no-op.
00144         if issubclass(cls, subclass):
00145             # This would create a cycle, which is bad for the algorithm below
00146             raise RuntimeError("Refusing to create an inheritance cycle")
00147         cls._abc_registry.add(subclass)
00148         ABCMeta._abc_invalidation_counter += 1  # Invalidate negative cache

Here is the caller graph for this function:


Member Data Documentation

Definition at line 478 of file zipfile.py.

Definition at line 482 of file zipfile.py.

Definition at line 481 of file zipfile.py.

Definition at line 480 of file zipfile.py.

Definition at line 485 of file zipfile.py.

Definition at line 477 of file zipfile.py.

Definition at line 503 of file zipfile.py.

Definition at line 476 of file zipfile.py.

Definition at line 489 of file zipfile.py.

Definition at line 488 of file zipfile.py.

Definition at line 504 of file zipfile.py.

Definition at line 486 of file zipfile.py.

Definition at line 491 of file zipfile.py.

Definition at line 466 of file zipfile.py.

Definition at line 469 of file zipfile.py.

Definition at line 499 of file zipfile.py.

Definition at line 500 of file zipfile.py.

Definition at line 492 of file zipfile.py.

tuple zipfile.ZipExtFile.PATTERN = re.compile(br'^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)') [static]

Definition at line 472 of file zipfile.py.


The documentation for this class was generated from the following file: