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