📄 codecs.py
字号:
""" codecs -- Python Codec Registry, API and helpers.Written by Marc-Andre Lemburg (mal@lemburg.com).(c) Copyright CNRI, All Rights Reserved. NO WARRANTY."""#"import struct, __builtin__### Registry and builtin stateless codec functionstry: from _codecs import *except ImportError, why: raise SystemError,\ 'Failed to load the builtin codecs: %s' % why__all__ = ["register", "lookup", "open", "EncodedFile", "BOM", "BOM_BE", "BOM_LE", "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE"]### Constants## Byte Order Mark (BOM) and its possible values (BOM_BE, BOM_LE)#BOM = struct.pack('=H', 0xFEFF)#BOM_BE = BOM32_BE = '\376\377'# corresponds to Unicode U+FEFF in UTF-16 on big endian# platforms == ZERO WIDTH NO-BREAK SPACEBOM_LE = BOM32_LE = '\377\376'# corresponds to Unicode U+FFFE in UTF-16 on little endian# platforms == defined as being an illegal Unicode character## 64-bit Byte Order Marks#BOM64_BE = '\000\000\376\377'# corresponds to Unicode U+0000FEFF in UCS-4BOM64_LE = '\377\376\000\000'# corresponds to Unicode U+0000FFFE in UCS-4### 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: '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. """ def 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. """ raise NotImplementedError def 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. """ raise NotImplementedError## The StreamWriter and StreamReader class provide generic working# interfaces which can be used to implement new encoding submodules# very easily. See encodings/utf_8.py for an example on how this is# done.#class StreamWriter(Codec): def __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 """ self.stream = stream self.errors = errors def write(self, object): """ Writes the object's contents encoded to self.stream. """ data, consumed = self.encode(object, self.errors) self.stream.write(data) def writelines(self, list): """ Writes the concatenated list of strings to the stream using .write(). """ self.write(''.join(list)) 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 def __getattr__(self, name, getattr=getattr): """ Inherit all other methods from the underlying stream. """ return getattr(self.stream, name)###class StreamReader(Codec): def __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; """ self.stream = stream self.errors = errors def 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. """ # Unsliced reading: if size < 0: return self.decode(self.stream.read(), self.errors)[0] # Sliced reading: read = self.stream.read decode = self.decode data = read(size) i = 0 while 1: try: object, decodedbytes = decode(data, self.errors) except ValueError, why: # 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 def 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. """ if size is None: line = self.stream.readline() else: line = self.stream.readline(size) return self.decode(line, self.errors)[0] def readlines(self, sizehint=None): """ 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. """ if sizehint is None: data = self.stream.read() else: data = self.stream.read(sizehint) return self.decode(data, self.errors)[0].splitlines(1) def 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. """ pass def __getattr__(self, name, getattr=getattr): """ Inherit all other methods from the underlying stream. """ return getattr(self.stream, name)###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. """ # Optional attributes set by the file wrappers below encoding = 'unknown' def __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. """ self.stream = stream
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -