Back to index

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

List of all members.

Public Member Functions

def seek
 Positioning ###.
def tell
def truncate
def flush
 Flush and close ###.
def close
def __del__
def seekable
 Inquiries ###.
def readable
def writable
def closed
def __enter__
 Context manager ###.
def __exit__
def fileno
 Lower-level APIs ###.
def isatty
def readline
 Readline[s] and writelines ###.
def __iter__
def __next__
def readlines
def writelines
def __new__
def register
def __instancecheck__
def __subclasscheck__

Private Member Functions

def _unsupported
 Internal ###.
def _checkSeekable
def _checkReadable
def _checkWritable
def _checkClosed

Static Private Attributes

 __closed = False

Detailed Description

The abstract base class for all I/O classes, acting on streams of
bytes. There is no public constructor.

This class provides dummy implementations for many methods that
derived classes can override selectively; the default implementations
represent a file that cannot be read, written or seeked.

Even though IOBase does not declare read, readinto, or write because
their signatures will vary, implementations and clients should
consider those methods part of the interface. Also, implementations
may raise UnsupportedOperation when operations they do not support are
called.

The basic type used for binary data read from or written to a file is
bytes. bytearrays are accepted too, and in some cases (such as
readinto) needed. Text I/O classes work with str data.

Note that calling any method (even inquiries) on a closed stream is
undefined. Implementations may raise IOError in this case.

IOBase (and its subclasses) support the iterator protocol, meaning
that an IOBase object can be iterated over yielding the lines in a
stream.

IOBase also supports the :keyword:`with` statement. In this example,
fp is closed after the suite of the with statement is complete:

with open('spam.txt', 'r') as fp:
    fp.write('Spam and eggs!')

Definition at line 255 of file _pyio.py.


Constructor & Destructor Documentation

def _pyio.IOBase.__del__ (   self)
Destructor.  Calls close().

Definition at line 345 of file _pyio.py.

00345 
00346     def __del__(self):
00347         """Destructor.  Calls close()."""
00348         # The try/except block is in case this is called at program
00349         # exit time, when it's possible that globals have already been
00350         # deleted, and then the close() call might fail.  Since
00351         # there's nothing we can do about such failures and they annoy
00352         # the end users, we suppress the traceback.
00353         try:
00354             self.close()
00355         except:
00356             pass

Here is the call graph for this function:


Member Function Documentation

def _pyio.IOBase.__enter__ (   self)

Context manager ###.

Context management protocol.  Returns self (an instance of IOBase).

Definition at line 419 of file _pyio.py.

00419 
00420     def __enter__(self):  # That's a forward reference
00421         """Context management protocol.  Returns self (an instance of IOBase)."""
00422         self._checkClosed()
00423         return self

Here is the call graph for this function:

def _pyio.IOBase.__exit__ (   self,
  args 
)
Context management protocol.  Calls close()

Definition at line 424 of file _pyio.py.

00424 
00425     def __exit__(self, *args):
00426         """Context management protocol.  Calls close()"""
00427         self.close()

Here is the call graph for this function:

Here is the caller graph for this function:

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 _pyio.IOBase.__iter__ (   self)

Definition at line 486 of file _pyio.py.

00486 
00487     def __iter__(self):
00488         self._checkClosed()
00489         return self

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 _pyio.IOBase.__next__ (   self)

Reimplemented in _pyio.TextIOWrapper.

Definition at line 490 of file _pyio.py.

00490 
00491     def __next__(self):
00492         line = self.readline()
00493         if not line:
00494             raise StopIteration
00495         return line

Here is the call graph for this function:

Here is the caller 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 _pyio.IOBase._checkClosed (   self,
  msg = None 
) [private]
Internal: raise an ValueError if file is closed

Definition at line 410 of file _pyio.py.

00410 
00411     def _checkClosed(self, msg=None):
00412         """Internal: raise an ValueError if file is closed
00413         """
00414         if self.closed:
00415             raise ValueError("I/O operation on closed file."
00416                              if msg is None else msg)

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase._checkReadable (   self,
  msg = None 
) [private]
Internal: raise UnsupportedOperation if file is not readable

Definition at line 381 of file _pyio.py.

00381 
00382     def _checkReadable(self, msg=None):
00383         """Internal: raise UnsupportedOperation if file is not readable
00384         """
00385         if not self.readable():
00386             raise UnsupportedOperation("File or stream is not readable."
00387                                        if msg is None else msg)

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase._checkSeekable (   self,
  msg = None 
) [private]
Internal: raise UnsupportedOperation if file is not seekable

Definition at line 367 of file _pyio.py.

00367 
00368     def _checkSeekable(self, msg=None):
00369         """Internal: raise UnsupportedOperation if file is not seekable
00370         """
00371         if not self.seekable():
00372             raise UnsupportedOperation("File or stream is not seekable."
00373                                        if msg is None else msg)

Here is the call graph for this function:

def _pyio.IOBase._checkWritable (   self,
  msg = None 
) [private]
Internal: raise UnsupportedOperation if file is not writable

Definition at line 395 of file _pyio.py.

00395 
00396     def _checkWritable(self, msg=None):
00397         """Internal: raise UnsupportedOperation if file is not writable
00398         """
00399         if not self.writable():
00400             raise UnsupportedOperation("File or stream is not writable."
00401                                        if msg is None else msg)

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase._unsupported (   self,
  name 
) [private]

Internal ###.

Internal: raise an IOError exception for unsupported operations.

Definition at line 290 of file _pyio.py.

00290 
00291     def _unsupported(self, name):
00292         """Internal: raise an IOError exception for unsupported operations."""
00293         raise UnsupportedOperation("%s.%s() not supported" %
00294                                    (self.__class__.__name__, name))

def _pyio.IOBase.close (   self)
Flush and close the IO object.

This method has no effect if the file is already closed.

Reimplemented in _pyio.TextIOWrapper, _pyio.BufferedRWPair, and _pyio._BufferedIOMixin.

Definition at line 336 of file _pyio.py.

00336 
00337     def close(self):
00338         """Flush and close the IO object.
00339 
00340         This method has no effect if the file is already closed.
00341         """
00342         if not self.__closed:
00343             self.flush()
00344             self.__closed = True

Here is the call graph for this function:

def _pyio.IOBase.closed (   self)
closed: bool.  True iff the file has been closed.

For backwards compatibility, this is a property, not a predicate.

Reimplemented in _pyio.TextIOWrapper, _pyio.BufferedRWPair, and _pyio._BufferedIOMixin.

Definition at line 403 of file _pyio.py.

00403 
00404     def closed(self):
00405         """closed: bool.  True iff the file has been closed.
00406 
00407         For backwards compatibility, this is a property, not a predicate.
00408         """
00409         return self.__closed

Here is the caller graph for this function:

def _pyio.IOBase.fileno (   self)

Lower-level APIs ###.

Returns underlying file descriptor (an int) if one exists.

An IOError is raised if the IO object does not use a file descriptor.

Reimplemented in _pyio.TextIOWrapper, and _pyio._BufferedIOMixin.

Definition at line 432 of file _pyio.py.

00432 
00433     def fileno(self):
00434         """Returns underlying file descriptor (an int) if one exists.
00435 
00436         An IOError is raised if the IO object does not use a file descriptor.
00437         """
00438         self._unsupported("fileno")

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase.flush (   self)

Flush and close ###.

Flush write buffers, if applicable.

This is not implemented for read-only and non-blocking streams.

Reimplemented in _pyio.TextIOWrapper, _pyio.BufferedRWPair, _pyio.BufferedWriter, and _pyio._BufferedIOMixin.

Definition at line 326 of file _pyio.py.

00326 
00327     def flush(self):
00328         """Flush write buffers, if applicable.
00329 
00330         This is not implemented for read-only and non-blocking streams.
00331         """
00332         self._checkClosed()
00333         # XXX Should this return the number of bytes written???

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase.isatty (   self)
Return a bool indicating whether this is an 'interactive' stream.

Return False if it can't be determined.

Reimplemented in _pyio.TextIOWrapper, _pyio.BufferedRWPair, and _pyio._BufferedIOMixin.

Definition at line 439 of file _pyio.py.

00439 
00440     def isatty(self):
00441         """Return a bool indicating whether this is an 'interactive' stream.
00442 
00443         Return False if it can't be determined.
00444         """
00445         self._checkClosed()
00446         return False

Here is the call graph for this function:

def _pyio.IOBase.readable (   self)
Return a bool indicating whether object was opened for reading.

If False, read() will raise UnsupportedOperation.

Reimplemented in _pyio.TextIOWrapper, _pyio.BufferedRWPair, _pyio.BytesIO, and _pyio._BufferedIOMixin.

Definition at line 374 of file _pyio.py.

00374 
00375     def readable(self):
00376         """Return a bool indicating whether object was opened for reading.
00377 
00378         If False, read() will raise UnsupportedOperation.
00379         """
00380         return False

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase.readline (   self,
  limit = -1 
)

Readline[s] and writelines ###.

Reimplemented in _pyio.TextIOWrapper.

Definition at line 449 of file _pyio.py.

00449 
00450     def readline(self, limit=-1):
00451         r"""Read and return a line of bytes from the stream.
00452 
00453         If limit is specified, at most limit bytes will be read.
00454         Limit should be an int.
00455 
00456         The line terminator is always b'\n' for binary files; for text
00457         files, the newlines argument to open can be used to select the line
00458         terminator(s) recognized.
00459         """
00460         # For backwards compatibility, a (slowish) readline().
00461         if hasattr(self, "peek"):
00462             def nreadahead():
00463                 readahead = self.peek(1)
00464                 if not readahead:
00465                     return 1
00466                 n = (readahead.find(b"\n") + 1) or len(readahead)
00467                 if limit >= 0:
00468                     n = min(n, limit)
00469                 return n
00470         else:
00471             def nreadahead():
00472                 return 1
00473         if limit is None:
00474             limit = -1
00475         elif not isinstance(limit, int):
00476             raise TypeError("limit must be an integer")
00477         res = bytearray()
00478         while limit < 0 or len(res) < limit:
00479             b = self.read(nreadahead())
00480             if not b:
00481                 break
00482             res += b
00483             if res.endswith(b"\n"):
00484                 break
00485         return bytes(res)

Here is the caller graph for this function:

def _pyio.IOBase.readlines (   self,
  hint = None 
)
Return a list of lines from the stream.

hint can be specified to control the number of lines read: no more
lines will be read if the total size (in bytes/characters) of all
lines so far exceeds hint.

Definition at line 496 of file _pyio.py.

00496 
00497     def readlines(self, hint=None):
00498         """Return a list of lines from the stream.
00499 
00500         hint can be specified to control the number of lines read: no more
00501         lines will be read if the total size (in bytes/characters) of all
00502         lines so far exceeds hint.
00503         """
00504         if hint is None or hint <= 0:
00505             return list(self)
00506         n = 0
00507         lines = []
00508         for line in self:
00509             lines.append(line)
00510             n += len(line)
00511             if n >= hint:
00512                 break
00513         return lines

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:

def _pyio.IOBase.seek (   self,
  pos,
  whence = 0 
)

Positioning ###.

Change stream position.

Change the stream position to byte offset offset. offset is
interpreted relative to the position indicated by whence.  Values
for whence are ints:

* 0 -- start of stream (the default); offset should be zero or positive
* 1 -- current stream position; offset may be negative
* 2 -- end of stream; offset is usually negative

Return an int indicating the new absolute position.

Reimplemented in _pyio.TextIOWrapper, _pyio.BufferedRandom, _pyio.BufferedWriter, _pyio.BufferedReader, _pyio.BytesIO, and _pyio._BufferedIOMixin.

Definition at line 297 of file _pyio.py.

00297 
00298     def seek(self, pos, whence=0):
00299         """Change stream position.
00300 
00301         Change the stream position to byte offset offset. offset is
00302         interpreted relative to the position indicated by whence.  Values
00303         for whence are ints:
00304 
00305         * 0 -- start of stream (the default); offset should be zero or positive
00306         * 1 -- current stream position; offset may be negative
00307         * 2 -- end of stream; offset is usually negative
00308 
00309         Return an int indicating the new absolute position.
00310         """
00311         self._unsupported("seek")

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase.seekable (   self)

Inquiries ###.

Return a bool indicating whether object supports random access.

If False, seek(), tell() and truncate() will raise UnsupportedOperation.
This method may need to do a test seek().

Reimplemented in _pyio.TextIOWrapper, _pyio.BytesIO, and _pyio._BufferedIOMixin.

Definition at line 359 of file _pyio.py.

00359 
00360     def seekable(self):
00361         """Return a bool indicating whether object supports random access.
00362 
00363         If False, seek(), tell() and truncate() will raise UnsupportedOperation.
00364         This method may need to do a test seek().
00365         """
00366         return False

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase.tell (   self)
Return an int indicating the current stream position.

Reimplemented in _pyio.TextIOWrapper, _pyio.BufferedRandom, _pyio.BufferedWriter, _pyio.BufferedReader, _pyio.BytesIO, and _pyio._BufferedIOMixin.

Definition at line 312 of file _pyio.py.

00312 
00313     def tell(self):
00314         """Return an int indicating the current stream position."""
00315         return self.seek(0, 1)

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase.truncate (   self,
  pos = None 
)
Truncate file to size bytes.

Size defaults to the current IO position as reported by tell().  Return
the new size.

Reimplemented in _pyio.TextIOWrapper, _pyio.TextIOBase, _pyio.BufferedRandom, _pyio.BufferedWriter, _pyio.BytesIO, and _pyio._BufferedIOMixin.

Definition at line 316 of file _pyio.py.

00316 
00317     def truncate(self, pos=None):
00318         """Truncate file to size bytes.
00319 
00320         Size defaults to the current IO position as reported by tell().  Return
00321         the new size.
00322         """
00323         self._unsupported("truncate")

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase.writable (   self)
Return a bool indicating whether object was opened for writing.

If False, write() and truncate() will raise UnsupportedOperation.

Reimplemented in _pyio.TextIOWrapper, _pyio.BufferedRWPair, _pyio.BytesIO, and _pyio._BufferedIOMixin.

Definition at line 388 of file _pyio.py.

00388 
00389     def writable(self):
00390         """Return a bool indicating whether object was opened for writing.
00391 
00392         If False, write() and truncate() will raise UnsupportedOperation.
00393         """
00394         return False

Here is the call graph for this function:

Here is the caller graph for this function:

def _pyio.IOBase.writelines (   self,
  lines 
)

Definition at line 514 of file _pyio.py.

00514 
00515     def writelines(self, lines):
00516         self._checkClosed()
00517         for line in lines:
00518             self.write(line)
00519 
00520 io.IOBase.register(IOBase)
00521 


Member Data Documentation

_pyio.IOBase.__closed = False [static, private]

Definition at line 334 of file _pyio.py.


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