]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.2/Lib/codecs.py
edk2: Remove AppPkg, StdLib, StdLibPrivateInternalFiles
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / codecs.py
diff --git a/AppPkg/Applications/Python/Python-2.7.2/Lib/codecs.py b/AppPkg/Applications/Python/Python-2.7.2/Lib/codecs.py
deleted file mode 100644 (file)
index c0e95f9..0000000
+++ /dev/null
@@ -1,1098 +0,0 @@
-""" codecs -- Python Codec Registry, API and helpers.\r
-\r
-\r
-Written by Marc-Andre Lemburg (mal@lemburg.com).\r
-\r
-(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.\r
-\r
-"""#"\r
-\r
-import __builtin__, sys\r
-\r
-### Registry and builtin stateless codec functions\r
-\r
-try:\r
-    from _codecs import *\r
-except ImportError, why:\r
-    raise SystemError('Failed to load the builtin codecs: %s' % why)\r
-\r
-__all__ = ["register", "lookup", "open", "EncodedFile", "BOM", "BOM_BE",\r
-           "BOM_LE", "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE",\r
-           "BOM_UTF8", "BOM_UTF16", "BOM_UTF16_LE", "BOM_UTF16_BE",\r
-           "BOM_UTF32", "BOM_UTF32_LE", "BOM_UTF32_BE",\r
-           "strict_errors", "ignore_errors", "replace_errors",\r
-           "xmlcharrefreplace_errors",\r
-           "register_error", "lookup_error"]\r
-\r
-### Constants\r
-\r
-#\r
-# Byte Order Mark (BOM = ZERO WIDTH NO-BREAK SPACE = U+FEFF)\r
-# and its possible byte string values\r
-# for UTF8/UTF16/UTF32 output and little/big endian machines\r
-#\r
-\r
-# UTF-8\r
-BOM_UTF8 = '\xef\xbb\xbf'\r
-\r
-# UTF-16, little endian\r
-BOM_LE = BOM_UTF16_LE = '\xff\xfe'\r
-\r
-# UTF-16, big endian\r
-BOM_BE = BOM_UTF16_BE = '\xfe\xff'\r
-\r
-# UTF-32, little endian\r
-BOM_UTF32_LE = '\xff\xfe\x00\x00'\r
-\r
-# UTF-32, big endian\r
-BOM_UTF32_BE = '\x00\x00\xfe\xff'\r
-\r
-if sys.byteorder == 'little':\r
-\r
-    # UTF-16, native endianness\r
-    BOM = BOM_UTF16 = BOM_UTF16_LE\r
-\r
-    # UTF-32, native endianness\r
-    BOM_UTF32 = BOM_UTF32_LE\r
-\r
-else:\r
-\r
-    # UTF-16, native endianness\r
-    BOM = BOM_UTF16 = BOM_UTF16_BE\r
-\r
-    # UTF-32, native endianness\r
-    BOM_UTF32 = BOM_UTF32_BE\r
-\r
-# Old broken names (don't use in new code)\r
-BOM32_LE = BOM_UTF16_LE\r
-BOM32_BE = BOM_UTF16_BE\r
-BOM64_LE = BOM_UTF32_LE\r
-BOM64_BE = BOM_UTF32_BE\r
-\r
-\r
-### Codec base classes (defining the API)\r
-\r
-class CodecInfo(tuple):\r
-\r
-    def __new__(cls, encode, decode, streamreader=None, streamwriter=None,\r
-        incrementalencoder=None, incrementaldecoder=None, name=None):\r
-        self = tuple.__new__(cls, (encode, decode, streamreader, streamwriter))\r
-        self.name = name\r
-        self.encode = encode\r
-        self.decode = decode\r
-        self.incrementalencoder = incrementalencoder\r
-        self.incrementaldecoder = incrementaldecoder\r
-        self.streamwriter = streamwriter\r
-        self.streamreader = streamreader\r
-        return self\r
-\r
-    def __repr__(self):\r
-        return "<%s.%s object for encoding %s at 0x%x>" % (self.__class__.__module__, self.__class__.__name__, self.name, id(self))\r
-\r
-class Codec:\r
-\r
-    """ Defines the interface for stateless encoders/decoders.\r
-\r
-        The .encode()/.decode() methods may use different error\r
-        handling schemes by providing the errors argument. These\r
-        string values are predefined:\r
-\r
-         'strict' - raise a ValueError error (or a subclass)\r
-         'ignore' - ignore the character and continue with the next\r
-         'replace' - replace with a suitable replacement character;\r
-                    Python will use the official U+FFFD REPLACEMENT\r
-                    CHARACTER for the builtin Unicode codecs on\r
-                    decoding and '?' on encoding.\r
-         'xmlcharrefreplace' - Replace with the appropriate XML\r
-                               character reference (only for encoding).\r
-         'backslashreplace'  - Replace with backslashed escape sequences\r
-                               (only for encoding).\r
-\r
-        The set of allowed values can be extended via register_error.\r
-\r
-    """\r
-    def encode(self, input, errors='strict'):\r
-\r
-        """ Encodes the object input and returns a tuple (output\r
-            object, length consumed).\r
-\r
-            errors defines the error handling to apply. It defaults to\r
-            'strict' handling.\r
-\r
-            The method may not store state in the Codec instance. Use\r
-            StreamCodec for codecs which have to keep state in order to\r
-            make encoding/decoding efficient.\r
-\r
-            The encoder must be able to handle zero length input and\r
-            return an empty object of the output object type in this\r
-            situation.\r
-\r
-        """\r
-        raise NotImplementedError\r
-\r
-    def decode(self, input, errors='strict'):\r
-\r
-        """ Decodes the object input and returns a tuple (output\r
-            object, length consumed).\r
-\r
-            input must be an object which provides the bf_getreadbuf\r
-            buffer slot. Python strings, buffer objects and memory\r
-            mapped files are examples of objects providing this slot.\r
-\r
-            errors defines the error handling to apply. It defaults to\r
-            'strict' handling.\r
-\r
-            The method may not store state in the Codec instance. Use\r
-            StreamCodec for codecs which have to keep state in order to\r
-            make encoding/decoding efficient.\r
-\r
-            The decoder must be able to handle zero length input and\r
-            return an empty object of the output object type in this\r
-            situation.\r
-\r
-        """\r
-        raise NotImplementedError\r
-\r
-class IncrementalEncoder(object):\r
-    """\r
-    An IncrementalEncoder encodes an input in multiple steps. The input can be\r
-    passed piece by piece to the encode() method. The IncrementalEncoder remembers\r
-    the state of the Encoding process between calls to encode().\r
-    """\r
-    def __init__(self, errors='strict'):\r
-        """\r
-        Creates an IncrementalEncoder instance.\r
-\r
-        The IncrementalEncoder may use different error handling schemes by\r
-        providing the errors keyword argument. See the module docstring\r
-        for a list of possible values.\r
-        """\r
-        self.errors = errors\r
-        self.buffer = ""\r
-\r
-    def encode(self, input, final=False):\r
-        """\r
-        Encodes input and returns the resulting object.\r
-        """\r
-        raise NotImplementedError\r
-\r
-    def reset(self):\r
-        """\r
-        Resets the encoder to the initial state.\r
-        """\r
-\r
-    def getstate(self):\r
-        """\r
-        Return the current state of the encoder.\r
-        """\r
-        return 0\r
-\r
-    def setstate(self, state):\r
-        """\r
-        Set the current state of the encoder. state must have been\r
-        returned by getstate().\r
-        """\r
-\r
-class BufferedIncrementalEncoder(IncrementalEncoder):\r
-    """\r
-    This subclass of IncrementalEncoder can be used as the baseclass for an\r
-    incremental encoder if the encoder must keep some of the output in a\r
-    buffer between calls to encode().\r
-    """\r
-    def __init__(self, errors='strict'):\r
-        IncrementalEncoder.__init__(self, errors)\r
-        self.buffer = "" # unencoded input that is kept between calls to encode()\r
-\r
-    def _buffer_encode(self, input, errors, final):\r
-        # Overwrite this method in subclasses: It must encode input\r
-        # and return an (output, length consumed) tuple\r
-        raise NotImplementedError\r
-\r
-    def encode(self, input, final=False):\r
-        # encode input (taking the buffer into account)\r
-        data = self.buffer + input\r
-        (result, consumed) = self._buffer_encode(data, self.errors, final)\r
-        # keep unencoded input until the next call\r
-        self.buffer = data[consumed:]\r
-        return result\r
-\r
-    def reset(self):\r
-        IncrementalEncoder.reset(self)\r
-        self.buffer = ""\r
-\r
-    def getstate(self):\r
-        return self.buffer or 0\r
-\r
-    def setstate(self, state):\r
-        self.buffer = state or ""\r
-\r
-class IncrementalDecoder(object):\r
-    """\r
-    An IncrementalDecoder decodes an input in multiple steps. The input can be\r
-    passed piece by piece to the decode() method. The IncrementalDecoder\r
-    remembers the state of the decoding process between calls to decode().\r
-    """\r
-    def __init__(self, errors='strict'):\r
-        """\r
-        Creates a IncrementalDecoder instance.\r
-\r
-        The IncrementalDecoder may use different error handling schemes by\r
-        providing the errors keyword argument. See the module docstring\r
-        for a list of possible values.\r
-        """\r
-        self.errors = errors\r
-\r
-    def decode(self, input, final=False):\r
-        """\r
-        Decodes input and returns the resulting object.\r
-        """\r
-        raise NotImplementedError\r
-\r
-    def reset(self):\r
-        """\r
-        Resets the decoder to the initial state.\r
-        """\r
-\r
-    def getstate(self):\r
-        """\r
-        Return the current state of the decoder.\r
-\r
-        This must be a (buffered_input, additional_state_info) tuple.\r
-        buffered_input must be a bytes object containing bytes that\r
-        were passed to decode() that have not yet been converted.\r
-        additional_state_info must be a non-negative integer\r
-        representing the state of the decoder WITHOUT yet having\r
-        processed the contents of buffered_input.  In the initial state\r
-        and after reset(), getstate() must return (b"", 0).\r
-        """\r
-        return (b"", 0)\r
-\r
-    def setstate(self, state):\r
-        """\r
-        Set the current state of the decoder.\r
-\r
-        state must have been returned by getstate().  The effect of\r
-        setstate((b"", 0)) must be equivalent to reset().\r
-        """\r
-\r
-class BufferedIncrementalDecoder(IncrementalDecoder):\r
-    """\r
-    This subclass of IncrementalDecoder can be used as the baseclass for an\r
-    incremental decoder if the decoder must be able to handle incomplete byte\r
-    sequences.\r
-    """\r
-    def __init__(self, errors='strict'):\r
-        IncrementalDecoder.__init__(self, errors)\r
-        self.buffer = "" # undecoded input that is kept between calls to decode()\r
-\r
-    def _buffer_decode(self, input, errors, final):\r
-        # Overwrite this method in subclasses: It must decode input\r
-        # and return an (output, length consumed) tuple\r
-        raise NotImplementedError\r
-\r
-    def decode(self, input, final=False):\r
-        # decode input (taking the buffer into account)\r
-        data = self.buffer + input\r
-        (result, consumed) = self._buffer_decode(data, self.errors, final)\r
-        # keep undecoded input until the next call\r
-        self.buffer = data[consumed:]\r
-        return result\r
-\r
-    def reset(self):\r
-        IncrementalDecoder.reset(self)\r
-        self.buffer = ""\r
-\r
-    def getstate(self):\r
-        # additional state info is always 0\r
-        return (self.buffer, 0)\r
-\r
-    def setstate(self, state):\r
-        # ignore additional state info\r
-        self.buffer = state[0]\r
-\r
-#\r
-# The StreamWriter and StreamReader class provide generic working\r
-# interfaces which can be used to implement new encoding submodules\r
-# very easily. See encodings/utf_8.py for an example on how this is\r
-# done.\r
-#\r
-\r
-class StreamWriter(Codec):\r
-\r
-    def __init__(self, stream, errors='strict'):\r
-\r
-        """ Creates a StreamWriter instance.\r
-\r
-            stream must be a file-like object open for writing\r
-            (binary) data.\r
-\r
-            The StreamWriter may use different error handling\r
-            schemes by providing the errors keyword argument. These\r
-            parameters are predefined:\r
-\r
-             'strict' - raise a ValueError (or a subclass)\r
-             'ignore' - ignore the character and continue with the next\r
-             'replace'- replace with a suitable replacement character\r
-             'xmlcharrefreplace' - Replace with the appropriate XML\r
-                                   character reference.\r
-             'backslashreplace'  - Replace with backslashed escape\r
-                                   sequences (only for encoding).\r
-\r
-            The set of allowed parameter values can be extended via\r
-            register_error.\r
-        """\r
-        self.stream = stream\r
-        self.errors = errors\r
-\r
-    def write(self, object):\r
-\r
-        """ Writes the object's contents encoded to self.stream.\r
-        """\r
-        data, consumed = self.encode(object, self.errors)\r
-        self.stream.write(data)\r
-\r
-    def writelines(self, list):\r
-\r
-        """ Writes the concatenated list of strings to the stream\r
-            using .write().\r
-        """\r
-        self.write(''.join(list))\r
-\r
-    def reset(self):\r
-\r
-        """ Flushes and resets the codec buffers used for keeping state.\r
-\r
-            Calling this method should ensure that the data on the\r
-            output is put into a clean state, that allows appending\r
-            of new fresh data without having to rescan the whole\r
-            stream to recover state.\r
-\r
-        """\r
-        pass\r
-\r
-    def seek(self, offset, whence=0):\r
-        self.stream.seek(offset, whence)\r
-        if whence == 0 and offset == 0:\r
-            self.reset()\r
-\r
-    def __getattr__(self, name,\r
-                    getattr=getattr):\r
-\r
-        """ Inherit all other methods from the underlying stream.\r
-        """\r
-        return getattr(self.stream, name)\r
-\r
-    def __enter__(self):\r
-        return self\r
-\r
-    def __exit__(self, type, value, tb):\r
-        self.stream.close()\r
-\r
-###\r
-\r
-class StreamReader(Codec):\r
-\r
-    def __init__(self, stream, errors='strict'):\r
-\r
-        """ Creates a StreamReader instance.\r
-\r
-            stream must be a file-like object open for reading\r
-            (binary) data.\r
-\r
-            The StreamReader may use different error handling\r
-            schemes by providing the errors keyword argument. These\r
-            parameters are predefined:\r
-\r
-             'strict' - raise a ValueError (or a subclass)\r
-             'ignore' - ignore the character and continue with the next\r
-             'replace'- replace with a suitable replacement character;\r
-\r
-            The set of allowed parameter values can be extended via\r
-            register_error.\r
-        """\r
-        self.stream = stream\r
-        self.errors = errors\r
-        self.bytebuffer = ""\r
-        # For str->str decoding this will stay a str\r
-        # For str->unicode decoding the first read will promote it to unicode\r
-        self.charbuffer = ""\r
-        self.linebuffer = None\r
-\r
-    def decode(self, input, errors='strict'):\r
-        raise NotImplementedError\r
-\r
-    def read(self, size=-1, chars=-1, firstline=False):\r
-\r
-        """ Decodes data from the stream self.stream and returns the\r
-            resulting object.\r
-\r
-            chars indicates the number of characters to read from the\r
-            stream. read() will never return more than chars\r
-            characters, but it might return less, if there are not enough\r
-            characters available.\r
-\r
-            size indicates the approximate maximum number of bytes to\r
-            read from the stream for decoding purposes. The decoder\r
-            can modify this setting as appropriate. The default value\r
-            -1 indicates to read and decode as much as possible.  size\r
-            is intended to prevent having to decode huge files in one\r
-            step.\r
-\r
-            If firstline is true, and a UnicodeDecodeError happens\r
-            after the first line terminator in the input only the first line\r
-            will be returned, the rest of the input will be kept until the\r
-            next call to read().\r
-\r
-            The method should use a greedy read strategy meaning that\r
-            it should read as much data as is allowed within the\r
-            definition of the encoding and the given size, e.g.  if\r
-            optional encoding endings or state markers are available\r
-            on the stream, these should be read too.\r
-        """\r
-        # If we have lines cached, first merge them back into characters\r
-        if self.linebuffer:\r
-            self.charbuffer = "".join(self.linebuffer)\r
-            self.linebuffer = None\r
-\r
-        # read until we get the required number of characters (if available)\r
-        while True:\r
-            # can the request can be satisfied from the character buffer?\r
-            if chars < 0:\r
-                if size < 0:\r
-                    if self.charbuffer:\r
-                        break\r
-                elif len(self.charbuffer) >= size:\r
-                    break\r
-            else:\r
-                if len(self.charbuffer) >= chars:\r
-                    break\r
-            # we need more data\r
-            if size < 0:\r
-                newdata = self.stream.read()\r
-            else:\r
-                newdata = self.stream.read(size)\r
-            # decode bytes (those remaining from the last call included)\r
-            data = self.bytebuffer + newdata\r
-            try:\r
-                newchars, decodedbytes = self.decode(data, self.errors)\r
-            except UnicodeDecodeError, exc:\r
-                if firstline:\r
-                    newchars, decodedbytes = self.decode(data[:exc.start], self.errors)\r
-                    lines = newchars.splitlines(True)\r
-                    if len(lines)<=1:\r
-                        raise\r
-                else:\r
-                    raise\r
-            # keep undecoded bytes until the next call\r
-            self.bytebuffer = data[decodedbytes:]\r
-            # put new characters in the character buffer\r
-            self.charbuffer += newchars\r
-            # there was no data available\r
-            if not newdata:\r
-                break\r
-        if chars < 0:\r
-            # Return everything we've got\r
-            result = self.charbuffer\r
-            self.charbuffer = ""\r
-        else:\r
-            # Return the first chars characters\r
-            result = self.charbuffer[:chars]\r
-            self.charbuffer = self.charbuffer[chars:]\r
-        return result\r
-\r
-    def readline(self, size=None, keepends=True):\r
-\r
-        """ Read one line from the input stream and return the\r
-            decoded data.\r
-\r
-            size, if given, is passed as size argument to the\r
-            read() method.\r
-\r
-        """\r
-        # If we have lines cached from an earlier read, return\r
-        # them unconditionally\r
-        if self.linebuffer:\r
-            line = self.linebuffer[0]\r
-            del self.linebuffer[0]\r
-            if len(self.linebuffer) == 1:\r
-                # revert to charbuffer mode; we might need more data\r
-                # next time\r
-                self.charbuffer = self.linebuffer[0]\r
-                self.linebuffer = None\r
-            if not keepends:\r
-                line = line.splitlines(False)[0]\r
-            return line\r
-\r
-        readsize = size or 72\r
-        line = ""\r
-        # If size is given, we call read() only once\r
-        while True:\r
-            data = self.read(readsize, firstline=True)\r
-            if data:\r
-                # If we're at a "\r" read one extra character (which might\r
-                # be a "\n") to get a proper line ending. If the stream is\r
-                # temporarily exhausted we return the wrong line ending.\r
-                if data.endswith("\r"):\r
-                    data += self.read(size=1, chars=1)\r
-\r
-            line += data\r
-            lines = line.splitlines(True)\r
-            if lines:\r
-                if len(lines) > 1:\r
-                    # More than one line result; the first line is a full line\r
-                    # to return\r
-                    line = lines[0]\r
-                    del lines[0]\r
-                    if len(lines) > 1:\r
-                        # cache the remaining lines\r
-                        lines[-1] += self.charbuffer\r
-                        self.linebuffer = lines\r
-                        self.charbuffer = None\r
-                    else:\r
-                        # only one remaining line, put it back into charbuffer\r
-                        self.charbuffer = lines[0] + self.charbuffer\r
-                    if not keepends:\r
-                        line = line.splitlines(False)[0]\r
-                    break\r
-                line0withend = lines[0]\r
-                line0withoutend = lines[0].splitlines(False)[0]\r
-                if line0withend != line0withoutend: # We really have a line end\r
-                    # Put the rest back together and keep it until the next call\r
-                    self.charbuffer = "".join(lines[1:]) + self.charbuffer\r
-                    if keepends:\r
-                        line = line0withend\r
-                    else:\r
-                        line = line0withoutend\r
-                    break\r
-            # we didn't get anything or this was our only try\r
-            if not data or size is not None:\r
-                if line and not keepends:\r
-                    line = line.splitlines(False)[0]\r
-                break\r
-            if readsize<8000:\r
-                readsize *= 2\r
-        return line\r
-\r
-    def readlines(self, sizehint=None, keepends=True):\r
-\r
-        """ Read all lines available on the input stream\r
-            and return them as list of lines.\r
-\r
-            Line breaks are implemented using the codec's decoder\r
-            method and are included in the list entries.\r
-\r
-            sizehint, if given, is ignored since there is no efficient\r
-            way to finding the true end-of-line.\r
-\r
-        """\r
-        data = self.read()\r
-        return data.splitlines(keepends)\r
-\r
-    def reset(self):\r
-\r
-        """ Resets the codec buffers used for keeping state.\r
-\r
-            Note that no stream repositioning should take place.\r
-            This method is primarily intended to be able to recover\r
-            from decoding errors.\r
-\r
-        """\r
-        self.bytebuffer = ""\r
-        self.charbuffer = u""\r
-        self.linebuffer = None\r
-\r
-    def seek(self, offset, whence=0):\r
-        """ Set the input stream's current position.\r
-\r
-            Resets the codec buffers used for keeping state.\r
-        """\r
-        self.stream.seek(offset, whence)\r
-        self.reset()\r
-\r
-    def next(self):\r
-\r
-        """ Return the next decoded line from the input stream."""\r
-        line = self.readline()\r
-        if line:\r
-            return line\r
-        raise StopIteration\r
-\r
-    def __iter__(self):\r
-        return self\r
-\r
-    def __getattr__(self, name,\r
-                    getattr=getattr):\r
-\r
-        """ Inherit all other methods from the underlying stream.\r
-        """\r
-        return getattr(self.stream, name)\r
-\r
-    def __enter__(self):\r
-        return self\r
-\r
-    def __exit__(self, type, value, tb):\r
-        self.stream.close()\r
-\r
-###\r
-\r
-class StreamReaderWriter:\r
-\r
-    """ StreamReaderWriter instances allow wrapping streams which\r
-        work in both read and write modes.\r
-\r
-        The design is such that one can use the factory functions\r
-        returned by the codec.lookup() function to construct the\r
-        instance.\r
-\r
-    """\r
-    # Optional attributes set by the file wrappers below\r
-    encoding = 'unknown'\r
-\r
-    def __init__(self, stream, Reader, Writer, errors='strict'):\r
-\r
-        """ Creates a StreamReaderWriter instance.\r
-\r
-            stream must be a Stream-like object.\r
-\r
-            Reader, Writer must be factory functions or classes\r
-            providing the StreamReader, StreamWriter interface resp.\r
-\r
-            Error handling is done in the same way as defined for the\r
-            StreamWriter/Readers.\r
-\r
-        """\r
-        self.stream = stream\r
-        self.reader = Reader(stream, errors)\r
-        self.writer = Writer(stream, errors)\r
-        self.errors = errors\r
-\r
-    def read(self, size=-1):\r
-\r
-        return self.reader.read(size)\r
-\r
-    def readline(self, size=None):\r
-\r
-        return self.reader.readline(size)\r
-\r
-    def readlines(self, sizehint=None):\r
-\r
-        return self.reader.readlines(sizehint)\r
-\r
-    def next(self):\r
-\r
-        """ Return the next decoded line from the input stream."""\r
-        return self.reader.next()\r
-\r
-    def __iter__(self):\r
-        return self\r
-\r
-    def write(self, data):\r
-\r
-        return self.writer.write(data)\r
-\r
-    def writelines(self, list):\r
-\r
-        return self.writer.writelines(list)\r
-\r
-    def reset(self):\r
-\r
-        self.reader.reset()\r
-        self.writer.reset()\r
-\r
-    def seek(self, offset, whence=0):\r
-        self.stream.seek(offset, whence)\r
-        self.reader.reset()\r
-        if whence == 0 and offset == 0:\r
-            self.writer.reset()\r
-\r
-    def __getattr__(self, name,\r
-                    getattr=getattr):\r
-\r
-        """ Inherit all other methods from the underlying stream.\r
-        """\r
-        return getattr(self.stream, name)\r
-\r
-    # these are needed to make "with codecs.open(...)" work properly\r
-\r
-    def __enter__(self):\r
-        return self\r
-\r
-    def __exit__(self, type, value, tb):\r
-        self.stream.close()\r
-\r
-###\r
-\r
-class StreamRecoder:\r
-\r
-    """ StreamRecoder instances provide a frontend - backend\r
-        view of encoding data.\r
-\r
-        They use the complete set of APIs returned by the\r
-        codecs.lookup() function to implement their task.\r
-\r
-        Data written to the stream is first decoded into an\r
-        intermediate format (which is dependent on the given codec\r
-        combination) and then written to the stream using an instance\r
-        of the provided Writer class.\r
-\r
-        In the other direction, data is read from the stream using a\r
-        Reader instance and then return encoded data to the caller.\r
-\r
-    """\r
-    # Optional attributes set by the file wrappers below\r
-    data_encoding = 'unknown'\r
-    file_encoding = 'unknown'\r
-\r
-    def __init__(self, stream, encode, decode, Reader, Writer,\r
-                 errors='strict'):\r
-\r
-        """ Creates a StreamRecoder instance which implements a two-way\r
-            conversion: encode and decode work on the frontend (the\r
-            input to .read() and output of .write()) while\r
-            Reader and Writer work on the backend (reading and\r
-            writing to the stream).\r
-\r
-            You can use these objects to do transparent direct\r
-            recodings from e.g. latin-1 to utf-8 and back.\r
-\r
-            stream must be a file-like object.\r
-\r
-            encode, decode must adhere to the Codec interface, Reader,\r
-            Writer must be factory functions or classes providing the\r
-            StreamReader, StreamWriter interface resp.\r
-\r
-            encode and decode are needed for the frontend translation,\r
-            Reader and Writer for the backend translation. Unicode is\r
-            used as intermediate encoding.\r
-\r
-            Error handling is done in the same way as defined for the\r
-            StreamWriter/Readers.\r
-\r
-        """\r
-        self.stream = stream\r
-        self.encode = encode\r
-        self.decode = decode\r
-        self.reader = Reader(stream, errors)\r
-        self.writer = Writer(stream, errors)\r
-        self.errors = errors\r
-\r
-    def read(self, size=-1):\r
-\r
-        data = self.reader.read(size)\r
-        data, bytesencoded = self.encode(data, self.errors)\r
-        return data\r
-\r
-    def readline(self, size=None):\r
-\r
-        if size is None:\r
-            data = self.reader.readline()\r
-        else:\r
-            data = self.reader.readline(size)\r
-        data, bytesencoded = self.encode(data, self.errors)\r
-        return data\r
-\r
-    def readlines(self, sizehint=None):\r
-\r
-        data = self.reader.read()\r
-        data, bytesencoded = self.encode(data, self.errors)\r
-        return data.splitlines(1)\r
-\r
-    def next(self):\r
-\r
-        """ Return the next decoded line from the input stream."""\r
-        data = self.reader.next()\r
-        data, bytesencoded = self.encode(data, self.errors)\r
-        return data\r
-\r
-    def __iter__(self):\r
-        return self\r
-\r
-    def write(self, data):\r
-\r
-        data, bytesdecoded = self.decode(data, self.errors)\r
-        return self.writer.write(data)\r
-\r
-    def writelines(self, list):\r
-\r
-        data = ''.join(list)\r
-        data, bytesdecoded = self.decode(data, self.errors)\r
-        return self.writer.write(data)\r
-\r
-    def reset(self):\r
-\r
-        self.reader.reset()\r
-        self.writer.reset()\r
-\r
-    def __getattr__(self, name,\r
-                    getattr=getattr):\r
-\r
-        """ Inherit all other methods from the underlying stream.\r
-        """\r
-        return getattr(self.stream, name)\r
-\r
-    def __enter__(self):\r
-        return self\r
-\r
-    def __exit__(self, type, value, tb):\r
-        self.stream.close()\r
-\r
-### Shortcuts\r
-\r
-def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):\r
-\r
-    """ Open an encoded file using the given mode and return\r
-        a wrapped version providing transparent encoding/decoding.\r
-\r
-        Note: The wrapped version will only accept the object format\r
-        defined by the codecs, i.e. Unicode objects for most builtin\r
-        codecs. Output is also codec dependent and will usually be\r
-        Unicode as well.\r
-\r
-        Files are always opened in binary mode, even if no binary mode\r
-        was specified. This is done to avoid data loss due to encodings\r
-        using 8-bit values. The default file mode is 'rb' meaning to\r
-        open the file in binary read mode.\r
-\r
-        encoding specifies the encoding which is to be used for the\r
-        file.\r
-\r
-        errors may be given to define the error handling. It defaults\r
-        to 'strict' which causes ValueErrors to be raised in case an\r
-        encoding error occurs.\r
-\r
-        buffering has the same meaning as for the builtin open() API.\r
-        It defaults to line buffered.\r
-\r
-        The returned wrapped file object provides an extra attribute\r
-        .encoding which allows querying the used encoding. This\r
-        attribute is only available if an encoding was specified as\r
-        parameter.\r
-\r
-    """\r
-    if encoding is not None:\r
-        if 'U' in mode:\r
-            # No automatic conversion of '\n' is done on reading and writing\r
-            mode = mode.strip().replace('U', '')\r
-            if mode[:1] not in set('rwa'):\r
-                mode = 'r' + mode\r
-        if 'b' not in mode:\r
-            # Force opening of the file in binary mode\r
-            mode = mode + 'b'\r
-    file = __builtin__.open(filename, mode, buffering)\r
-    if encoding is None:\r
-        return file\r
-    info = lookup(encoding)\r
-    srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)\r
-    # Add attributes to simplify introspection\r
-    srw.encoding = encoding\r
-    return srw\r
-\r
-def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):\r
-\r
-    """ Return a wrapped version of file which provides transparent\r
-        encoding translation.\r
-\r
-        Strings written to the wrapped file are interpreted according\r
-        to the given data_encoding and then written to the original\r
-        file as string using file_encoding. The intermediate encoding\r
-        will usually be Unicode but depends on the specified codecs.\r
-\r
-        Strings are read from the file using file_encoding and then\r
-        passed back to the caller as string using data_encoding.\r
-\r
-        If file_encoding is not given, it defaults to data_encoding.\r
-\r
-        errors may be given to define the error handling. It defaults\r
-        to 'strict' which causes ValueErrors to be raised in case an\r
-        encoding error occurs.\r
-\r
-        The returned wrapped file object provides two extra attributes\r
-        .data_encoding and .file_encoding which reflect the given\r
-        parameters of the same name. The attributes can be used for\r
-        introspection by Python programs.\r
-\r
-    """\r
-    if file_encoding is None:\r
-        file_encoding = data_encoding\r
-    data_info = lookup(data_encoding)\r
-    file_info = lookup(file_encoding)\r
-    sr = StreamRecoder(file, data_info.encode, data_info.decode,\r
-                       file_info.streamreader, file_info.streamwriter, errors)\r
-    # Add attributes to simplify introspection\r
-    sr.data_encoding = data_encoding\r
-    sr.file_encoding = file_encoding\r
-    return sr\r
-\r
-### Helpers for codec lookup\r
-\r
-def getencoder(encoding):\r
-\r
-    """ Lookup up the codec for the given encoding and return\r
-        its encoder function.\r
-\r
-        Raises a LookupError in case the encoding cannot be found.\r
-\r
-    """\r
-    return lookup(encoding).encode\r
-\r
-def getdecoder(encoding):\r
-\r
-    """ Lookup up the codec for the given encoding and return\r
-        its decoder function.\r
-\r
-        Raises a LookupError in case the encoding cannot be found.\r
-\r
-    """\r
-    return lookup(encoding).decode\r
-\r
-def getincrementalencoder(encoding):\r
-\r
-    """ Lookup up the codec for the given encoding and return\r
-        its IncrementalEncoder class or factory function.\r
-\r
-        Raises a LookupError in case the encoding cannot be found\r
-        or the codecs doesn't provide an incremental encoder.\r
-\r
-    """\r
-    encoder = lookup(encoding).incrementalencoder\r
-    if encoder is None:\r
-        raise LookupError(encoding)\r
-    return encoder\r
-\r
-def getincrementaldecoder(encoding):\r
-\r
-    """ Lookup up the codec for the given encoding and return\r
-        its IncrementalDecoder class or factory function.\r
-\r
-        Raises a LookupError in case the encoding cannot be found\r
-        or the codecs doesn't provide an incremental decoder.\r
-\r
-    """\r
-    decoder = lookup(encoding).incrementaldecoder\r
-    if decoder is None:\r
-        raise LookupError(encoding)\r
-    return decoder\r
-\r
-def getreader(encoding):\r
-\r
-    """ Lookup up the codec for the given encoding and return\r
-        its StreamReader class or factory function.\r
-\r
-        Raises a LookupError in case the encoding cannot be found.\r
-\r
-    """\r
-    return lookup(encoding).streamreader\r
-\r
-def getwriter(encoding):\r
-\r
-    """ Lookup up the codec for the given encoding and return\r
-        its StreamWriter class or factory function.\r
-\r
-        Raises a LookupError in case the encoding cannot be found.\r
-\r
-    """\r
-    return lookup(encoding).streamwriter\r
-\r
-def iterencode(iterator, encoding, errors='strict', **kwargs):\r
-    """\r
-    Encoding iterator.\r
-\r
-    Encodes the input strings from the iterator using a IncrementalEncoder.\r
-\r
-    errors and kwargs are passed through to the IncrementalEncoder\r
-    constructor.\r
-    """\r
-    encoder = getincrementalencoder(encoding)(errors, **kwargs)\r
-    for input in iterator:\r
-        output = encoder.encode(input)\r
-        if output:\r
-            yield output\r
-    output = encoder.encode("", True)\r
-    if output:\r
-        yield output\r
-\r
-def iterdecode(iterator, encoding, errors='strict', **kwargs):\r
-    """\r
-    Decoding iterator.\r
-\r
-    Decodes the input strings from the iterator using a IncrementalDecoder.\r
-\r
-    errors and kwargs are passed through to the IncrementalDecoder\r
-    constructor.\r
-    """\r
-    decoder = getincrementaldecoder(encoding)(errors, **kwargs)\r
-    for input in iterator:\r
-        output = decoder.decode(input)\r
-        if output:\r
-            yield output\r
-    output = decoder.decode("", True)\r
-    if output:\r
-        yield output\r
-\r
-### Helpers for charmap-based codecs\r
-\r
-def make_identity_dict(rng):\r
-\r
-    """ make_identity_dict(rng) -> dict\r
-\r
-        Return a dictionary where elements of the rng sequence are\r
-        mapped to themselves.\r
-\r
-    """\r
-    res = {}\r
-    for i in rng:\r
-        res[i]=i\r
-    return res\r
-\r
-def make_encoding_map(decoding_map):\r
-\r
-    """ Creates an encoding map from a decoding map.\r
-\r
-        If a target mapping in the decoding map occurs multiple\r
-        times, then that target is mapped to None (undefined mapping),\r
-        causing an exception when encountered by the charmap codec\r
-        during translation.\r
-\r
-        One example where this happens is cp875.py which decodes\r
-        multiple character to \u001a.\r
-\r
-    """\r
-    m = {}\r
-    for k,v in decoding_map.items():\r
-        if not v in m:\r
-            m[v] = k\r
-        else:\r
-            m[v] = None\r
-    return m\r
-\r
-### error handlers\r
-\r
-try:\r
-    strict_errors = lookup_error("strict")\r
-    ignore_errors = lookup_error("ignore")\r
-    replace_errors = lookup_error("replace")\r
-    xmlcharrefreplace_errors = lookup_error("xmlcharrefreplace")\r
-    backslashreplace_errors = lookup_error("backslashreplace")\r
-except LookupError:\r
-    # In --disable-unicode builds, these error handler are missing\r
-    strict_errors = None\r
-    ignore_errors = None\r
-    replace_errors = None\r
-    xmlcharrefreplace_errors = None\r
-    backslashreplace_errors = None\r
-\r
-# Tell modulefinder that using codecs probably needs the encodings\r
-# package\r
-_false = 0\r
-if _false:\r
-    import encodings\r
-\r
-### Tests\r
-\r
-if __name__ == '__main__':\r
-\r
-    # Make stdout translate Latin-1 output into UTF-8 output\r
-    sys.stdout = EncodedFile(sys.stdout, 'latin-1', 'utf-8')\r
-\r
-    # Have stdin translate Latin-1 input into UTF-8 input\r
-    sys.stdin = EncodedFile(sys.stdin, 'utf-8', 'latin-1')\r