codecs
index
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.py
Module Docs

codecs -- Python Codec Registry, API and helpers.
 
 
Written by Marc-Andre Lemburg (mal@lemburg.com).
 
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.

 
Modules
       
__builtin__
sys

 
Classes
       
__builtin__.object
IncrementalDecoder
IncrementalEncoder
__builtin__.tuple(__builtin__.object)
CodecInfo
Codec
StreamReader
StreamWriter
StreamReaderWriter
StreamRecoder

 
class Codec
    Defines the interface for stateless encoders/decoders.
 
The .encode()/.decode() methods may use different error
handling schemes by providing the errors argument. These
string values are predefined:
 
 '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 on
            decoding and '?' on encoding.
 'xmlcharrefreplace' - Replace with the appropriate XML
                       character reference (only for encoding).
 'backslashreplace'  - Replace with backslashed escape sequences
                       (only for encoding).
 
The set of allowed values can be extended via register_error.
 
  Methods defined here:
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 CodecInfo(__builtin__.tuple)
    
Method resolution order:
CodecInfo
__builtin__.tuple
__builtin__.object

Methods defined here:
__repr__(self)

Static methods defined here:
__new__(cls, encode, decode, streamreader=None, streamwriter=None, incrementalencoder=None, incrementaldecoder=None, name=None)

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)

Methods inherited from __builtin__.tuple:
__add__(...)
x.__add__(y) <==> x+y
__contains__(...)
x.__contains__(y) <==> y in x
__eq__(...)
x.__eq__(y) <==> x==y
__ge__(...)
x.__ge__(y) <==> x>=y
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getnewargs__(...)
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__gt__(...)
x.__gt__(y) <==> x>y
__hash__(...)
x.__hash__() <==> hash(x)
__iter__(...)
x.__iter__() <==> iter(x)
__le__(...)
x.__le__(y) <==> x<=y
__len__(...)
x.__len__() <==> len(x)
__lt__(...)
x.__lt__(y) <==> x<y
__mul__(...)
x.__mul__(n) <==> x*n
__ne__(...)
x.__ne__(y) <==> x!=y
__rmul__(...)
x.__rmul__(n) <==> n*x
count(...)
T.count(value) -> integer -- return number of occurrences of value
index(...)
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.

 
class IncrementalDecoder(__builtin__.object)
    An IncrementalDecoder decodes an input in multiple steps. The input can be
passed piece by piece to the decode() method. The IncrementalDecoder
remembers the state of the decoding process between calls to decode().
 
  Methods defined here:
__init__(self, errors='strict')
Creates a IncrementalDecoder instance.
 
The IncrementalDecoder may use different error handling schemes by
providing the errors keyword argument. See the module docstring
for a list of possible values.
decode(self, input, final=False)
Decodes input and returns the resulting object.
getstate(self)
Return the current state of the decoder.
 
This must be a (buffered_input, additional_state_info) tuple.
buffered_input must be a bytes object containing bytes that
were passed to decode() that have not yet been converted.
additional_state_info must be a non-negative integer
representing the state of the decoder WITHOUT yet having
processed the contents of buffered_input.  In the initial state
and after reset(), getstate() must return (b"", 0).
reset(self)
Resets the decoder to the initial state.
setstate(self, state)
Set the current state of the decoder.
 
state must have been returned by getstate().  The effect of
setstate((b"", 0)) must be equivalent to reset().

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class IncrementalEncoder(__builtin__.object)
    An IncrementalEncoder encodes an input in multiple steps. The input can be
passed piece by piece to the encode() method. The IncrementalEncoder remembers
the state of the Encoding process between calls to encode().
 
  Methods defined here:
__init__(self, errors='strict')
Creates an IncrementalEncoder instance.
 
The IncrementalEncoder may use different error handling schemes by
providing the errors keyword argument. See the module docstring
for a list of possible values.
encode(self, input, final=False)
Encodes input and returns the resulting object.
getstate(self)
Return the current state of the encoder.
reset(self)
Resets the encoder to the initial state.
setstate(self, state)
Set the current state of the encoder. state must have been
returned by getstate().

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class StreamReader(Codec)
     Methods defined here:
__enter__(self)
__exit__(self, type, value, tb)
__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 use different error handling
schemes by providing the errors keyword argument. These
parameters are predefined:
 
 'strict' - raise a ValueError (or a subclass)
 'ignore' - ignore the character and continue with the next
 'replace'- replace with a suitable replacement character;
 
The set of allowed parameter values can be extended via
register_error.
__iter__(self)
decode(self, input, errors='strict')
next(self)
Return the next decoded line from the input stream.
read(self, size=-1, chars=-1, firstline=False)
Decodes data from the stream self.stream and returns the
resulting object.
 
chars indicates the number of characters to read from the
stream. read() will never return more than chars
characters, but it might return less, if there are not enough
characters available.
 
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.
 
If firstline is true, and a UnicodeDecodeError happens
after the first line terminator in the input only the first line
will be returned, the rest of the input will be kept until the
next call to read().
 
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, keepends=True)
Read one line from the input stream and return the
decoded data.
 
size, if given, is passed as size argument to the
read() method.
readlines(self, sizehint=None, keepends=True)
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 ignored since there is no efficient
way to finding the true end-of-line.
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.
seek(self, offset, whence=0)
Set the input stream's current position.
 
Resets the codec buffers used for keeping state.

Methods inherited from Codec:
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 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.
 
  Methods defined here:
__enter__(self)
__exit__(self, type, value, tb)
__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 StreamReaderStreamWriter interface resp.
 
Error handling is done in the same way as defined for the
StreamWriter/Readers.
__iter__(self)
next(self)
Return the next decoded line from the input stream.
read(self, size=-1)
readline(self, size=None)
readlines(self, sizehint=None)
reset(self)
seek(self, offset, whence=0)
write(self, data)
writelines(self, list)

Data and other attributes defined here:
encoding = 'unknown'

 
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.
 
  Methods defined here:
__enter__(self)
__exit__(self, type, value, tb)
__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
StreamReaderStreamWriter 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.
__iter__(self)
next(self)
Return the next decoded line from the input stream.
read(self, size=-1)
readline(self, size=None)
readlines(self, sizehint=None)
reset(self)
write(self, data)
writelines(self, list)

Data and other attributes defined here:
data_encoding = 'unknown'
file_encoding = 'unknown'

 
class StreamWriter(Codec)
     Methods defined here:
__enter__(self)
__exit__(self, type, value, tb)
__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 use different error handling
schemes by providing the errors keyword argument. These
parameters are predefined:
 
 'strict' - raise a ValueError (or a subclass)
 'ignore' - ignore the character and continue with the next
 'replace'- replace with a suitable replacement character
 'xmlcharrefreplace' - Replace with the appropriate XML
                       character reference.
 'backslashreplace'  - Replace with backslashed escape
                       sequences (only for encoding).
 
The set of allowed parameter values can be extended via
register_error.
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.
seek(self, offset, whence=0)
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().

Methods inherited from Codec:
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.

 
Functions
       
EncodedFile(file, data_encoding, file_encoding=None, errors='strict')
Return a wrapped version of file which provides transparent
encoding translation.
 
Strings written to the wrapped file are interpreted according
to the given data_encoding and then written to the original
file as string using file_encoding. The intermediate encoding
will usually be Unicode but depends on the specified codecs.
 
Strings are read from the file using file_encoding and then
passed back to the caller as string using data_encoding.
 
If file_encoding is not given, it defaults to data_encoding.
 
errors may be given to define the error handling. It defaults
to 'strict' which causes ValueErrors to be raised in case an
encoding error occurs.
 
The returned wrapped file object provides two extra attributes
.data_encoding and .file_encoding which reflect the given
parameters of the same name. The attributes can be used for
introspection by Python programs.
backslashreplace_errors(...)
Implements the 'backslashreplace' error handling, which replaces an unencodable character with a backslashed escape sequence.
decode(...)
decode(obj, [encoding[,errors]]) -> object
 
Decodes obj using the codec registered for encoding. encoding defaults
to the default encoding. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a ValueError. Other possible values are 'ignore' and 'replace'
as well as any other name registered with codecs.register_error that is
able to handle ValueErrors.
encode(...)
encode(obj, [encoding[,errors]]) -> object
 
Encodes obj using the codec registered for encoding. encoding defaults
to the default encoding. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a ValueError. Other possible values are 'ignore', 'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that can handle ValueErrors.
getdecoder(encoding)
Lookup up the codec for the given encoding and return
its decoder function.
 
Raises a LookupError in case the encoding cannot be found.
getencoder(encoding)
Lookup up the codec for the given encoding and return
its encoder function.
 
Raises a LookupError in case the encoding cannot be found.
getincrementaldecoder(encoding)
Lookup up the codec for the given encoding and return
its IncrementalDecoder class or factory function.
 
Raises a LookupError in case the encoding cannot be found
or the codecs doesn't provide an incremental decoder.
getincrementalencoder(encoding)
Lookup up the codec for the given encoding and return
its IncrementalEncoder class or factory function.
 
Raises a LookupError in case the encoding cannot be found
or the codecs doesn't provide an incremental encoder.
getreader(encoding)
Lookup up the codec for the given encoding and return
its StreamReader class or factory function.
 
Raises a LookupError in case the encoding cannot be found.
getwriter(encoding)
Lookup up the codec for the given encoding and return
its StreamWriter class or factory function.
 
Raises a LookupError in case the encoding cannot be found.
ignore_errors(...)
Implements the 'ignore' error handling, which ignores malformed data and continues.
iterdecode(iterator, encoding, errors='strict', **kwargs)
Decoding iterator.
 
Decodes the input strings from the iterator using a IncrementalDecoder.
 
errors and kwargs are passed through to the IncrementalDecoder
constructor.
iterencode(iterator, encoding, errors='strict', **kwargs)
Encoding iterator.
 
Encodes the input strings from the iterator using a IncrementalEncoder.
 
errors and kwargs are passed through to the IncrementalEncoder
constructor.
lookup(...)
lookup(encoding) -> CodecInfo
 
Looks up a codec tuple in the Python codec registry and returns
CodecInfo object.
lookup_error(...)
lookup_error(errors) -> handler
 
Return the error handler for the specified error handling name
or raise a LookupError, if no handler exists under this name.
open(filename, mode='rb', encoding=None, errors='strict', buffering=1)
Open an encoded file using the given mode and return
a wrapped version providing transparent encoding/decoding.
 
Note: The wrapped version will only accept the object format
defined by the codecs, i.e. Unicode objects for most builtin
codecs. Output is also codec dependent and will usually be
Unicode as well.
 
Files are always opened in binary mode, even if no binary mode
was specified. This is done to avoid data loss due to encodings
using 8-bit values. The default file mode is 'rb' meaning to
open the file in binary read mode.
 
encoding specifies the encoding which is to be used for the
file.
 
errors may be given to define the error handling. It defaults
to 'strict' which causes ValueErrors to be raised in case an
encoding error occurs.
 
buffering has the same meaning as for the builtin open() API.
It defaults to line buffered.
 
The returned wrapped file object provides an extra attribute
.encoding which allows querying the used encoding. This
attribute is only available if an encoding was specified as
parameter.
register(...)
register(search_function)
 
Register a codec search function. Search functions are expected to take
one argument, the encoding name in all lower case letters, and return
tuple of functions (encoder, decoder, stream_reader, stream_writer)
(or a CodecInfo object).
register_error(...)
register_error(errors, handler)
 
Register the specified error handler under the name
errors. handler must be a callable object, that
will be called with an exception instance containing
information about the location of the encoding/decoding
error and must return a (replacement, new position) tuple.
replace_errors(...)
Implements the 'replace' error handling, which replaces malformed data with a replacement marker.
strict_errors(...)
Implements the 'strict' error handling, which raises a UnicodeError on coding errors.
xmlcharrefreplace_errors(...)
Implements the 'xmlcharrefreplace' error handling, which replaces an unencodable character with the appropriate XML character reference.

 
Data
        BOM = '\xff\xfe'
BOM32_BE = '\xfe\xff'
BOM32_LE = '\xff\xfe'
BOM64_BE = '\x00\x00\xfe\xff'
BOM64_LE = '\xff\xfe\x00\x00'
BOM_BE = '\xfe\xff'
BOM_LE = '\xff\xfe'
BOM_UTF16 = '\xff\xfe'
BOM_UTF16_BE = '\xfe\xff'
BOM_UTF16_LE = '\xff\xfe'
BOM_UTF32 = '\xff\xfe\x00\x00'
BOM_UTF32_BE = '\x00\x00\xfe\xff'
BOM_UTF32_LE = '\xff\xfe\x00\x00'
BOM_UTF8 = '\xef\xbb\xbf'
__all__ = ['register', 'lookup', 'open', 'EncodedFile', 'BOM', 'BOM_BE', 'BOM_LE', 'BOM32_BE', 'BOM32_LE', 'BOM64_BE', 'BOM64_LE', 'BOM_UTF8', 'BOM_UTF16', 'BOM_UTF16_LE', 'BOM_UTF16_BE', 'BOM_UTF32', 'BOM_UTF32_LE', 'BOM_UTF32_BE', 'CodecInfo', 'Codec', ...]