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 | |
Marc-André Lemburg | b28de0d | 2002-12-12 17:37:50 +0000 | [diff] [blame] | 10 | import __builtin__, 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 * |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 16 | except ImportError, why: |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 17 | raise SystemError,\ |
| 18 | 'Failed to load the builtin codecs: %s' % why |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 19 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 20 | __all__ = ["register", "lookup", "open", "EncodedFile", "BOM", "BOM_BE", |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 21 | "BOM_LE", "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE", |
| 22 | "BOM_UTF8", "BOM_UTF16", "BOM_UTF16_LE", "BOM_UTF16_BE", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 23 | "BOM_UTF32", "BOM_UTF32_LE", "BOM_UTF32_BE", |
| 24 | "strict_errors", "ignore_errors", "replace_errors", |
| 25 | "xmlcharrefreplace_errors", |
| 26 | "register_error", "lookup_error"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 28 | ### Constants |
| 29 | |
| 30 | # |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 31 | # Byte Order Mark (BOM = ZERO WIDTH NO-BREAK SPACE = U+FEFF) |
| 32 | # and its possible byte string values |
| 33 | # for UTF8/UTF16/UTF32 output and little/big endian machines |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 34 | # |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 35 | |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 36 | # UTF-8 |
| 37 | BOM_UTF8 = '\xef\xbb\xbf' |
| 38 | |
| 39 | # UTF-16, little endian |
| 40 | BOM_LE = BOM_UTF16_LE = '\xff\xfe' |
| 41 | |
| 42 | # UTF-16, big endian |
| 43 | BOM_BE = BOM_UTF16_BE = '\xfe\xff' |
| 44 | |
| 45 | # UTF-32, little endian |
| 46 | BOM_UTF32_LE = '\xff\xfe\x00\x00' |
| 47 | |
| 48 | # UTF-32, big endian |
| 49 | BOM_UTF32_BE = '\x00\x00\xfe\xff' |
| 50 | |
Marc-André Lemburg | b28de0d | 2002-12-12 17:37:50 +0000 | [diff] [blame] | 51 | if sys.byteorder == 'little': |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 52 | |
Marc-André Lemburg | b28de0d | 2002-12-12 17:37:50 +0000 | [diff] [blame] | 53 | # UTF-16, native endianness |
| 54 | BOM = BOM_UTF16 = BOM_UTF16_LE |
| 55 | |
| 56 | # UTF-32, native endianness |
| 57 | BOM_UTF32 = BOM_UTF32_LE |
| 58 | |
| 59 | else: |
| 60 | |
| 61 | # UTF-16, native endianness |
| 62 | BOM = BOM_UTF16 = BOM_UTF16_BE |
| 63 | |
| 64 | # UTF-32, native endianness |
| 65 | BOM_UTF32 = BOM_UTF32_BE |
Walter Dörwald | 474458d | 2002-06-04 15:16:29 +0000 | [diff] [blame] | 66 | |
| 67 | # Old broken names (don't use in new code) |
| 68 | BOM32_LE = BOM_UTF16_LE |
| 69 | BOM32_BE = BOM_UTF16_BE |
| 70 | BOM64_LE = BOM_UTF32_LE |
| 71 | BOM64_BE = BOM_UTF32_BE |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | ### Codec base classes (defining the API) |
| 75 | |
| 76 | class Codec: |
| 77 | |
| 78 | """ Defines the interface for stateless encoders/decoders. |
| 79 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 80 | The .encode()/.decode() methods may use different error |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 81 | handling schemes by providing the errors argument. These |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 82 | string values are predefined: |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 84 | 'strict' - raise a ValueError error (or a subclass) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 85 | 'ignore' - ignore the character and continue with the next |
| 86 | 'replace' - replace with a suitable replacement character; |
| 87 | Python will use the official U+FFFD REPLACEMENT |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 88 | CHARACTER for the builtin Unicode codecs on |
| 89 | decoding and '?' on encoding. |
| 90 | 'xmlcharrefreplace' - Replace with the appropriate XML |
| 91 | character reference (only for encoding). |
| 92 | 'backslashreplace' - Replace with backslashed escape sequences |
| 93 | (only for encoding). |
| 94 | |
| 95 | The set of allowed values can be extended via register_error. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 96 | |
| 97 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 98 | def encode(self, input, errors='strict'): |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 99 | |
Fred Drake | 3e74c0d | 2000-03-17 15:40:35 +0000 | [diff] [blame] | 100 | """ Encodes the object input and returns a tuple (output |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 101 | object, length consumed). |
| 102 | |
| 103 | errors defines the error handling to apply. It defaults to |
| 104 | 'strict' handling. |
| 105 | |
| 106 | The method may not store state in the Codec instance. Use |
| 107 | StreamCodec for codecs which have to keep state in order to |
| 108 | make encoding/decoding efficient. |
| 109 | |
| 110 | The encoder must be able to handle zero length input and |
| 111 | return an empty object of the output object type in this |
| 112 | situation. |
| 113 | |
| 114 | """ |
| 115 | raise NotImplementedError |
| 116 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 117 | def decode(self, input, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 118 | |
| 119 | """ Decodes the object input and returns a tuple (output |
| 120 | object, length consumed). |
| 121 | |
| 122 | input must be an object which provides the bf_getreadbuf |
| 123 | buffer slot. Python strings, buffer objects and memory |
| 124 | mapped files are examples of objects providing this slot. |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 126 | errors defines the error handling to apply. It defaults to |
| 127 | 'strict' handling. |
| 128 | |
| 129 | The method may not store state in the Codec instance. Use |
| 130 | StreamCodec for codecs which have to keep state in order to |
| 131 | make encoding/decoding efficient. |
| 132 | |
| 133 | The decoder must be able to handle zero length input and |
| 134 | return an empty object of the output object type in this |
| 135 | situation. |
| 136 | |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 137 | """ |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 138 | raise NotImplementedError |
| 139 | |
| 140 | # |
| 141 | # The StreamWriter and StreamReader class provide generic working |
Andrew M. Kuchling | 97c5635 | 2001-09-18 20:29:48 +0000 | [diff] [blame] | 142 | # interfaces which can be used to implement new encoding submodules |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 143 | # very easily. See encodings/utf_8.py for an example on how this is |
| 144 | # done. |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 145 | # |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 146 | |
| 147 | class StreamWriter(Codec): |
| 148 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 149 | def __init__(self, stream, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 150 | |
| 151 | """ Creates a StreamWriter instance. |
| 152 | |
| 153 | stream must be a file-like object open for writing |
| 154 | (binary) data. |
| 155 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 156 | The StreamWriter may use different error handling |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 157 | schemes by providing the errors keyword argument. These |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 158 | parameters are predefined: |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 159 | |
| 160 | 'strict' - raise a ValueError (or a subclass) |
| 161 | 'ignore' - ignore the character and continue with the next |
| 162 | 'replace'- replace with a suitable replacement character |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 163 | 'xmlcharrefreplace' - Replace with the appropriate XML |
| 164 | character reference. |
| 165 | 'backslashreplace' - Replace with backslashed escape |
| 166 | sequences (only for encoding). |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 167 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 168 | The set of allowed parameter values can be extended via |
| 169 | register_error. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 170 | """ |
| 171 | self.stream = stream |
| 172 | self.errors = errors |
| 173 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 174 | def write(self, object): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 175 | |
| 176 | """ Writes the object's contents encoded to self.stream. |
| 177 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 178 | data, consumed = self.encode(object, self.errors) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 179 | self.stream.write(data) |
| 180 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 181 | def writelines(self, list): |
| 182 | |
| 183 | """ Writes the concatenated list of strings to the stream |
| 184 | using .write(). |
| 185 | """ |
| 186 | self.write(''.join(list)) |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 188 | def reset(self): |
| 189 | |
| 190 | """ Flushes and resets the codec buffers used for keeping state. |
| 191 | |
| 192 | Calling this method should ensure that the data on the |
| 193 | output is put into a clean state, that allows appending |
| 194 | of new fresh data without having to rescan the whole |
| 195 | stream to recover state. |
| 196 | |
| 197 | """ |
| 198 | pass |
| 199 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 200 | def __getattr__(self, name, |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 201 | getattr=getattr): |
| 202 | |
| 203 | """ Inherit all other methods from the underlying stream. |
| 204 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 205 | return getattr(self.stream, name) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 206 | |
| 207 | ### |
| 208 | |
| 209 | class StreamReader(Codec): |
| 210 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 211 | def __init__(self, stream, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 212 | |
| 213 | """ Creates a StreamReader instance. |
| 214 | |
| 215 | stream must be a file-like object open for reading |
| 216 | (binary) data. |
| 217 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 218 | The StreamReader may use different error handling |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 219 | schemes by providing the errors keyword argument. These |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 220 | parameters are predefined: |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 221 | |
| 222 | 'strict' - raise a ValueError (or a subclass) |
| 223 | 'ignore' - ignore the character and continue with the next |
| 224 | 'replace'- replace with a suitable replacement character; |
| 225 | |
Walter Dörwald | 7f82f79 | 2002-11-19 21:42:53 +0000 | [diff] [blame] | 226 | The set of allowed parameter values can be extended via |
| 227 | register_error. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 228 | """ |
| 229 | self.stream = stream |
| 230 | self.errors = errors |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 231 | self.bytebuffer = "" |
| 232 | self.charbuffer = u"" |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 233 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 234 | def decode(self, input, errors='strict'): |
| 235 | raise NotImplementedError |
| 236 | |
| 237 | def read(self, size=-1, chars=-1): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 238 | |
| 239 | """ Decodes data from the stream self.stream and returns the |
| 240 | resulting object. |
| 241 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 242 | chars indicates the number of characters to read from the |
| 243 | stream. read() will never return more than chars |
| 244 | characters, but it might return less, if there are not enough |
| 245 | characters available. |
| 246 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 247 | size indicates the approximate maximum number of bytes to |
| 248 | read from the stream for decoding purposes. The decoder |
| 249 | can modify this setting as appropriate. The default value |
| 250 | -1 indicates to read and decode as much as possible. size |
| 251 | is intended to prevent having to decode huge files in one |
| 252 | step. |
| 253 | |
| 254 | The method should use a greedy read strategy meaning that |
| 255 | it should read as much data as is allowed within the |
| 256 | definition of the encoding and the given size, e.g. if |
| 257 | optional encoding endings or state markers are available |
| 258 | on the stream, these should be read too. |
| 259 | |
| 260 | """ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 261 | # read until we get the required number of characters (if available) |
| 262 | done = False |
| 263 | while True: |
| 264 | # can the request can be satisfied from the character buffer? |
| 265 | if chars < 0: |
| 266 | if self.charbuffer: |
| 267 | done = True |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 268 | else: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 269 | if len(self.charbuffer) >= chars: |
| 270 | done = True |
| 271 | if done: |
| 272 | if chars < 0: |
| 273 | result = self.charbuffer |
| 274 | self.charbuffer = u"" |
| 275 | break |
| 276 | else: |
| 277 | result = self.charbuffer[:chars] |
| 278 | self.charbuffer = self.charbuffer[chars:] |
| 279 | break |
| 280 | # we need more data |
| 281 | if size < 0: |
| 282 | newdata = self.stream.read() |
| 283 | else: |
| 284 | newdata = self.stream.read(size) |
| 285 | data = self.bytebuffer + newdata |
| 286 | object, decodedbytes = self.decode(data, self.errors) |
| 287 | # keep undecoded bytes until the next call |
| 288 | self.bytebuffer = data[decodedbytes:] |
| 289 | # put new characters in the character buffer |
| 290 | self.charbuffer += object |
| 291 | # there was no data available |
| 292 | if not newdata: |
| 293 | done = True |
| 294 | return result |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 295 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 296 | def readline(self, size=None, keepends=True): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 297 | |
| 298 | """ Read one line from the input stream and return the |
| 299 | decoded data. |
| 300 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 301 | size, if given, is passed as size argument to the |
| 302 | read() method. |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 303 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 304 | """ |
| 305 | if size is None: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 306 | size = 10 |
| 307 | line = u"" |
| 308 | while True: |
| 309 | data = self.read(size) |
| 310 | line += data |
| 311 | pos = line.find("\n") |
| 312 | if pos>=0: |
| 313 | self.charbuffer = line[pos+1:] + self.charbuffer |
| 314 | if keepends: |
| 315 | line = line[:pos+1] |
| 316 | else: |
| 317 | line = line[:pos] |
| 318 | return line |
| 319 | elif not data: |
| 320 | return line |
| 321 | if size<8000: |
| 322 | size *= 2 |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 323 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 324 | def readlines(self, sizehint=None, keepends=True): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 325 | |
| 326 | """ Read all lines available on the input stream |
| 327 | and return them as list of lines. |
| 328 | |
| 329 | Line breaks are implemented using the codec's decoder |
| 330 | method and are included in the list entries. |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 331 | |
Marc-André Lemburg | d594849 | 2004-02-26 15:22:17 +0000 | [diff] [blame] | 332 | sizehint, if given, is ignored since there is no efficient |
| 333 | way to finding the true end-of-line. |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 334 | |
| 335 | """ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 336 | data = self.read() |
| 337 | return self.splitlines(keepends) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 338 | |
| 339 | def reset(self): |
| 340 | |
| 341 | """ Resets the codec buffers used for keeping state. |
| 342 | |
| 343 | Note that no stream repositioning should take place. |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 344 | This method is primarily intended to be able to recover |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 345 | from decoding errors. |
| 346 | |
| 347 | """ |
| 348 | pass |
| 349 | |
Walter Dörwald | 4dbf192 | 2002-11-06 16:53:44 +0000 | [diff] [blame] | 350 | def next(self): |
| 351 | |
| 352 | """ Return the next decoded line from the input stream.""" |
| 353 | line = self.readline() |
| 354 | if line: |
| 355 | return line |
| 356 | raise StopIteration |
| 357 | |
| 358 | def __iter__(self): |
| 359 | return self |
| 360 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 361 | def __getattr__(self, name, |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 362 | getattr=getattr): |
| 363 | |
| 364 | """ Inherit all other methods from the underlying stream. |
| 365 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 366 | return getattr(self.stream, name) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 367 | |
| 368 | ### |
| 369 | |
| 370 | class StreamReaderWriter: |
| 371 | |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 372 | """ StreamReaderWriter instances allow wrapping streams which |
| 373 | work in both read and write modes. |
| 374 | |
| 375 | The design is such that one can use the factory functions |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 376 | returned by the codec.lookup() function to construct the |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 377 | instance. |
| 378 | |
| 379 | """ |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 380 | # Optional attributes set by the file wrappers below |
| 381 | encoding = 'unknown' |
| 382 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 383 | def __init__(self, stream, Reader, Writer, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 384 | |
| 385 | """ Creates a StreamReaderWriter instance. |
| 386 | |
| 387 | stream must be a Stream-like object. |
| 388 | |
| 389 | Reader, Writer must be factory functions or classes |
| 390 | providing the StreamReader, StreamWriter interface resp. |
| 391 | |
| 392 | Error handling is done in the same way as defined for the |
| 393 | StreamWriter/Readers. |
| 394 | |
| 395 | """ |
| 396 | self.stream = stream |
| 397 | self.reader = Reader(stream, errors) |
| 398 | self.writer = Writer(stream, errors) |
| 399 | self.errors = errors |
| 400 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 401 | def read(self, size=-1): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 402 | |
| 403 | return self.reader.read(size) |
| 404 | |
Guido van Rossum | d58c26f | 2000-05-01 16:17:32 +0000 | [diff] [blame] | 405 | def readline(self, size=None): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 406 | |
| 407 | return self.reader.readline(size) |
| 408 | |
Guido van Rossum | d58c26f | 2000-05-01 16:17:32 +0000 | [diff] [blame] | 409 | def readlines(self, sizehint=None): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 410 | |
| 411 | return self.reader.readlines(sizehint) |
| 412 | |
Walter Dörwald | 4dbf192 | 2002-11-06 16:53:44 +0000 | [diff] [blame] | 413 | def next(self): |
| 414 | |
| 415 | """ Return the next decoded line from the input stream.""" |
| 416 | return self.reader.next() |
| 417 | |
| 418 | def __iter__(self): |
| 419 | return self |
| 420 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 421 | def write(self, data): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 422 | |
| 423 | return self.writer.write(data) |
| 424 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 425 | def writelines(self, list): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 426 | |
| 427 | return self.writer.writelines(list) |
| 428 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 429 | def reset(self): |
| 430 | |
| 431 | self.reader.reset() |
| 432 | self.writer.reset() |
| 433 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 434 | def __getattr__(self, name, |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 435 | getattr=getattr): |
| 436 | |
| 437 | """ Inherit all other methods from the underlying stream. |
| 438 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 439 | return getattr(self.stream, name) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 440 | |
| 441 | ### |
| 442 | |
| 443 | class StreamRecoder: |
| 444 | |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 445 | """ StreamRecoder instances provide a frontend - backend |
| 446 | view of encoding data. |
| 447 | |
| 448 | They use the complete set of APIs returned by the |
| 449 | codecs.lookup() function to implement their task. |
| 450 | |
| 451 | Data written to the stream is first decoded into an |
| 452 | intermediate format (which is dependent on the given codec |
| 453 | combination) and then written to the stream using an instance |
| 454 | of the provided Writer class. |
| 455 | |
| 456 | In the other direction, data is read from the stream using a |
| 457 | Reader instance and then return encoded data to the caller. |
| 458 | |
| 459 | """ |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 460 | # Optional attributes set by the file wrappers below |
| 461 | data_encoding = 'unknown' |
| 462 | file_encoding = 'unknown' |
| 463 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 464 | def __init__(self, stream, encode, decode, Reader, Writer, |
| 465 | errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 466 | |
| 467 | """ Creates a StreamRecoder instance which implements a two-way |
| 468 | conversion: encode and decode work on the frontend (the |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 469 | input to .read() and output of .write()) while |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 470 | Reader and Writer work on the backend (reading and |
Fred Drake | 908670c | 2000-03-17 15:42:11 +0000 | [diff] [blame] | 471 | writing to the stream). |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 472 | |
| 473 | You can use these objects to do transparent direct |
| 474 | recodings from e.g. latin-1 to utf-8 and back. |
| 475 | |
| 476 | stream must be a file-like object. |
| 477 | |
| 478 | encode, decode must adhere to the Codec interface, Reader, |
| 479 | Writer must be factory functions or classes providing the |
| 480 | StreamReader, StreamWriter interface resp. |
| 481 | |
| 482 | encode and decode are needed for the frontend translation, |
| 483 | Reader and Writer for the backend translation. Unicode is |
| 484 | used as intermediate encoding. |
| 485 | |
| 486 | Error handling is done in the same way as defined for the |
| 487 | StreamWriter/Readers. |
| 488 | |
| 489 | """ |
| 490 | self.stream = stream |
| 491 | self.encode = encode |
| 492 | self.decode = decode |
| 493 | self.reader = Reader(stream, errors) |
| 494 | self.writer = Writer(stream, errors) |
| 495 | self.errors = errors |
| 496 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 497 | def read(self, size=-1): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 498 | |
| 499 | data = self.reader.read(size) |
| 500 | data, bytesencoded = self.encode(data, self.errors) |
| 501 | return data |
| 502 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 503 | def readline(self, size=None): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 504 | |
| 505 | if size is None: |
| 506 | data = self.reader.readline() |
| 507 | else: |
| 508 | data = self.reader.readline(size) |
| 509 | data, bytesencoded = self.encode(data, self.errors) |
| 510 | return data |
| 511 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 512 | def readlines(self, sizehint=None): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 513 | |
Marc-André Lemburg | d594849 | 2004-02-26 15:22:17 +0000 | [diff] [blame] | 514 | data = self.reader.read() |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 515 | data, bytesencoded = self.encode(data, self.errors) |
| 516 | return data.splitlines(1) |
| 517 | |
Walter Dörwald | 4dbf192 | 2002-11-06 16:53:44 +0000 | [diff] [blame] | 518 | def next(self): |
| 519 | |
| 520 | """ Return the next decoded line from the input stream.""" |
| 521 | return self.reader.next() |
| 522 | |
| 523 | def __iter__(self): |
| 524 | return self |
| 525 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 526 | def write(self, data): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 527 | |
| 528 | data, bytesdecoded = self.decode(data, self.errors) |
| 529 | return self.writer.write(data) |
| 530 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 531 | def writelines(self, list): |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 532 | |
| 533 | data = ''.join(list) |
| 534 | data, bytesdecoded = self.decode(data, self.errors) |
| 535 | return self.writer.write(data) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 536 | |
| 537 | def reset(self): |
| 538 | |
| 539 | self.reader.reset() |
| 540 | self.writer.reset() |
| 541 | |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 542 | def __getattr__(self, name, |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 543 | getattr=getattr): |
| 544 | |
| 545 | """ Inherit all other methods from the underlying stream. |
| 546 | """ |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 547 | return getattr(self.stream, name) |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 548 | |
| 549 | ### Shortcuts |
| 550 | |
Marc-André Lemburg | 349a3d3 | 2000-06-21 21:21:04 +0000 | [diff] [blame] | 551 | def open(filename, mode='rb', encoding=None, errors='strict', buffering=1): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 552 | |
| 553 | """ Open an encoded file using the given mode and return |
| 554 | a wrapped version providing transparent encoding/decoding. |
| 555 | |
| 556 | Note: The wrapped version will only accept the object format |
| 557 | defined by the codecs, i.e. Unicode objects for most builtin |
| 558 | codecs. Output is also codec dependent and will usually by |
| 559 | Unicode as well. |
| 560 | |
Marc-André Lemburg | 349a3d3 | 2000-06-21 21:21:04 +0000 | [diff] [blame] | 561 | 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] | 562 | 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] | 563 | using 8-bit values. The default file mode is 'rb' meaning to |
| 564 | open the file in binary read mode. |
| 565 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 566 | encoding specifies the encoding which is to be used for the |
Walter Dörwald | 7f3ed74 | 2003-02-02 23:08:27 +0000 | [diff] [blame] | 567 | file. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 568 | |
| 569 | errors may be given to define the error handling. It defaults |
| 570 | to 'strict' which causes ValueErrors to be raised in case an |
| 571 | encoding error occurs. |
| 572 | |
| 573 | buffering has the same meaning as for the builtin open() API. |
| 574 | It defaults to line buffered. |
| 575 | |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 576 | The returned wrapped file object provides an extra attribute |
| 577 | .encoding which allows querying the used encoding. This |
| 578 | attribute is only available if an encoding was specified as |
| 579 | parameter. |
| 580 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 581 | """ |
| 582 | if encoding is not None and \ |
| 583 | 'b' not in mode: |
| 584 | # Force opening of the file in binary mode |
| 585 | mode = mode + 'b' |
| 586 | file = __builtin__.open(filename, mode, buffering) |
| 587 | if encoding is None: |
| 588 | return file |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 589 | (e, d, sr, sw) = lookup(encoding) |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 590 | srw = StreamReaderWriter(file, sr, sw, errors) |
| 591 | # Add attributes to simplify introspection |
| 592 | srw.encoding = encoding |
| 593 | return srw |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 594 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 595 | def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'): |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 596 | |
| 597 | """ Return a wrapped version of file which provides transparent |
| 598 | encoding translation. |
| 599 | |
| 600 | Strings written to the wrapped file are interpreted according |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 601 | to the given data_encoding and then written to the original |
| 602 | file as string using file_encoding. The intermediate encoding |
| 603 | will usually be Unicode but depends on the specified codecs. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 604 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 605 | Strings are read from the file using file_encoding and then |
| 606 | passed back to the caller as string using data_encoding. |
| 607 | |
| 608 | If file_encoding is not given, it defaults to data_encoding. |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 609 | |
| 610 | errors may be given to define the error handling. It defaults |
| 611 | to 'strict' which causes ValueErrors to be raised in case an |
| 612 | encoding error occurs. |
| 613 | |
Fred Drake | 49fd107 | 2000-04-13 14:11:21 +0000 | [diff] [blame] | 614 | The returned wrapped file object provides two extra attributes |
| 615 | .data_encoding and .file_encoding which reflect the given |
| 616 | parameters of the same name. The attributes can be used for |
| 617 | introspection by Python programs. |
| 618 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 619 | """ |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 620 | if file_encoding is None: |
| 621 | file_encoding = data_encoding |
| 622 | encode, decode = lookup(data_encoding)[:2] |
| 623 | Reader, Writer = lookup(file_encoding)[2:] |
| 624 | sr = StreamRecoder(file, |
Tim Peters | 30324a7 | 2001-05-15 17:19:16 +0000 | [diff] [blame] | 625 | encode, decode, Reader, Writer, |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 626 | errors) |
| 627 | # Add attributes to simplify introspection |
| 628 | sr.data_encoding = data_encoding |
| 629 | sr.file_encoding = file_encoding |
| 630 | return sr |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 631 | |
Marc-André Lemburg | aa32c5a | 2001-09-19 11:24:48 +0000 | [diff] [blame] | 632 | ### Helpers for codec lookup |
| 633 | |
| 634 | def getencoder(encoding): |
| 635 | |
| 636 | """ Lookup up the codec for the given encoding and return |
| 637 | its encoder function. |
| 638 | |
| 639 | Raises a LookupError in case the encoding cannot be found. |
| 640 | |
| 641 | """ |
| 642 | return lookup(encoding)[0] |
| 643 | |
| 644 | def getdecoder(encoding): |
| 645 | |
| 646 | """ Lookup up the codec for the given encoding and return |
| 647 | its decoder function. |
| 648 | |
| 649 | Raises a LookupError in case the encoding cannot be found. |
| 650 | |
| 651 | """ |
| 652 | return lookup(encoding)[1] |
| 653 | |
| 654 | def getreader(encoding): |
| 655 | |
| 656 | """ Lookup up the codec for the given encoding and return |
| 657 | its StreamReader class or factory function. |
| 658 | |
| 659 | Raises a LookupError in case the encoding cannot be found. |
| 660 | |
| 661 | """ |
| 662 | return lookup(encoding)[2] |
| 663 | |
| 664 | def getwriter(encoding): |
| 665 | |
| 666 | """ Lookup up the codec for the given encoding and return |
| 667 | its StreamWriter class or factory function. |
| 668 | |
| 669 | Raises a LookupError in case the encoding cannot be found. |
| 670 | |
| 671 | """ |
| 672 | return lookup(encoding)[3] |
| 673 | |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 674 | ### Helpers for charmap-based codecs |
| 675 | |
| 676 | def make_identity_dict(rng): |
| 677 | |
| 678 | """ make_identity_dict(rng) -> dict |
| 679 | |
| 680 | Return a dictionary where elements of the rng sequence are |
| 681 | mapped to themselves. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 682 | |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 683 | """ |
| 684 | res = {} |
| 685 | for i in rng: |
| 686 | res[i]=i |
| 687 | return res |
| 688 | |
Marc-André Lemburg | 716cf91 | 2001-05-16 09:41:45 +0000 | [diff] [blame] | 689 | def make_encoding_map(decoding_map): |
| 690 | |
| 691 | """ Creates an encoding map from a decoding map. |
| 692 | |
Walter Dörwald | 7f3ed74 | 2003-02-02 23:08:27 +0000 | [diff] [blame] | 693 | If a target mapping in the decoding map occurs multiple |
Marc-André Lemburg | 716cf91 | 2001-05-16 09:41:45 +0000 | [diff] [blame] | 694 | times, then that target is mapped to None (undefined mapping), |
| 695 | causing an exception when encountered by the charmap codec |
| 696 | during translation. |
| 697 | |
| 698 | One example where this happens is cp875.py which decodes |
| 699 | multiple character to \u001a. |
| 700 | |
| 701 | """ |
| 702 | m = {} |
| 703 | for k,v in decoding_map.items(): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 704 | if not v in m: |
Marc-André Lemburg | 716cf91 | 2001-05-16 09:41:45 +0000 | [diff] [blame] | 705 | m[v] = k |
| 706 | else: |
| 707 | m[v] = None |
| 708 | return m |
Tim Peters | 3a2ab1a | 2001-05-29 06:06:54 +0000 | [diff] [blame] | 709 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 710 | ### error handlers |
| 711 | |
| 712 | strict_errors = lookup_error("strict") |
| 713 | ignore_errors = lookup_error("ignore") |
| 714 | replace_errors = lookup_error("replace") |
| 715 | xmlcharrefreplace_errors = lookup_error("xmlcharrefreplace") |
| 716 | backslashreplace_errors = lookup_error("backslashreplace") |
| 717 | |
Martin v. Löwis | 6cd441d | 2001-07-31 08:54:55 +0000 | [diff] [blame] | 718 | # Tell modulefinder that using codecs probably needs the encodings |
| 719 | # package |
| 720 | _false = 0 |
| 721 | if _false: |
| 722 | import encodings |
| 723 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 724 | ### Tests |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 725 | |
Guido van Rossum | 0612d84 | 2000-03-10 23:20:43 +0000 | [diff] [blame] | 726 | if __name__ == '__main__': |
| 727 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 728 | # Make stdout translate Latin-1 output into UTF-8 output |
| 729 | sys.stdout = EncodedFile(sys.stdout, 'latin-1', 'utf-8') |
Guido van Rossum | 1c89b0e | 2000-04-11 15:41:38 +0000 | [diff] [blame] | 730 | |
Guido van Rossum | a327713 | 2000-04-11 15:37:43 +0000 | [diff] [blame] | 731 | # Have stdin translate Latin-1 input into UTF-8 input |
| 732 | sys.stdin = EncodedFile(sys.stdin, 'utf-8', 'latin-1') |