| | |
- Codec
-
- StreamReader
- StreamWriter
- StreamReaderWriter
- StreamRecoder
class Codec |
| |
Defines the interface for stateless encoders/decoders.
The .encode()/.decode() methods may implement different error
handling schemes by providing the errors argument. These
string values are defined:
'strict' - raise a ValueError error (or a subclass)
'ignore' - ignore the character and continue with the next
'replace' - replace with a suitable replacement character;
Python will use the official U+FFFD REPLACEMENT
CHARACTER for the builtin Unicode codecs. |
| |
- decode(self, input, errors='strict')
- Decodes the object input and returns a tuple (output
object, length consumed).
input must be an object which provides the bf_getreadbuf
buffer slot. Python strings, buffer objects and memory
mapped files are examples of objects providing this slot.
errors defines the error handling to apply. It defaults to
'strict' handling.
The method may not store state in the Codec instance. Use
StreamCodec for codecs which have to keep state in order to
make encoding/decoding efficient.
The decoder must be able to handle zero length input and
return an empty object of the output object type in this
situation.
- encode(self, input, errors='strict')
- Encodes the object input and returns a tuple (output
object, length consumed).
errors defines the error handling to apply. It defaults to
'strict' handling.
The method may not store state in the Codec instance. Use
StreamCodec for codecs which have to keep state in order to
make encoding/decoding efficient.
The encoder must be able to handle zero length input and
return an empty object of the output object type in this
situation.
|
class StreamReader(Codec) |
| |
|
| |
- __getattr__(self, name, getattr=<built-in function getattr>)
- Inherit all other methods from the underlying stream.
- __init__(self, stream, errors='strict')
- Creates a StreamReader instance.
stream must be a file-like object open for reading
(binary) data.
The StreamReader may implement different error handling
schemes by providing the errors keyword argument. These
parameters are defined:
'strict' - raise a ValueError (or a subclass)
'ignore' - ignore the character and continue with the next
'replace'- replace with a suitable replacement character;
- decode(self, input, errors='strict') from Codec
- encode(self, input, errors='strict') from Codec
- read(self, size=-1)
- Decodes data from the stream self.stream and returns the
resulting object.
size indicates the approximate maximum number of bytes to
read from the stream for decoding purposes. The decoder
can modify this setting as appropriate. The default value
-1 indicates to read and decode as much as possible. size
is intended to prevent having to decode huge files in one
step.
The method should use a greedy read strategy meaning that
it should read as much data as is allowed within the
definition of the encoding and the given size, e.g. if
optional encoding endings or state markers are available
on the stream, these should be read too.
- readline(self, size=None)
- Read one line from the input stream and return the
decoded data.
Note: Unlike the .readlines() method, this method inherits
the line breaking knowledge from the underlying stream's
.readline() method -- there is currently no support for
line breaking using the codec decoder due to lack of line
buffering. Sublcasses should however, if possible, try to
implement this method using their own knowledge of line
breaking.
size, if given, is passed as size argument to the stream's
.readline() method.
- readlines(self, sizehint=0)
- Read all lines available on the input stream
and return them as list of lines.
Line breaks are implemented using the codec's decoder
method and are included in the list entries.
sizehint, if given, is passed as size argument to the
stream's .read() method.
- reset(self)
- Resets the codec buffers used for keeping state.
Note that no stream repositioning should take place.
This method is primarily intended to be able to recover
from decoding errors.
|
class StreamReaderWriter |
| |
StreamReaderWriter instances allow wrapping streams which
work in both read and write modes.
The design is such that one can use the factory functions
returned by the codec.lookup() function to construct the
instance. |
| |
- __getattr__(self, name, getattr=<built-in function getattr>)
- Inherit all other methods from the underlying stream.
- __init__(self, stream, Reader, Writer, errors='strict')
- Creates a StreamReaderWriter instance.
stream must be a Stream-like object.
Reader, Writer must be factory functions or classes
providing the StreamReader, StreamWriter interface resp.
Error handling is done in the same way as defined for the
StreamWriter/Readers.
- read(self, size=-1)
- readline(self, size=None)
- readlines(self, sizehint=None)
- reset(self)
- write(self, data)
- writelines(self, list)
|
class StreamRecoder |
| |
StreamRecoder instances provide a frontend - backend
view of encoding data.
They use the complete set of APIs returned by the
codecs.lookup() function to implement their task.
Data written to the stream is first decoded into an
intermediate format (which is dependent on the given codec
combination) and then written to the stream using an instance
of the provided Writer class.
In the other direction, data is read from the stream using a
Reader instance and then return encoded data to the caller. |
| |
- __getattr__(self, name, getattr=<built-in function getattr>)
- Inherit all other methods from the underlying stream.
- __init__(self, stream, encode, decode, Reader, Writer, errors='strict')
- Creates a StreamRecoder instance which implements a two-way
conversion: encode and decode work on the frontend (the
input to .read() and output of .write()) while
Reader and Writer work on the backend (reading and
writing to the stream).
You can use these objects to do transparent direct
recodings from e.g. latin-1 to utf-8 and back.
stream must be a file-like object.
encode, decode must adhere to the Codec interface, Reader,
Writer must be factory functions or classes providing the
StreamReader, StreamWriter interface resp.
encode and decode are needed for the frontend translation,
Reader and Writer for the backend translation. Unicode is
used as intermediate encoding.
Error handling is done in the same way as defined for the
StreamWriter/Readers.
- read(self, size=-1)
- readline(self, size=None)
- readlines(self, sizehint=None)
- reset(self)
- write(self, data)
- writelines(self, list)
|
class StreamWriter(Codec) |
| |
|
| |
- __getattr__(self, name, getattr=<built-in function getattr>)
- Inherit all other methods from the underlying stream.
- __init__(self, stream, errors='strict')
- Creates a StreamWriter instance.
stream must be a file-like object open for writing
(binary) data.
The StreamWriter may implement different error handling
schemes by providing the errors keyword argument. These
parameters are defined:
'strict' - raise a ValueError (or a subclass)
'ignore' - ignore the character and continue with the next
'replace'- replace with a suitable replacement character
- decode(self, input, errors='strict') from Codec
- encode(self, input, errors='strict') from Codec
- reset(self)
- Flushes and resets the codec buffers used for keeping state.
Calling this method should ensure that the data on the
output is put into a clean state, that allows appending
of new fresh data without having to rescan the whole
stream to recover state.
- write(self, object)
- Writes the object's contents encoded to self.stream.
- writelines(self, list)
- Writes the concatenated list of strings to the stream
using .write().
| |