Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 1 | """ codecs -- Python Codec Registry, API and helpers. |
| 2 | |
| 3 | |
| 4 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 5 | |
| 6 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 7 | |
| 8 | """#" |
| 9 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 10 | import builtins, sys |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 11 | |
| 12 | ### Registry and builtin stateless codec functions |
| 13 | |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 14 | try: |
| 15 | from _codecs import * |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 16 | except ImportError as why: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 17 | raise SystemError('Failed to load the builtin codecs: %s' % why) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 18 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 19 | __all__ = ["register", "lookup", "open", "EncodedFile", "BOM", "BOM_BE", |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 20 | "BOM_LE", "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE", |
| 21 | "BOM_UTF8", "BOM_UTF16", "BOM_UTF16_LE", "BOM_UTF16_BE", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 22 | "BOM_UTF32", "BOM_UTF32_LE", "BOM_UTF32_BE", |
Serhiy Storchaka | de3ee5b | 2014-12-20 17:42:38 +0200 | [diff] [blame] | 23 | "CodecInfo", "Codec", "IncrementalEncoder", "IncrementalDecoder", |
| 24 | "StreamReader", "StreamWriter", |
| 25 | "StreamReaderWriter", "StreamRecoder", |
| 26 | "getencoder", "getdecoder", "getincrementalencoder", |
| 27 | "getincrementaldecoder", "getreader", "getwriter", |
| 28 | "encode", "decode", "iterencode", "iterdecode", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 29 | "strict_errors", "ignore_errors", "replace_errors", |
| 30 | "xmlcharrefreplace_errors", |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 31 | "backslashreplace_errors", "namereplace_errors", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 32 | "register_error", "lookup_error"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 34 | ### Constants |
| 35 | |
| 36 | # |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 37 | # Byte Order Mark (BOM = ZERO WIDTH NO-BREAK SPACE = U+FEFF) |
| 38 | # and its possible byte string values |
| 39 | # for UTF8/UTF16/UTF32 output and little/big endian machines |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 40 | # |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 41 | |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 42 | # UTF-8 |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 43 | BOM_UTF8 = b'\xef\xbb\xbf' |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 44 | |
| 45 | # UTF-16, little endian |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 46 | BOM_LE = BOM_UTF16_LE = b'\xff\xfe' |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 47 | |
| 48 | # UTF-16, big endian |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 49 | BOM_BE = BOM_UTF16_BE = b'\xfe\xff' |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 50 | |
| 51 | # UTF-32, little endian |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 52 | BOM_UTF32_LE = b'\xff\xfe\x00\x00' |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 53 | |
| 54 | # UTF-32, big endian |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 55 | BOM_UTF32_BE = b'\x00\x00\xfe\xff' |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 56 | |
Marc-André Lemburg | b28de0d | 2002-12-12 17:37:50 +0000 | [diff] [blame] | 57 | if sys.byteorder == 'little': |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 58 | |
Marc-André Lemburg | b28de0d | 2002-12-12 17:37:50 +0000 | [diff] [blame] | 59 | # UTF-16, native endianness |
| 60 | BOM = BOM_UTF16 = BOM_UTF16_LE |
| 61 | |
| 62 | # UTF-32, native endianness |
| 63 | BOM_UTF32 = BOM_UTF32_LE |
| 64 | |
| 65 | else: |
| 66 | |
| 67 | # UTF-16, native endianness |
| 68 | BOM = BOM_UTF16 = BOM_UTF16_BE |
| 69 | |
| 70 | # UTF-32, native endianness |
| 71 | BOM_UTF32 = BOM_UTF32_BE |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 72 | |
| 73 | # Old broken names (don't use in new code) |
| 74 | BOM32_LE = BOM_UTF16_LE |
| 75 | BOM32_BE = BOM_UTF16_BE |
| 76 | BOM64_LE = BOM_UTF32_LE |
| 77 | BOM64_BE = BOM_UTF32_BE |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | ### Codec base classes (defining the API) |
| 81 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 82 | class CodecInfo(tuple): |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 83 | """Codec details when looking up the codec registry""" |
| 84 | |
| 85 | # Private API to allow Python 3.4 to blacklist the known non-Unicode |
| 86 | # codecs in the standard library. A more general mechanism to |
| 87 | # reliably distinguish test encodings from other codecs will hopefully |
| 88 | # be defined for Python 3.5 |
| 89 | # |
| 90 | # See http://bugs.python.org/issue19619 |
| 91 | _is_text_encoding = True # Assume codecs are text encodings by default |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 92 | |
| 93 | def __new__(cls, encode, decode, streamreader=None, streamwriter=None, |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 94 | incrementalencoder=None, incrementaldecoder=None, name=None, |
| 95 | *, _is_text_encoding=None): |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 96 | self = tuple.__new__(cls, (encode, decode, streamreader, streamwriter)) |
| 97 | self.name = name |
| 98 | self.encode = encode |
| 99 | self.decode = decode |
| 100 | self.incrementalencoder = incrementalencoder |
| 101 | self.incrementaldecoder = incrementaldecoder |
| 102 | self.streamwriter = streamwriter |
| 103 | self.streamreader = streamreader |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 104 | if _is_text_encoding is not None: |
| 105 | self._is_text_encoding = _is_text_encoding |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 106 | return self |
| 107 | |
| 108 | def __repr__(self): |
Serhiy Storchaka | 521e586 | 2014-07-22 15:00:37 +0300 | [diff] [blame] | 109 | return "<%s.%s object for encoding %s at %#x>" % \ |
| 110 | (self.__class__.__module__, self.__class__.__qualname__, |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 111 | self.name, id(self)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 113 | class Codec: |
| 114 | |
| 115 | """ Defines the interface for stateless encoders/decoders. |
| 116 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 117 | The .encode()/.decode() methods may use different error |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 118 | handling schemes by providing the errors argument. These |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 119 | string values are predefined: |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 121 | 'strict' - raise a ValueError error (or a subclass) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 122 | 'ignore' - ignore the character and continue with the next |
| 123 | 'replace' - replace with a suitable replacement character; |
| 124 | Python will use the official U+FFFD REPLACEMENT |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 125 | CHARACTER for the builtin Unicode codecs on |
| 126 | decoding and '?' on encoding. |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 127 | 'surrogateescape' - replace with private code points U+DCnn. |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 128 | 'xmlcharrefreplace' - Replace with the appropriate XML |
| 129 | character reference (only for encoding). |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 130 | 'backslashreplace' - Replace with backslashed escape sequences. |
| 131 | 'namereplace' - Replace with \\N{...} escape sequences |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 132 | (only for encoding). |
| 133 | |
| 134 | The set of allowed values can be extended via register_error. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 135 | |
| 136 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 137 | def encode(self, input, errors='strict'): |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 138 | |
Fred Drake | 3e74c0d | 2000-03-17 15:40:35 +0000 | [diff] [blame] | 139 | """ Encodes the object input and returns a tuple (output |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 140 | object, length consumed). |
| 141 | |
| 142 | errors defines the error handling to apply. It defaults to |
| 143 | 'strict' handling. |
| 144 | |
| 145 | The method may not store state in the Codec instance. Use |
Berker Peksag | 41ca828 | 2015-07-30 18:26:10 +0300 | [diff] [blame] | 146 | StreamWriter for codecs which have to keep state in order to |
| 147 | make encoding efficient. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 148 | |
| 149 | The encoder must be able to handle zero length input and |
| 150 | return an empty object of the output object type in this |
| 151 | situation. |
| 152 | |
| 153 | """ |
| 154 | raise NotImplementedError |
| 155 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 156 | def decode(self, input, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 157 | |
| 158 | """ Decodes the object input and returns a tuple (output |
| 159 | object, length consumed). |
| 160 | |
| 161 | input must be an object which provides the bf_getreadbuf |
| 162 | buffer slot. Python strings, buffer objects and memory |
| 163 | mapped files are examples of objects providing this slot. |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 165 | errors defines the error handling to apply. It defaults to |
| 166 | 'strict' handling. |
| 167 | |
| 168 | The method may not store state in the Codec instance. Use |
Berker Peksag | 41ca828 | 2015-07-30 18:26:10 +0300 | [diff] [blame] | 169 | StreamReader for codecs which have to keep state in order to |
| 170 | make decoding efficient. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 171 | |
| 172 | The decoder must be able to handle zero length input and |
| 173 | return an empty object of the output object type in this |
| 174 | situation. |
| 175 | |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 176 | """ |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 177 | raise NotImplementedError |
| 178 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 179 | class IncrementalEncoder(object): |
| 180 | """ |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 181 | An IncrementalEncoder encodes an input in multiple steps. The input can |
| 182 | be passed piece by piece to the encode() method. The IncrementalEncoder |
| 183 | remembers the state of the encoding process between calls to encode(). |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 184 | """ |
| 185 | def __init__(self, errors='strict'): |
| 186 | """ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 187 | Creates an IncrementalEncoder instance. |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 188 | |
| 189 | The IncrementalEncoder may use different error handling schemes by |
| 190 | providing the errors keyword argument. See the module docstring |
| 191 | for a list of possible values. |
| 192 | """ |
| 193 | self.errors = errors |
| 194 | self.buffer = "" |
| 195 | |
| 196 | def encode(self, input, final=False): |
| 197 | """ |
| 198 | Encodes input and returns the resulting object. |
| 199 | """ |
| 200 | raise NotImplementedError |
| 201 | |
| 202 | def reset(self): |
| 203 | """ |
| 204 | Resets the encoder to the initial state. |
| 205 | """ |
| 206 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 207 | def getstate(self): |
| 208 | """ |
| 209 | Return the current state of the encoder. |
| 210 | """ |
| 211 | return 0 |
| 212 | |
| 213 | def setstate(self, state): |
| 214 | """ |
| 215 | Set the current state of the encoder. state must have been |
| 216 | returned by getstate(). |
| 217 | """ |
| 218 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 219 | class BufferedIncrementalEncoder(IncrementalEncoder): |
| 220 | """ |
| 221 | This subclass of IncrementalEncoder can be used as the baseclass for an |
| 222 | incremental encoder if the encoder must keep some of the output in a |
| 223 | buffer between calls to encode(). |
| 224 | """ |
| 225 | def __init__(self, errors='strict'): |
| 226 | IncrementalEncoder.__init__(self, errors) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 227 | # unencoded input that is kept between calls to encode() |
| 228 | self.buffer = "" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 229 | |
| 230 | def _buffer_encode(self, input, errors, final): |
| 231 | # Overwrite this method in subclasses: It must encode input |
| 232 | # and return an (output, length consumed) tuple |
| 233 | raise NotImplementedError |
| 234 | |
| 235 | def encode(self, input, final=False): |
| 236 | # encode input (taking the buffer into account) |
| 237 | data = self.buffer + input |
| 238 | (result, consumed) = self._buffer_encode(data, self.errors, final) |
| 239 | # keep unencoded input until the next call |
| 240 | self.buffer = data[consumed:] |
| 241 | return result |
| 242 | |
| 243 | def reset(self): |
| 244 | IncrementalEncoder.reset(self) |
| 245 | self.buffer = "" |
| 246 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 247 | def getstate(self): |
| 248 | return self.buffer or 0 |
| 249 | |
| 250 | def setstate(self, state): |
| 251 | self.buffer = state or "" |
| 252 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 253 | class IncrementalDecoder(object): |
| 254 | """ |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 255 | An IncrementalDecoder decodes an input in multiple steps. The input can |
| 256 | be passed piece by piece to the decode() method. The IncrementalDecoder |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 257 | remembers the state of the decoding process between calls to decode(). |
| 258 | """ |
| 259 | def __init__(self, errors='strict'): |
| 260 | """ |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 261 | Create a IncrementalDecoder instance. |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 262 | |
| 263 | The IncrementalDecoder may use different error handling schemes by |
| 264 | providing the errors keyword argument. See the module docstring |
| 265 | for a list of possible values. |
| 266 | """ |
| 267 | self.errors = errors |
| 268 | |
| 269 | def decode(self, input, final=False): |
| 270 | """ |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 271 | Decode input and returns the resulting object. |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 272 | """ |
| 273 | raise NotImplementedError |
| 274 | |
| 275 | def reset(self): |
| 276 | """ |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 277 | Reset the decoder to the initial state. |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 278 | """ |
| 279 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 280 | def getstate(self): |
| 281 | """ |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 282 | Return the current state of the decoder. |
| 283 | |
| 284 | This must be a (buffered_input, additional_state_info) tuple. |
| 285 | buffered_input must be a bytes object containing bytes that |
| 286 | were passed to decode() that have not yet been converted. |
| 287 | additional_state_info must be a non-negative integer |
| 288 | representing the state of the decoder WITHOUT yet having |
| 289 | processed the contents of buffered_input. In the initial state |
| 290 | and after reset(), getstate() must return (b"", 0). |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 291 | """ |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 292 | return (b"", 0) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 293 | |
| 294 | def setstate(self, state): |
| 295 | """ |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 296 | Set the current state of the decoder. |
| 297 | |
| 298 | state must have been returned by getstate(). The effect of |
| 299 | setstate((b"", 0)) must be equivalent to reset(). |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 300 | """ |
| 301 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 302 | class BufferedIncrementalDecoder(IncrementalDecoder): |
| 303 | """ |
| 304 | This subclass of IncrementalDecoder can be used as the baseclass for an |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 305 | incremental decoder if the decoder must be able to handle incomplete |
| 306 | byte sequences. |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 307 | """ |
| 308 | def __init__(self, errors='strict'): |
| 309 | IncrementalDecoder.__init__(self, errors) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 310 | # undecoded input that is kept between calls to decode() |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 311 | self.buffer = b"" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 312 | |
| 313 | def _buffer_decode(self, input, errors, final): |
| 314 | # Overwrite this method in subclasses: It must decode input |
| 315 | # and return an (output, length consumed) tuple |
| 316 | raise NotImplementedError |
| 317 | |
| 318 | def decode(self, input, final=False): |
| 319 | # decode input (taking the buffer into account) |
| 320 | data = self.buffer + input |
| 321 | (result, consumed) = self._buffer_decode(data, self.errors, final) |
| 322 | # keep undecoded input until the next call |
| 323 | self.buffer = data[consumed:] |
| 324 | return result |
| 325 | |
| 326 | def reset(self): |
| 327 | IncrementalDecoder.reset(self) |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 328 | self.buffer = b"" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 329 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 330 | def getstate(self): |
| 331 | # additional state info is always 0 |
| 332 | return (self.buffer, 0) |
| 333 | |
| 334 | def setstate(self, state): |
| 335 | # ignore additional state info |
| 336 | self.buffer = state[0] |
| 337 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 338 | # |
| 339 | # The StreamWriter and StreamReader class provide generic working |
Andrew M. Kuchling | 97c5635 | 2001-09-18 20:29:48 +0000 | [diff] [blame] | 340 | # interfaces which can be used to implement new encoding submodules |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 341 | # very easily. See encodings/utf_8.py for an example on how this is |
| 342 | # done. |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 343 | # |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 344 | |
| 345 | class StreamWriter(Codec): |
| 346 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 347 | def __init__(self, stream, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 348 | |
| 349 | """ Creates a StreamWriter instance. |
| 350 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 351 | stream must be a file-like object open for writing. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 352 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 353 | The StreamWriter may use different error handling |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 354 | schemes by providing the errors keyword argument. These |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 355 | parameters are predefined: |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 356 | |
| 357 | 'strict' - raise a ValueError (or a subclass) |
| 358 | 'ignore' - ignore the character and continue with the next |
| 359 | 'replace'- replace with a suitable replacement character |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 360 | 'xmlcharrefreplace' - Replace with the appropriate XML |
| 361 | character reference. |
| 362 | 'backslashreplace' - Replace with backslashed escape |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 363 | sequences. |
| 364 | 'namereplace' - Replace with \\N{...} escape sequences. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 365 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 366 | The set of allowed parameter values can be extended via |
| 367 | register_error. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 368 | """ |
| 369 | self.stream = stream |
| 370 | self.errors = errors |
| 371 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 372 | def write(self, object): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 373 | |
| 374 | """ Writes the object's contents encoded to self.stream. |
| 375 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 376 | data, consumed = self.encode(object, self.errors) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 377 | self.stream.write(data) |
| 378 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 379 | def writelines(self, list): |
| 380 | |
| 381 | """ Writes the concatenated list of strings to the stream |
| 382 | using .write(). |
| 383 | """ |
| 384 | self.write(''.join(list)) |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 385 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 386 | def reset(self): |
| 387 | |
| 388 | """ Flushes and resets the codec buffers used for keeping state. |
| 389 | |
| 390 | Calling this method should ensure that the data on the |
| 391 | output is put into a clean state, that allows appending |
| 392 | of new fresh data without having to rescan the whole |
| 393 | stream to recover state. |
| 394 | |
| 395 | """ |
| 396 | pass |
| 397 | |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 398 | def seek(self, offset, whence=0): |
| 399 | self.stream.seek(offset, whence) |
| 400 | if whence == 0 and offset == 0: |
| 401 | self.reset() |
| 402 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 403 | def __getattr__(self, name, |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 404 | getattr=getattr): |
| 405 | |
| 406 | """ Inherit all other methods from the underlying stream. |
| 407 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 408 | return getattr(self.stream, name) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 409 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 410 | def __enter__(self): |
| 411 | return self |
| 412 | |
| 413 | def __exit__(self, type, value, tb): |
| 414 | self.stream.close() |
| 415 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 416 | ### |
| 417 | |
| 418 | class StreamReader(Codec): |
| 419 | |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 420 | charbuffertype = str |
| 421 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 422 | def __init__(self, stream, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 423 | |
| 424 | """ Creates a StreamReader instance. |
| 425 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 426 | stream must be a file-like object open for reading. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 427 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 428 | The StreamReader may use different error handling |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 429 | schemes by providing the errors keyword argument. These |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 430 | parameters are predefined: |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 431 | |
| 432 | 'strict' - raise a ValueError (or a subclass) |
| 433 | 'ignore' - ignore the character and continue with the next |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 434 | 'replace'- replace with a suitable replacement character |
| 435 | 'backslashreplace' - Replace with backslashed escape sequences; |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 436 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 437 | The set of allowed parameter values can be extended via |
| 438 | register_error. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 439 | """ |
| 440 | self.stream = stream |
| 441 | self.errors = errors |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 442 | self.bytebuffer = b"" |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 443 | self._empty_charbuffer = self.charbuffertype() |
| 444 | self.charbuffer = self._empty_charbuffer |
Martin v. Löwis | 4ed6738 | 2005-09-18 08:34:39 +0000 | [diff] [blame] | 445 | self.linebuffer = None |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 446 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 447 | def decode(self, input, errors='strict'): |
| 448 | raise NotImplementedError |
| 449 | |
Martin v. Löwis | 56066d2 | 2005-08-24 07:38:12 +0000 | [diff] [blame] | 450 | def read(self, size=-1, chars=-1, firstline=False): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 451 | |
| 452 | """ Decodes data from the stream self.stream and returns the |
| 453 | resulting object. |
| 454 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 455 | chars indicates the number of decoded code points or bytes to |
| 456 | return. read() will never return more data than requested, |
| 457 | but it might return less, if there is not enough available. |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 458 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 459 | size indicates the approximate maximum number of decoded |
| 460 | bytes or code points to read for decoding. The decoder |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 461 | can modify this setting as appropriate. The default value |
| 462 | -1 indicates to read and decode as much as possible. size |
| 463 | is intended to prevent having to decode huge files in one |
| 464 | step. |
| 465 | |
Martin v. Löwis | 56066d2 | 2005-08-24 07:38:12 +0000 | [diff] [blame] | 466 | If firstline is true, and a UnicodeDecodeError happens |
| 467 | after the first line terminator in the input only the first line |
| 468 | will be returned, the rest of the input will be kept until the |
| 469 | next call to read(). |
| 470 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 471 | The method should use a greedy read strategy, meaning that |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 472 | it should read as much data as is allowed within the |
| 473 | definition of the encoding and the given size, e.g. if |
| 474 | optional encoding endings or state markers are available |
| 475 | on the stream, these should be read too. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 476 | """ |
Martin v. Löwis | 4ed6738 | 2005-09-18 08:34:39 +0000 | [diff] [blame] | 477 | # If we have lines cached, first merge them back into characters |
| 478 | if self.linebuffer: |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 479 | self.charbuffer = self._empty_charbuffer.join(self.linebuffer) |
Martin v. Löwis | 4ed6738 | 2005-09-18 08:34:39 +0000 | [diff] [blame] | 480 | self.linebuffer = None |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 481 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 482 | # read until we get the required number of characters (if available) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 483 | while True: |
Tim Golden | 621302c | 2012-10-01 16:40:40 +0100 | [diff] [blame] | 484 | # can the request be satisfied from the character buffer? |
Serhiy Storchaka | dbe0982 | 2014-01-26 19:27:56 +0200 | [diff] [blame] | 485 | if chars >= 0: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 486 | if len(self.charbuffer) >= chars: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 487 | break |
Serhiy Storchaka | dbe0982 | 2014-01-26 19:27:56 +0200 | [diff] [blame] | 488 | elif size >= 0: |
| 489 | if len(self.charbuffer) >= size: |
| 490 | break |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 491 | # we need more data |
| 492 | if size < 0: |
| 493 | newdata = self.stream.read() |
| 494 | else: |
| 495 | newdata = self.stream.read(size) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 496 | # decode bytes (those remaining from the last call included) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 497 | data = self.bytebuffer + newdata |
Serhiy Storchaka | dbe0982 | 2014-01-26 19:27:56 +0200 | [diff] [blame] | 498 | if not data: |
| 499 | break |
Martin v. Löwis | 56066d2 | 2005-08-24 07:38:12 +0000 | [diff] [blame] | 500 | try: |
| 501 | newchars, decodedbytes = self.decode(data, self.errors) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 502 | except UnicodeDecodeError as exc: |
Martin v. Löwis | 56066d2 | 2005-08-24 07:38:12 +0000 | [diff] [blame] | 503 | if firstline: |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 504 | newchars, decodedbytes = \ |
| 505 | self.decode(data[:exc.start], self.errors) |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 506 | lines = newchars.splitlines(keepends=True) |
Martin v. Löwis | 56066d2 | 2005-08-24 07:38:12 +0000 | [diff] [blame] | 507 | if len(lines)<=1: |
| 508 | raise |
| 509 | else: |
| 510 | raise |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 511 | # keep undecoded bytes until the next call |
| 512 | self.bytebuffer = data[decodedbytes:] |
| 513 | # put new characters in the character buffer |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 514 | self.charbuffer += newchars |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 515 | # there was no data available |
| 516 | if not newdata: |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 517 | break |
| 518 | if chars < 0: |
| 519 | # Return everything we've got |
| 520 | result = self.charbuffer |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 521 | self.charbuffer = self._empty_charbuffer |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 522 | else: |
| 523 | # Return the first chars characters |
| 524 | result = self.charbuffer[:chars] |
| 525 | self.charbuffer = self.charbuffer[chars:] |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 526 | return result |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 527 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 528 | def readline(self, size=None, keepends=True): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 529 | |
| 530 | """ Read one line from the input stream and return the |
| 531 | decoded data. |
| 532 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 533 | size, if given, is passed as size argument to the |
| 534 | read() method. |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 535 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 536 | """ |
Martin v. Löwis | 4ed6738 | 2005-09-18 08:34:39 +0000 | [diff] [blame] | 537 | # If we have lines cached from an earlier read, return |
| 538 | # them unconditionally |
| 539 | if self.linebuffer: |
| 540 | line = self.linebuffer[0] |
| 541 | del self.linebuffer[0] |
| 542 | if len(self.linebuffer) == 1: |
| 543 | # revert to charbuffer mode; we might need more data |
| 544 | # next time |
| 545 | self.charbuffer = self.linebuffer[0] |
| 546 | self.linebuffer = None |
| 547 | if not keepends: |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 548 | line = line.splitlines(keepends=False)[0] |
Martin v. Löwis | 4ed6738 | 2005-09-18 08:34:39 +0000 | [diff] [blame] | 549 | return line |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 550 | |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 551 | readsize = size or 72 |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 552 | line = self._empty_charbuffer |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 553 | # If size is given, we call read() only once |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 554 | while True: |
Martin v. Löwis | 56066d2 | 2005-08-24 07:38:12 +0000 | [diff] [blame] | 555 | data = self.read(readsize, firstline=True) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 556 | if data: |
Walter Dörwald | a4eb2d5 | 2005-04-21 21:42:35 +0000 | [diff] [blame] | 557 | # If we're at a "\r" read one extra character (which might |
| 558 | # be a "\n") to get a proper line ending. If the stream is |
Walter Dörwald | bc8e642 | 2005-04-21 21:32:03 +0000 | [diff] [blame] | 559 | # temporarily exhausted we return the wrong line ending. |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 560 | if (isinstance(data, str) and data.endswith("\r")) or \ |
| 561 | (isinstance(data, bytes) and data.endswith(b"\r")): |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 562 | data += self.read(size=1, chars=1) |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 563 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 564 | line += data |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 565 | lines = line.splitlines(keepends=True) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 566 | if lines: |
Martin v. Löwis | 4ed6738 | 2005-09-18 08:34:39 +0000 | [diff] [blame] | 567 | if len(lines) > 1: |
| 568 | # More than one line result; the first line is a full line |
| 569 | # to return |
| 570 | line = lines[0] |
| 571 | del lines[0] |
| 572 | if len(lines) > 1: |
| 573 | # cache the remaining lines |
| 574 | lines[-1] += self.charbuffer |
| 575 | self.linebuffer = lines |
| 576 | self.charbuffer = None |
| 577 | else: |
| 578 | # only one remaining line, put it back into charbuffer |
| 579 | self.charbuffer = lines[0] + self.charbuffer |
| 580 | if not keepends: |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 581 | line = line.splitlines(keepends=False)[0] |
Martin v. Löwis | 4ed6738 | 2005-09-18 08:34:39 +0000 | [diff] [blame] | 582 | break |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 583 | line0withend = lines[0] |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 584 | line0withoutend = lines[0].splitlines(keepends=False)[0] |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 585 | if line0withend != line0withoutend: # We really have a line end |
| 586 | # Put the rest back together and keep it until the next call |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 587 | self.charbuffer = self._empty_charbuffer.join(lines[1:]) + \ |
| 588 | self.charbuffer |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 589 | if keepends: |
| 590 | line = line0withend |
| 591 | else: |
| 592 | line = line0withoutend |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 593 | break |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 594 | # we didn't get anything or this was our only try |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 595 | if not data or size is not None: |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 596 | if line and not keepends: |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 597 | line = line.splitlines(keepends=False)[0] |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 598 | break |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 599 | if readsize < 8000: |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 600 | readsize *= 2 |
| 601 | return line |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 602 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 603 | def readlines(self, sizehint=None, keepends=True): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 604 | |
| 605 | """ Read all lines available on the input stream |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 606 | and return them as a list. |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 607 | |
| 608 | Line breaks are implemented using the codec's decoder |
| 609 | method and are included in the list entries. |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 610 | |
Marc-André Lemburg | d594849 | 2004-02-26 15:22:17 +0000 | [diff] [blame] | 611 | sizehint, if given, is ignored since there is no efficient |
| 612 | way to finding the true end-of-line. |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 613 | |
| 614 | """ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 615 | data = self.read() |
Hye-Shik Chang | af5c7cf | 2004-10-17 23:51:21 +0000 | [diff] [blame] | 616 | return data.splitlines(keepends) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 617 | |
| 618 | def reset(self): |
| 619 | |
| 620 | """ Resets the codec buffers used for keeping state. |
| 621 | |
| 622 | Note that no stream repositioning should take place. |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 623 | This method is primarily intended to be able to recover |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 624 | from decoding errors. |
| 625 | |
| 626 | """ |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 627 | self.bytebuffer = b"" |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 628 | self.charbuffer = self._empty_charbuffer |
Martin v. Löwis | 4ed6738 | 2005-09-18 08:34:39 +0000 | [diff] [blame] | 629 | self.linebuffer = None |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 630 | |
Walter Dörwald | 71fd90d | 2005-03-14 19:25:41 +0000 | [diff] [blame] | 631 | def seek(self, offset, whence=0): |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 632 | """ Set the input stream's current position. |
| 633 | |
| 634 | Resets the codec buffers used for keeping state. |
| 635 | """ |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 636 | self.stream.seek(offset, whence) |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 637 | self.reset() |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 638 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 639 | def __next__(self): |
Walter Dörwald | 4dbf192 | 2002-11-06 16:53:44 +0000 | [diff] [blame] | 640 | |
| 641 | """ Return the next decoded line from the input stream.""" |
| 642 | line = self.readline() |
| 643 | if line: |
| 644 | return line |
| 645 | raise StopIteration |
| 646 | |
| 647 | def __iter__(self): |
| 648 | return self |
| 649 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 650 | def __getattr__(self, name, |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 651 | getattr=getattr): |
| 652 | |
| 653 | """ Inherit all other methods from the underlying stream. |
| 654 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 655 | return getattr(self.stream, name) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 656 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 657 | def __enter__(self): |
| 658 | return self |
| 659 | |
| 660 | def __exit__(self, type, value, tb): |
| 661 | self.stream.close() |
| 662 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 663 | ### |
| 664 | |
| 665 | class StreamReaderWriter: |
| 666 | |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 667 | """ StreamReaderWriter instances allow wrapping streams which |
| 668 | work in both read and write modes. |
| 669 | |
| 670 | The design is such that one can use the factory functions |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 671 | returned by the codec.lookup() function to construct the |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 672 | instance. |
| 673 | |
| 674 | """ |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 675 | # Optional attributes set by the file wrappers below |
| 676 | encoding = 'unknown' |
| 677 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 678 | def __init__(self, stream, Reader, Writer, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 679 | |
| 680 | """ Creates a StreamReaderWriter instance. |
| 681 | |
| 682 | stream must be a Stream-like object. |
| 683 | |
| 684 | Reader, Writer must be factory functions or classes |
| 685 | providing the StreamReader, StreamWriter interface resp. |
| 686 | |
| 687 | Error handling is done in the same way as defined for the |
| 688 | StreamWriter/Readers. |
| 689 | |
| 690 | """ |
| 691 | self.stream = stream |
| 692 | self.reader = Reader(stream, errors) |
| 693 | self.writer = Writer(stream, errors) |
| 694 | self.errors = errors |
| 695 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 696 | def read(self, size=-1): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 697 | |
| 698 | return self.reader.read(size) |
| 699 | |
Guido van Rossum | d58c26f | 2000-05-01 16:17:32 +0000 | [diff] [blame] | 700 | def readline(self, size=None): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 701 | |
| 702 | return self.reader.readline(size) |
| 703 | |
Guido van Rossum | d58c26f | 2000-05-01 16:17:32 +0000 | [diff] [blame] | 704 | def readlines(self, sizehint=None): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 705 | |
| 706 | return self.reader.readlines(sizehint) |
| 707 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 708 | def __next__(self): |
Walter Dörwald | 4dbf192 | 2002-11-06 16:53:44 +0000 | [diff] [blame] | 709 | |
| 710 | """ Return the next decoded line from the input stream.""" |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 711 | return next(self.reader) |
Walter Dörwald | 4dbf192 | 2002-11-06 16:53:44 +0000 | [diff] [blame] | 712 | |
| 713 | def __iter__(self): |
| 714 | return self |
| 715 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 716 | def write(self, data): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 717 | |
| 718 | return self.writer.write(data) |
| 719 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 720 | def writelines(self, list): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 721 | |
| 722 | return self.writer.writelines(list) |
| 723 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 724 | def reset(self): |
| 725 | |
| 726 | self.reader.reset() |
| 727 | self.writer.reset() |
| 728 | |
Victor Stinner | 3fed087 | 2010-05-22 02:16:27 +0000 | [diff] [blame] | 729 | def seek(self, offset, whence=0): |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 730 | self.stream.seek(offset, whence) |
| 731 | self.reader.reset() |
| 732 | if whence == 0 and offset == 0: |
| 733 | self.writer.reset() |
Victor Stinner | 3fed087 | 2010-05-22 02:16:27 +0000 | [diff] [blame] | 734 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 735 | def __getattr__(self, name, |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 736 | getattr=getattr): |
| 737 | |
| 738 | """ Inherit all other methods from the underlying stream. |
| 739 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 740 | return getattr(self.stream, name) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 741 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 742 | # these are needed to make "with codecs.open(...)" work properly |
| 743 | |
| 744 | def __enter__(self): |
| 745 | return self |
| 746 | |
| 747 | def __exit__(self, type, value, tb): |
| 748 | self.stream.close() |
| 749 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 750 | ### |
| 751 | |
| 752 | class StreamRecoder: |
| 753 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 754 | """ StreamRecoder instances translate data from one encoding to another. |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 755 | |
| 756 | They use the complete set of APIs returned by the |
| 757 | codecs.lookup() function to implement their task. |
| 758 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 759 | Data written to the StreamRecoder is first decoded into an |
| 760 | intermediate format (depending on the "decode" codec) and then |
| 761 | written to the underlying stream using an instance of the provided |
| 762 | Writer class. |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 763 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 764 | In the other direction, data is read from the underlying stream using |
| 765 | a Reader instance and then encoded and returned to the caller. |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 766 | |
| 767 | """ |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 768 | # Optional attributes set by the file wrappers below |
| 769 | data_encoding = 'unknown' |
| 770 | file_encoding = 'unknown' |
| 771 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 772 | def __init__(self, stream, encode, decode, Reader, Writer, |
| 773 | errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 774 | |
| 775 | """ Creates a StreamRecoder instance which implements a two-way |
| 776 | conversion: encode and decode work on the frontend (the |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 777 | data visible to .read() and .write()) while Reader and Writer |
| 778 | work on the backend (the data in stream). |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 779 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 780 | You can use these objects to do transparent |
| 781 | transcodings from e.g. latin-1 to utf-8 and back. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 782 | |
| 783 | stream must be a file-like object. |
| 784 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 785 | encode and decode must adhere to the Codec interface; Reader and |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 786 | Writer must be factory functions or classes providing the |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 787 | StreamReader and StreamWriter interfaces resp. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 788 | |
| 789 | Error handling is done in the same way as defined for the |
| 790 | StreamWriter/Readers. |
| 791 | |
| 792 | """ |
| 793 | self.stream = stream |
| 794 | self.encode = encode |
| 795 | self.decode = decode |
| 796 | self.reader = Reader(stream, errors) |
| 797 | self.writer = Writer(stream, errors) |
| 798 | self.errors = errors |
| 799 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 800 | def read(self, size=-1): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 801 | |
| 802 | data = self.reader.read(size) |
| 803 | data, bytesencoded = self.encode(data, self.errors) |
| 804 | return data |
| 805 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 806 | def readline(self, size=None): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 807 | |
| 808 | if size is None: |
| 809 | data = self.reader.readline() |
| 810 | else: |
| 811 | data = self.reader.readline(size) |
| 812 | data, bytesencoded = self.encode(data, self.errors) |
| 813 | return data |
| 814 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 815 | def readlines(self, sizehint=None): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 816 | |
Marc-André Lemburg | d594849 | 2004-02-26 15:22:17 +0000 | [diff] [blame] | 817 | data = self.reader.read() |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 818 | data, bytesencoded = self.encode(data, self.errors) |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 819 | return data.splitlines(keepends=True) |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 820 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 821 | def __next__(self): |
Walter Dörwald | 4dbf192 | 2002-11-06 16:53:44 +0000 | [diff] [blame] | 822 | |
| 823 | """ Return the next decoded line from the input stream.""" |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 824 | data = next(self.reader) |
Walter Dörwald | c5238b8 | 2005-09-01 11:56:53 +0000 | [diff] [blame] | 825 | data, bytesencoded = self.encode(data, self.errors) |
| 826 | return data |
Walter Dörwald | 4dbf192 | 2002-11-06 16:53:44 +0000 | [diff] [blame] | 827 | |
| 828 | def __iter__(self): |
| 829 | return self |
| 830 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 831 | def write(self, data): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 832 | |
| 833 | data, bytesdecoded = self.decode(data, self.errors) |
| 834 | return self.writer.write(data) |
| 835 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 836 | def writelines(self, list): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 837 | |
| 838 | data = ''.join(list) |
| 839 | data, bytesdecoded = self.decode(data, self.errors) |
| 840 | return self.writer.write(data) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 841 | |
| 842 | def reset(self): |
| 843 | |
| 844 | self.reader.reset() |
| 845 | self.writer.reset() |
| 846 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 847 | def __getattr__(self, name, |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 848 | getattr=getattr): |
| 849 | |
| 850 | """ Inherit all other methods from the underlying stream. |
| 851 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 852 | return getattr(self.stream, name) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 853 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 854 | def __enter__(self): |
| 855 | return self |
| 856 | |
| 857 | def __exit__(self, type, value, tb): |
| 858 | self.stream.close() |
| 859 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 860 | ### Shortcuts |
| 861 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 862 | def open(filename, mode='r', encoding=None, errors='strict', buffering=1): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 863 | |
| 864 | """ Open an encoded file using the given mode and return |
| 865 | a wrapped version providing transparent encoding/decoding. |
| 866 | |
| 867 | Note: The wrapped version will only accept the object format |
| 868 | defined by the codecs, i.e. Unicode objects for most builtin |
Skip Montanaro | 9f5f9d9 | 2005-03-16 03:51:56 +0000 | [diff] [blame] | 869 | codecs. Output is also codec dependent and will usually be |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 870 | Unicode as well. |
| 871 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 872 | Underlying encoded files are always opened in binary mode. |
| 873 | The default file mode is 'r', meaning to open the file in read mode. |
Marc-André Lemburg | 349a3d3 | 2000-06-21 21:21:04 +0000 | [diff] [blame] | 874 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 875 | encoding specifies the encoding which is to be used for the |
Walter Dörwald | 7f3ed74 | 2003-02-02 23:08:27 +0000 | [diff] [blame] | 876 | file. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 877 | |
| 878 | errors may be given to define the error handling. It defaults |
| 879 | to 'strict' which causes ValueErrors to be raised in case an |
| 880 | encoding error occurs. |
| 881 | |
| 882 | buffering has the same meaning as for the builtin open() API. |
| 883 | It defaults to line buffered. |
| 884 | |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 885 | The returned wrapped file object provides an extra attribute |
| 886 | .encoding which allows querying the used encoding. This |
| 887 | attribute is only available if an encoding was specified as |
| 888 | parameter. |
| 889 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 890 | """ |
| 891 | if encoding is not None and \ |
| 892 | 'b' not in mode: |
| 893 | # Force opening of the file in binary mode |
| 894 | mode = mode + 'b' |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 895 | file = builtins.open(filename, mode, buffering) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 896 | if encoding is None: |
| 897 | return file |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 898 | info = lookup(encoding) |
| 899 | srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors) |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 900 | # Add attributes to simplify introspection |
| 901 | srw.encoding = encoding |
| 902 | return srw |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 903 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 904 | def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 905 | |
| 906 | """ Return a wrapped version of file which provides transparent |
| 907 | encoding translation. |
| 908 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 909 | Data written to the wrapped file is decoded according |
| 910 | to the given data_encoding and then encoded to the underlying |
| 911 | file using file_encoding. The intermediate data type |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 912 | will usually be Unicode but depends on the specified codecs. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 913 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 914 | Bytes read from the file are decoded using file_encoding and then |
| 915 | passed back to the caller encoded using data_encoding. |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 916 | |
| 917 | If file_encoding is not given, it defaults to data_encoding. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 918 | |
| 919 | errors may be given to define the error handling. It defaults |
| 920 | to 'strict' which causes ValueErrors to be raised in case an |
| 921 | encoding error occurs. |
| 922 | |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 923 | The returned wrapped file object provides two extra attributes |
| 924 | .data_encoding and .file_encoding which reflect the given |
| 925 | parameters of the same name. The attributes can be used for |
| 926 | introspection by Python programs. |
| 927 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 928 | """ |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 929 | if file_encoding is None: |
| 930 | file_encoding = data_encoding |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 931 | data_info = lookup(data_encoding) |
| 932 | file_info = lookup(file_encoding) |
| 933 | sr = StreamRecoder(file, data_info.encode, data_info.decode, |
| 934 | file_info.streamreader, file_info.streamwriter, errors) |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 935 | # Add attributes to simplify introspection |
| 936 | sr.data_encoding = data_encoding |
| 937 | sr.file_encoding = file_encoding |
| 938 | return sr |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 939 | |
Marc-André Lemburg | aa32c5a | 2001-09-19 11:24:48 +0000 | [diff] [blame] | 940 | ### Helpers for codec lookup |
| 941 | |
| 942 | def getencoder(encoding): |
| 943 | |
| 944 | """ Lookup up the codec for the given encoding and return |
| 945 | its encoder function. |
| 946 | |
| 947 | Raises a LookupError in case the encoding cannot be found. |
| 948 | |
| 949 | """ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 950 | return lookup(encoding).encode |
Marc-André Lemburg | aa32c5a | 2001-09-19 11:24:48 +0000 | [diff] [blame] | 951 | |
| 952 | def getdecoder(encoding): |
| 953 | |
| 954 | """ Lookup up the codec for the given encoding and return |
| 955 | its decoder function. |
| 956 | |
| 957 | Raises a LookupError in case the encoding cannot be found. |
| 958 | |
| 959 | """ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 960 | return lookup(encoding).decode |
| 961 | |
| 962 | def getincrementalencoder(encoding): |
| 963 | |
| 964 | """ Lookup up the codec for the given encoding and return |
| 965 | its IncrementalEncoder class or factory function. |
| 966 | |
| 967 | Raises a LookupError in case the encoding cannot be found |
| 968 | or the codecs doesn't provide an incremental encoder. |
| 969 | |
| 970 | """ |
| 971 | encoder = lookup(encoding).incrementalencoder |
| 972 | if encoder is None: |
| 973 | raise LookupError(encoding) |
| 974 | return encoder |
| 975 | |
| 976 | def getincrementaldecoder(encoding): |
| 977 | |
| 978 | """ Lookup up the codec for the given encoding and return |
| 979 | its IncrementalDecoder class or factory function. |
| 980 | |
| 981 | Raises a LookupError in case the encoding cannot be found |
| 982 | or the codecs doesn't provide an incremental decoder. |
| 983 | |
| 984 | """ |
| 985 | decoder = lookup(encoding).incrementaldecoder |
| 986 | if decoder is None: |
| 987 | raise LookupError(encoding) |
| 988 | return decoder |
Marc-André Lemburg | aa32c5a | 2001-09-19 11:24:48 +0000 | [diff] [blame] | 989 | |
| 990 | def getreader(encoding): |
| 991 | |
| 992 | """ Lookup up the codec for the given encoding and return |
| 993 | its StreamReader class or factory function. |
| 994 | |
| 995 | Raises a LookupError in case the encoding cannot be found. |
| 996 | |
| 997 | """ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 998 | return lookup(encoding).streamreader |
Marc-André Lemburg | aa32c5a | 2001-09-19 11:24:48 +0000 | [diff] [blame] | 999 | |
| 1000 | def getwriter(encoding): |
| 1001 | |
| 1002 | """ Lookup up the codec for the given encoding and return |
| 1003 | its StreamWriter class or factory function. |
| 1004 | |
| 1005 | Raises a LookupError in case the encoding cannot be found. |
| 1006 | |
| 1007 | """ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1008 | return lookup(encoding).streamwriter |
| 1009 | |
| 1010 | def iterencode(iterator, encoding, errors='strict', **kwargs): |
| 1011 | """ |
| 1012 | Encoding iterator. |
| 1013 | |
| 1014 | Encodes the input strings from the iterator using a IncrementalEncoder. |
| 1015 | |
| 1016 | errors and kwargs are passed through to the IncrementalEncoder |
| 1017 | constructor. |
| 1018 | """ |
| 1019 | encoder = getincrementalencoder(encoding)(errors, **kwargs) |
| 1020 | for input in iterator: |
| 1021 | output = encoder.encode(input) |
| 1022 | if output: |
| 1023 | yield output |
| 1024 | output = encoder.encode("", True) |
| 1025 | if output: |
| 1026 | yield output |
| 1027 | |
| 1028 | def iterdecode(iterator, encoding, errors='strict', **kwargs): |
| 1029 | """ |
| 1030 | Decoding iterator. |
| 1031 | |
| 1032 | Decodes the input strings from the iterator using a IncrementalDecoder. |
| 1033 | |
| 1034 | errors and kwargs are passed through to the IncrementalDecoder |
| 1035 | constructor. |
| 1036 | """ |
| 1037 | decoder = getincrementaldecoder(encoding)(errors, **kwargs) |
| 1038 | for input in iterator: |
| 1039 | output = decoder.decode(input) |
| 1040 | if output: |
| 1041 | yield output |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1042 | output = decoder.decode(b"", True) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1043 | if output: |
| 1044 | yield output |
Marc-André Lemburg | aa32c5a | 2001-09-19 11:24:48 +0000 | [diff] [blame] | 1045 | |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 1046 | ### Helpers for charmap-based codecs |
| 1047 | |
| 1048 | def make_identity_dict(rng): |
| 1049 | |
| 1050 | """ make_identity_dict(rng) -> dict |
| 1051 | |
| 1052 | Return a dictionary where elements of the rng sequence are |
| 1053 | mapped to themselves. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 1054 | |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 1055 | """ |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 1056 | return {i:i for i in rng} |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 1057 | |
Marc-André Lemburg | 716cf91 | 2001-05-16 09:41:45 +0000 | [diff] [blame] | 1058 | def make_encoding_map(decoding_map): |
| 1059 | |
| 1060 | """ Creates an encoding map from a decoding map. |
| 1061 | |
Walter Dörwald | 7f3ed74 | 2003-02-02 23:08:27 +0000 | [diff] [blame] | 1062 | If a target mapping in the decoding map occurs multiple |
Marc-André Lemburg | 716cf91 | 2001-05-16 09:41:45 +0000 | [diff] [blame] | 1063 | times, then that target is mapped to None (undefined mapping), |
| 1064 | causing an exception when encountered by the charmap codec |
| 1065 | during translation. |
| 1066 | |
| 1067 | One example where this happens is cp875.py which decodes |
Serhiy Storchaka | 9f8a891 | 2015-04-03 18:12:41 +0300 | [diff] [blame] | 1068 | multiple character to \\u001a. |
Marc-André Lemburg | 716cf91 | 2001-05-16 09:41:45 +0000 | [diff] [blame] | 1069 | |
| 1070 | """ |
| 1071 | m = {} |
| 1072 | for k,v in decoding_map.items(): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1073 | if not v in m: |
Marc-André Lemburg | 716cf91 | 2001-05-16 09:41:45 +0000 | [diff] [blame] | 1074 | m[v] = k |
| 1075 | else: |
| 1076 | m[v] = None |
| 1077 | return m |
Tim Peters | 3a2ab1a | 2001-05-29 06:06:54 +0000 | [diff] [blame] | 1078 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1079 | ### error handlers |
| 1080 | |
Martin v. Löwis | e2713be | 2005-03-08 15:03:08 +0000 | [diff] [blame] | 1081 | try: |
| 1082 | strict_errors = lookup_error("strict") |
| 1083 | ignore_errors = lookup_error("ignore") |
| 1084 | replace_errors = lookup_error("replace") |
| 1085 | xmlcharrefreplace_errors = lookup_error("xmlcharrefreplace") |
| 1086 | backslashreplace_errors = lookup_error("backslashreplace") |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 1087 | namereplace_errors = lookup_error("namereplace") |
Martin v. Löwis | e2713be | 2005-03-08 15:03:08 +0000 | [diff] [blame] | 1088 | except LookupError: |
| 1089 | # In --disable-unicode builds, these error handler are missing |
| 1090 | strict_errors = None |
| 1091 | ignore_errors = None |
| 1092 | replace_errors = None |
| 1093 | xmlcharrefreplace_errors = None |
| 1094 | backslashreplace_errors = None |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 1095 | namereplace_errors = None |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1096 | |
Martin v. Löwis | 6cd441d | 2001-07-31 08:54:55 +0000 | [diff] [blame] | 1097 | # Tell modulefinder that using codecs probably needs the encodings |
| 1098 | # package |
| 1099 | _false = 0 |
| 1100 | if _false: |
| 1101 | import encodings |
| 1102 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 1103 | ### Tests |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 1104 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 1105 | if __name__ == '__main__': |
| 1106 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 1107 | # Make stdout translate Latin-1 output into UTF-8 output |
| 1108 | sys.stdout = EncodedFile(sys.stdout, 'latin-1', 'utf-8') |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 1109 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 1110 | # Have stdin translate Latin-1 input into UTF-8 input |
| 1111 | sys.stdin = EncodedFile(sys.stdin, 'utf-8', 'latin-1') |