2000-03-10 19:20:43 -04:00
|
|
|
""" codecs -- Python Codec Registry, API and helpers.
|
|
|
|
|
|
|
|
|
|
|
|
Written by Marc-Andre Lemburg (mal@lemburg.com).
|
|
|
|
|
|
|
|
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
|
|
|
|
|
|
|
|
"""#"
|
|
|
|
|
2001-08-02 04:15:29 -03:00
|
|
|
import struct, __builtin__
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
### Registry and builtin stateless codec functions
|
|
|
|
|
2000-03-31 13:25:23 -04:00
|
|
|
try:
|
|
|
|
from _codecs import *
|
2001-05-15 14:19:16 -03:00
|
|
|
except ImportError, why:
|
2000-03-31 13:25:23 -04:00
|
|
|
raise SystemError,\
|
|
|
|
'Failed to load the builtin codecs: %s' % why
|
2000-03-10 19:20:43 -04:00
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
__all__ = ["register", "lookup", "open", "EncodedFile", "BOM", "BOM_BE",
|
|
|
|
"BOM_LE", "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE"]
|
2001-01-20 15:54:20 -04:00
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
### Constants
|
|
|
|
|
|
|
|
#
|
|
|
|
# Byte Order Mark (BOM) and its possible values (BOM_BE, BOM_LE)
|
|
|
|
#
|
2001-05-15 14:19:16 -03:00
|
|
|
BOM = struct.pack('=H', 0xFEFF)
|
2000-03-10 19:20:43 -04:00
|
|
|
#
|
|
|
|
BOM_BE = BOM32_BE = '\376\377'
|
2001-01-14 19:36:06 -04:00
|
|
|
# corresponds to Unicode U+FEFF in UTF-16 on big endian
|
|
|
|
# platforms == ZERO WIDTH NO-BREAK SPACE
|
2000-04-11 12:41:38 -03:00
|
|
|
BOM_LE = BOM32_LE = '\377\376'
|
2001-01-14 19:36:06 -04:00
|
|
|
# corresponds to Unicode U+FFFE in UTF-16 on little endian
|
|
|
|
# platforms == defined as being an illegal Unicode character
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# 64-bit Byte Order Marks
|
|
|
|
#
|
|
|
|
BOM64_BE = '\000\000\376\377'
|
2001-01-14 19:36:06 -04:00
|
|
|
# corresponds to Unicode U+0000FEFF in UCS-4
|
2000-03-10 19:20:43 -04:00
|
|
|
BOM64_LE = '\377\376\000\000'
|
2001-01-14 19:36:06 -04:00
|
|
|
# corresponds to Unicode U+0000FFFE in UCS-4
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
### Codec base classes (defining the API)
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
2000-03-24 18:14:19 -04:00
|
|
|
'strict' - raise a ValueError error (or a subclass)
|
2000-03-10 19:20:43 -04:00
|
|
|
'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.
|
|
|
|
|
|
|
|
"""
|
2001-05-15 14:19:16 -03:00
|
|
|
def encode(self, input, errors='strict'):
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-03-17 11:40:35 -04:00
|
|
|
""" Encodes the object input and returns a tuple (output
|
2000-03-10 19:20:43 -04:00
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def decode(self, input, errors='strict'):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
""" 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.
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
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.
|
|
|
|
|
2000-04-11 12:41:38 -03:00
|
|
|
"""
|
2000-03-10 19:20:43 -04:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
#
|
|
|
|
# The StreamWriter and StreamReader class provide generic working
|
2001-09-18 17:29:48 -03:00
|
|
|
# interfaces which can be used to implement new encoding submodules
|
2000-03-10 19:20:43 -04:00
|
|
|
# very easily. See encodings/utf_8.py for an example on how this is
|
|
|
|
# done.
|
2000-04-11 12:41:38 -03:00
|
|
|
#
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
class StreamWriter(Codec):
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def __init__(self, stream, errors='strict'):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
""" 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
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.stream = stream
|
|
|
|
self.errors = errors
|
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
def write(self, object):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
""" Writes the object's contents encoded to self.stream.
|
|
|
|
"""
|
2001-05-15 14:19:16 -03:00
|
|
|
data, consumed = self.encode(object, self.errors)
|
2000-03-10 19:20:43 -04:00
|
|
|
self.stream.write(data)
|
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
def writelines(self, list):
|
|
|
|
|
|
|
|
""" Writes the concatenated list of strings to the stream
|
|
|
|
using .write().
|
|
|
|
"""
|
|
|
|
self.write(''.join(list))
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
def 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def __getattr__(self, name,
|
2000-03-10 19:20:43 -04:00
|
|
|
getattr=getattr):
|
|
|
|
|
|
|
|
""" Inherit all other methods from the underlying stream.
|
|
|
|
"""
|
2001-05-15 14:19:16 -03:00
|
|
|
return getattr(self.stream, name)
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
class StreamReader(Codec):
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def __init__(self, stream, errors='strict'):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
""" 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;
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.stream = stream
|
|
|
|
self.errors = errors
|
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
def read(self, size=-1):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
""" 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Unsliced reading:
|
|
|
|
if size < 0:
|
2000-12-10 11:12:14 -04:00
|
|
|
return self.decode(self.stream.read(), self.errors)[0]
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
# Sliced reading:
|
|
|
|
read = self.stream.read
|
|
|
|
decode = self.decode
|
|
|
|
data = read(size)
|
|
|
|
i = 0
|
|
|
|
while 1:
|
|
|
|
try:
|
2000-12-10 11:12:14 -04:00
|
|
|
object, decodedbytes = decode(data, self.errors)
|
2001-05-15 14:19:16 -03:00
|
|
|
except ValueError, why:
|
2000-03-10 19:20:43 -04:00
|
|
|
# This method is slow but should work under pretty much
|
|
|
|
# all conditions; at most 10 tries are made
|
|
|
|
i = i + 1
|
|
|
|
newdata = read(1)
|
|
|
|
if not newdata or i > 10:
|
|
|
|
raise
|
|
|
|
data = data + newdata
|
|
|
|
else:
|
|
|
|
return object
|
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
def readline(self, size=None):
|
|
|
|
|
|
|
|
""" Read one line from the input stream and return the
|
|
|
|
decoded data.
|
|
|
|
|
2000-04-13 11:11:21 -03:00
|
|
|
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.
|
2000-04-11 12:37:43 -03:00
|
|
|
|
|
|
|
size, if given, is passed as size argument to the stream's
|
|
|
|
.readline() method.
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
"""
|
|
|
|
if size is None:
|
|
|
|
line = self.stream.readline()
|
|
|
|
else:
|
|
|
|
line = self.stream.readline(size)
|
2001-05-15 14:19:16 -03:00
|
|
|
return self.decode(line, self.errors)[0]
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
|
2002-03-05 11:46:38 -04:00
|
|
|
def readlines(self, sizehint=None):
|
2000-04-11 12:37:43 -03:00
|
|
|
|
|
|
|
""" 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.
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
sizehint, if given, is passed as size argument to the
|
|
|
|
stream's .read() method.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if sizehint is None:
|
|
|
|
data = self.stream.read()
|
|
|
|
else:
|
|
|
|
data = self.stream.read(sizehint)
|
2001-05-15 14:19:16 -03:00
|
|
|
return self.decode(data, self.errors)[0].splitlines(1)
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
|
|
|
|
""" Resets the codec buffers used for keeping state.
|
|
|
|
|
|
|
|
Note that no stream repositioning should take place.
|
2000-07-16 09:04:32 -03:00
|
|
|
This method is primarily intended to be able to recover
|
2000-03-10 19:20:43 -04:00
|
|
|
from decoding errors.
|
|
|
|
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def __getattr__(self, name,
|
2000-03-10 19:20:43 -04:00
|
|
|
getattr=getattr):
|
|
|
|
|
|
|
|
""" Inherit all other methods from the underlying stream.
|
|
|
|
"""
|
2001-05-15 14:19:16 -03:00
|
|
|
return getattr(self.stream, name)
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
class StreamReaderWriter:
|
|
|
|
|
2000-04-13 11:11:21 -03:00
|
|
|
""" StreamReaderWriter instances allow wrapping streams which
|
|
|
|
work in both read and write modes.
|
|
|
|
|
|
|
|
The design is such that one can use the factory functions
|
2000-07-16 09:04:32 -03:00
|
|
|
returned by the codec.lookup() function to construct the
|
2000-04-13 11:11:21 -03:00
|
|
|
instance.
|
|
|
|
|
|
|
|
"""
|
2000-04-11 12:37:43 -03:00
|
|
|
# Optional attributes set by the file wrappers below
|
|
|
|
encoding = 'unknown'
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def __init__(self, stream, Reader, Writer, errors='strict'):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
""" 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.stream = stream
|
|
|
|
self.reader = Reader(stream, errors)
|
|
|
|
self.writer = Writer(stream, errors)
|
|
|
|
self.errors = errors
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def read(self, size=-1):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
return self.reader.read(size)
|
|
|
|
|
2000-05-01 13:17:32 -03:00
|
|
|
def readline(self, size=None):
|
2000-04-11 12:37:43 -03:00
|
|
|
|
|
|
|
return self.reader.readline(size)
|
|
|
|
|
2000-05-01 13:17:32 -03:00
|
|
|
def readlines(self, sizehint=None):
|
2000-04-11 12:37:43 -03:00
|
|
|
|
|
|
|
return self.reader.readlines(sizehint)
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def write(self, data):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
return self.writer.write(data)
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def writelines(self, list):
|
2000-04-11 12:37:43 -03:00
|
|
|
|
|
|
|
return self.writer.writelines(list)
|
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
def reset(self):
|
|
|
|
|
|
|
|
self.reader.reset()
|
|
|
|
self.writer.reset()
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def __getattr__(self, name,
|
2000-03-10 19:20:43 -04:00
|
|
|
getattr=getattr):
|
|
|
|
|
|
|
|
""" Inherit all other methods from the underlying stream.
|
|
|
|
"""
|
2001-05-15 14:19:16 -03:00
|
|
|
return getattr(self.stream, name)
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
class StreamRecoder:
|
|
|
|
|
2000-04-13 11:11:21 -03:00
|
|
|
""" 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.
|
|
|
|
|
|
|
|
"""
|
2000-04-11 12:37:43 -03:00
|
|
|
# Optional attributes set by the file wrappers below
|
|
|
|
data_encoding = 'unknown'
|
|
|
|
file_encoding = 'unknown'
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def __init__(self, stream, encode, decode, Reader, Writer,
|
|
|
|
errors='strict'):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
""" Creates a StreamRecoder instance which implements a two-way
|
|
|
|
conversion: encode and decode work on the frontend (the
|
2000-04-11 12:41:38 -03:00
|
|
|
input to .read() and output of .write()) while
|
2000-03-10 19:20:43 -04:00
|
|
|
Reader and Writer work on the backend (reading and
|
2000-03-17 11:42:11 -04:00
|
|
|
writing to the stream).
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.stream = stream
|
|
|
|
self.encode = encode
|
|
|
|
self.decode = decode
|
|
|
|
self.reader = Reader(stream, errors)
|
|
|
|
self.writer = Writer(stream, errors)
|
|
|
|
self.errors = errors
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def read(self, size=-1):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
data = self.reader.read(size)
|
|
|
|
data, bytesencoded = self.encode(data, self.errors)
|
|
|
|
return data
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def readline(self, size=None):
|
2000-04-11 12:37:43 -03:00
|
|
|
|
|
|
|
if size is None:
|
|
|
|
data = self.reader.readline()
|
|
|
|
else:
|
|
|
|
data = self.reader.readline(size)
|
|
|
|
data, bytesencoded = self.encode(data, self.errors)
|
|
|
|
return data
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def readlines(self, sizehint=None):
|
2000-04-11 12:37:43 -03:00
|
|
|
|
|
|
|
if sizehint is None:
|
|
|
|
data = self.reader.read()
|
|
|
|
else:
|
|
|
|
data = self.reader.read(sizehint)
|
|
|
|
data, bytesencoded = self.encode(data, self.errors)
|
|
|
|
return data.splitlines(1)
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def write(self, data):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
data, bytesdecoded = self.decode(data, self.errors)
|
|
|
|
return self.writer.write(data)
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def writelines(self, list):
|
2000-04-11 12:37:43 -03:00
|
|
|
|
|
|
|
data = ''.join(list)
|
|
|
|
data, bytesdecoded = self.decode(data, self.errors)
|
|
|
|
return self.writer.write(data)
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
|
|
|
|
self.reader.reset()
|
|
|
|
self.writer.reset()
|
|
|
|
|
2001-05-15 14:19:16 -03:00
|
|
|
def __getattr__(self, name,
|
2000-03-10 19:20:43 -04:00
|
|
|
getattr=getattr):
|
|
|
|
|
|
|
|
""" Inherit all other methods from the underlying stream.
|
|
|
|
"""
|
2001-05-15 14:19:16 -03:00
|
|
|
return getattr(self.stream, name)
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
### Shortcuts
|
|
|
|
|
2000-06-21 18:21:04 -03:00
|
|
|
def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
""" 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 by
|
|
|
|
Unicode as well.
|
|
|
|
|
2000-06-21 18:21:04 -03:00
|
|
|
Files are always opened in binary mode, even if no binary mode
|
|
|
|
was specified. Thisis 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.
|
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
encoding specifies the encoding which is to be used for the
|
|
|
|
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.
|
|
|
|
|
2000-04-13 11:11:21 -03:00
|
|
|
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.
|
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
"""
|
|
|
|
if encoding is not None and \
|
|
|
|
'b' not in mode:
|
|
|
|
# Force opening of the file in binary mode
|
|
|
|
mode = mode + 'b'
|
|
|
|
file = __builtin__.open(filename, mode, buffering)
|
|
|
|
if encoding is None:
|
|
|
|
return file
|
2001-05-15 14:19:16 -03:00
|
|
|
(e, d, sr, sw) = lookup(encoding)
|
2000-04-11 12:37:43 -03:00
|
|
|
srw = StreamReaderWriter(file, sr, sw, errors)
|
|
|
|
# Add attributes to simplify introspection
|
|
|
|
srw.encoding = encoding
|
|
|
|
return srw
|
2000-03-10 19:20:43 -04:00
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
""" Return a wrapped version of file which provides transparent
|
|
|
|
encoding translation.
|
|
|
|
|
|
|
|
Strings written to the wrapped file are interpreted according
|
2000-04-11 12:37:43 -03:00
|
|
|
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.
|
2000-03-10 19:20:43 -04:00
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
If file_encoding is not given, it defaults to data_encoding.
|
2000-03-10 19:20:43 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2000-04-13 11:11:21 -03:00
|
|
|
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.
|
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
"""
|
2000-04-11 12:37:43 -03:00
|
|
|
if file_encoding is None:
|
|
|
|
file_encoding = data_encoding
|
|
|
|
encode, decode = lookup(data_encoding)[:2]
|
|
|
|
Reader, Writer = lookup(file_encoding)[2:]
|
|
|
|
sr = StreamRecoder(file,
|
2001-05-15 14:19:16 -03:00
|
|
|
encode, decode, Reader, Writer,
|
2000-04-11 12:37:43 -03:00
|
|
|
errors)
|
|
|
|
# Add attributes to simplify introspection
|
|
|
|
sr.data_encoding = data_encoding
|
|
|
|
sr.file_encoding = file_encoding
|
|
|
|
return sr
|
2000-03-10 19:20:43 -04:00
|
|
|
|
2001-09-19 08:24:48 -03:00
|
|
|
### Helpers for codec lookup
|
|
|
|
|
|
|
|
def 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return lookup(encoding)[0]
|
|
|
|
|
|
|
|
def 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return lookup(encoding)[1]
|
|
|
|
|
|
|
|
def 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return lookup(encoding)[2]
|
|
|
|
|
|
|
|
def 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return lookup(encoding)[3]
|
|
|
|
|
2001-01-03 17:29:14 -04:00
|
|
|
### Helpers for charmap-based codecs
|
|
|
|
|
|
|
|
def make_identity_dict(rng):
|
|
|
|
|
|
|
|
""" make_identity_dict(rng) -> dict
|
|
|
|
|
|
|
|
Return a dictionary where elements of the rng sequence are
|
|
|
|
mapped to themselves.
|
2001-01-14 19:36:06 -04:00
|
|
|
|
2001-01-03 17:29:14 -04:00
|
|
|
"""
|
|
|
|
res = {}
|
|
|
|
for i in rng:
|
|
|
|
res[i]=i
|
|
|
|
return res
|
|
|
|
|
2001-05-16 06:41:45 -03:00
|
|
|
def make_encoding_map(decoding_map):
|
|
|
|
|
|
|
|
""" Creates an encoding map from a decoding map.
|
|
|
|
|
|
|
|
If a target mapping in the decoding map occurrs multiple
|
|
|
|
times, then that target is mapped to None (undefined mapping),
|
|
|
|
causing an exception when encountered by the charmap codec
|
|
|
|
during translation.
|
|
|
|
|
|
|
|
One example where this happens is cp875.py which decodes
|
|
|
|
multiple character to \u001a.
|
|
|
|
|
|
|
|
"""
|
|
|
|
m = {}
|
|
|
|
for k,v in decoding_map.items():
|
|
|
|
if not m.has_key(v):
|
|
|
|
m[v] = k
|
|
|
|
else:
|
|
|
|
m[v] = None
|
|
|
|
return m
|
2001-05-29 03:06:54 -03:00
|
|
|
|
2001-07-31 05:54:55 -03:00
|
|
|
# Tell modulefinder that using codecs probably needs the encodings
|
|
|
|
# package
|
|
|
|
_false = 0
|
|
|
|
if _false:
|
|
|
|
import encodings
|
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
### Tests
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-03-10 19:20:43 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
import sys
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
# Make stdout translate Latin-1 output into UTF-8 output
|
|
|
|
sys.stdout = EncodedFile(sys.stdout, 'latin-1', 'utf-8')
|
2000-04-11 12:41:38 -03:00
|
|
|
|
2000-04-11 12:37:43 -03:00
|
|
|
# Have stdin translate Latin-1 input into UTF-8 input
|
|
|
|
sys.stdin = EncodedFile(sys.stdin, 'utf-8', 'latin-1')
|