Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`codecs` --- Codec registry and base classes |
| 2 | ================================================= |
| 3 | |
| 4 | .. module:: codecs |
| 5 | :synopsis: Encode and decode data and streams. |
| 6 | .. moduleauthor:: Marc-Andre Lemburg <mal@lemburg.com> |
| 7 | .. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com> |
| 8 | .. sectionauthor:: Martin v. Löwis <martin@v.loewis.de> |
| 9 | |
| 10 | |
| 11 | .. index:: |
| 12 | single: Unicode |
| 13 | single: Codecs |
| 14 | pair: Codecs; encode |
| 15 | pair: Codecs; decode |
| 16 | single: streams |
| 17 | pair: stackable; streams |
| 18 | |
| 19 | This module defines base classes for standard Python codecs (encoders and |
| 20 | decoders) and provides access to the internal Python codec registry which |
| 21 | manages the codec and error handling lookup process. |
| 22 | |
| 23 | It defines the following functions: |
| 24 | |
| 25 | |
| 26 | .. function:: register(search_function) |
| 27 | |
| 28 | Register a codec search function. Search functions are expected to take one |
| 29 | argument, the encoding name in all lower case letters, and return a |
| 30 | :class:`CodecInfo` object having the following attributes: |
| 31 | |
| 32 | * ``name`` The name of the encoding; |
| 33 | |
Walter Dörwald | 62073e0 | 2008-10-23 13:21:33 +0000 | [diff] [blame] | 34 | * ``encode`` The stateless encoding function; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
Walter Dörwald | 62073e0 | 2008-10-23 13:21:33 +0000 | [diff] [blame] | 36 | * ``decode`` The stateless decoding function; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
| 38 | * ``incrementalencoder`` An incremental encoder class or factory function; |
| 39 | |
| 40 | * ``incrementaldecoder`` An incremental decoder class or factory function; |
| 41 | |
| 42 | * ``streamwriter`` A stream writer class or factory function; |
| 43 | |
| 44 | * ``streamreader`` A stream reader class or factory function. |
| 45 | |
| 46 | The various functions or classes take the following arguments: |
| 47 | |
Walter Dörwald | 62073e0 | 2008-10-23 13:21:33 +0000 | [diff] [blame] | 48 | *encode* and *decode*: These must be functions or methods which have the same |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | interface as the :meth:`encode`/:meth:`decode` methods of Codec instances (see |
| 50 | Codec Interface). The functions/methods are expected to work in a stateless |
| 51 | mode. |
| 52 | |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 53 | *incrementalencoder* and *incrementaldecoder*: These have to be factory |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | functions providing the following interface: |
| 55 | |
| 56 | ``factory(errors='strict')`` |
| 57 | |
| 58 | The factory functions must return objects providing the interfaces defined by |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 59 | the base classes :class:`IncrementalEncoder` and :class:`IncrementalDecoder`, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | respectively. Incremental codecs can maintain state. |
| 61 | |
| 62 | *streamreader* and *streamwriter*: These have to be factory functions providing |
| 63 | the following interface: |
| 64 | |
| 65 | ``factory(stream, errors='strict')`` |
| 66 | |
| 67 | The factory functions must return objects providing the interfaces defined by |
| 68 | the base classes :class:`StreamWriter` and :class:`StreamReader`, respectively. |
| 69 | Stream codecs can maintain state. |
| 70 | |
| 71 | Possible values for errors are ``'strict'`` (raise an exception in case of an |
| 72 | encoding error), ``'replace'`` (replace malformed data with a suitable |
| 73 | replacement marker, such as ``'?'``), ``'ignore'`` (ignore malformed data and |
| 74 | continue without further notice), ``'xmlcharrefreplace'`` (replace with the |
Georg Brandl | 090e30f | 2009-08-06 17:51:03 +0000 | [diff] [blame] | 75 | appropriate XML character reference (for encoding only)), |
| 76 | ``'backslashreplace'`` (replace with backslashed escape sequences (for |
Philip Jenvey | 5a54539 | 2009-08-06 20:00:08 +0000 | [diff] [blame] | 77 | encoding only)), ``'surrogateescape'`` (replace with surrogate U+DCxx, see |
Georg Brandl | 090e30f | 2009-08-06 17:51:03 +0000 | [diff] [blame] | 78 | :pep:`383`) as well as any other error handling name defined via |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | :func:`register_error`. |
| 80 | |
| 81 | In case a search function cannot find a given encoding, it should return |
| 82 | ``None``. |
| 83 | |
| 84 | |
| 85 | .. function:: lookup(encoding) |
| 86 | |
| 87 | Looks up the codec info in the Python codec registry and returns a |
| 88 | :class:`CodecInfo` object as defined above. |
| 89 | |
| 90 | Encodings are first looked up in the registry's cache. If not found, the list of |
| 91 | registered search functions is scanned. If no :class:`CodecInfo` object is |
| 92 | found, a :exc:`LookupError` is raised. Otherwise, the :class:`CodecInfo` object |
| 93 | is stored in the cache and returned to the caller. |
| 94 | |
| 95 | To simplify access to the various codecs, the module provides these additional |
| 96 | functions which use :func:`lookup` for the codec lookup: |
| 97 | |
| 98 | |
| 99 | .. function:: getencoder(encoding) |
| 100 | |
| 101 | Look up the codec for the given encoding and return its encoder function. |
| 102 | |
| 103 | Raises a :exc:`LookupError` in case the encoding cannot be found. |
| 104 | |
| 105 | |
| 106 | .. function:: getdecoder(encoding) |
| 107 | |
| 108 | Look up the codec for the given encoding and return its decoder function. |
| 109 | |
| 110 | Raises a :exc:`LookupError` in case the encoding cannot be found. |
| 111 | |
| 112 | |
| 113 | .. function:: getincrementalencoder(encoding) |
| 114 | |
| 115 | Look up the codec for the given encoding and return its incremental encoder |
| 116 | class or factory function. |
| 117 | |
| 118 | Raises a :exc:`LookupError` in case the encoding cannot be found or the codec |
| 119 | doesn't support an incremental encoder. |
| 120 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 121 | |
| 122 | .. function:: getincrementaldecoder(encoding) |
| 123 | |
| 124 | Look up the codec for the given encoding and return its incremental decoder |
| 125 | class or factory function. |
| 126 | |
| 127 | Raises a :exc:`LookupError` in case the encoding cannot be found or the codec |
| 128 | doesn't support an incremental decoder. |
| 129 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 130 | |
| 131 | .. function:: getreader(encoding) |
| 132 | |
| 133 | Look up the codec for the given encoding and return its StreamReader class or |
| 134 | factory function. |
| 135 | |
| 136 | Raises a :exc:`LookupError` in case the encoding cannot be found. |
| 137 | |
| 138 | |
| 139 | .. function:: getwriter(encoding) |
| 140 | |
| 141 | Look up the codec for the given encoding and return its StreamWriter class or |
| 142 | factory function. |
| 143 | |
| 144 | Raises a :exc:`LookupError` in case the encoding cannot be found. |
| 145 | |
| 146 | |
| 147 | .. function:: register_error(name, error_handler) |
| 148 | |
| 149 | Register the error handling function *error_handler* under the name *name*. |
| 150 | *error_handler* will be called during encoding and decoding in case of an error, |
| 151 | when *name* is specified as the errors parameter. |
| 152 | |
| 153 | For encoding *error_handler* will be called with a :exc:`UnicodeEncodeError` |
| 154 | instance, which contains information about the location of the error. The error |
| 155 | handler must either raise this or a different exception or return a tuple with a |
| 156 | replacement for the unencodable part of the input and a position where encoding |
| 157 | should continue. The encoder will encode the replacement and continue encoding |
| 158 | the original input at the specified position. Negative position values will be |
| 159 | treated as being relative to the end of the input string. If the resulting |
| 160 | position is out of bound an :exc:`IndexError` will be raised. |
| 161 | |
| 162 | Decoding and translating works similar, except :exc:`UnicodeDecodeError` or |
| 163 | :exc:`UnicodeTranslateError` will be passed to the handler and that the |
| 164 | replacement from the error handler will be put into the output directly. |
| 165 | |
| 166 | |
| 167 | .. function:: lookup_error(name) |
| 168 | |
| 169 | Return the error handler previously registered under the name *name*. |
| 170 | |
| 171 | Raises a :exc:`LookupError` in case the handler cannot be found. |
| 172 | |
| 173 | |
| 174 | .. function:: strict_errors(exception) |
| 175 | |
| 176 | Implements the ``strict`` error handling. |
| 177 | |
| 178 | |
| 179 | .. function:: replace_errors(exception) |
| 180 | |
| 181 | Implements the ``replace`` error handling. |
| 182 | |
| 183 | |
| 184 | .. function:: ignore_errors(exception) |
| 185 | |
| 186 | Implements the ``ignore`` error handling. |
| 187 | |
| 188 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 189 | .. function:: xmlcharrefreplace_errors(exception) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | |
| 191 | Implements the ``xmlcharrefreplace`` error handling. |
| 192 | |
| 193 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 194 | .. function:: backslashreplace_errors(exception) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 195 | |
| 196 | Implements the ``backslashreplace`` error handling. |
| 197 | |
| 198 | To simplify working with encoded files or stream, the module also defines these |
| 199 | utility functions: |
| 200 | |
| 201 | |
| 202 | .. function:: open(filename, mode[, encoding[, errors[, buffering]]]) |
| 203 | |
| 204 | Open an encoded file using the given *mode* and return a wrapped version |
Christian Heimes | 18c6689 | 2008-02-17 13:31:39 +0000 | [diff] [blame] | 205 | providing transparent encoding/decoding. The default file mode is ``'r'`` |
| 206 | meaning to open the file in read mode. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 207 | |
| 208 | .. note:: |
| 209 | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 210 | The wrapped version's methods will accept and return strings only. Bytes |
| 211 | arguments will be rejected. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 | |
Christian Heimes | 18c6689 | 2008-02-17 13:31:39 +0000 | [diff] [blame] | 213 | .. note:: |
| 214 | |
| 215 | Files are always opened in binary mode, even if no binary mode was |
| 216 | specified. This is done to avoid data loss due to encodings using 8-bit |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 217 | values. This means that no automatic conversion of ``b'\n'`` is done |
Christian Heimes | 18c6689 | 2008-02-17 13:31:39 +0000 | [diff] [blame] | 218 | on reading and writing. |
| 219 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 | *encoding* specifies the encoding which is to be used for the file. |
| 221 | |
| 222 | *errors* may be given to define the error handling. It defaults to ``'strict'`` |
| 223 | which causes a :exc:`ValueError` to be raised in case an encoding error occurs. |
| 224 | |
| 225 | *buffering* has the same meaning as for the built-in :func:`open` function. It |
| 226 | defaults to line buffered. |
| 227 | |
| 228 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 229 | .. function:: EncodedFile(file, data_encoding, file_encoding=None, errors='strict') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | |
| 231 | Return a wrapped version of file which provides transparent encoding |
| 232 | translation. |
| 233 | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 234 | Bytes written to the wrapped file are interpreted according to the given |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 235 | *data_encoding* and then written to the original file as bytes using the |
| 236 | *file_encoding*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 238 | If *file_encoding* is not given, it defaults to *data_encoding*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 240 | *errors* may be given to define the error handling. It defaults to |
| 241 | ``'strict'``, which causes :exc:`ValueError` to be raised in case an encoding |
| 242 | error occurs. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 243 | |
| 244 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 245 | .. function:: iterencode(iterator, encoding, errors='strict', **kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | |
| 247 | Uses an incremental encoder to iteratively encode the input provided by |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 248 | *iterator*. This function is a :term:`generator`. *errors* (as well as any |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 249 | other keyword argument) is passed through to the incremental encoder. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 252 | .. function:: iterdecode(iterator, encoding, errors='strict', **kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 253 | |
| 254 | Uses an incremental decoder to iteratively decode the input provided by |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 255 | *iterator*. This function is a :term:`generator`. *errors* (as well as any |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 256 | other keyword argument) is passed through to the incremental decoder. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 257 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 258 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | The module also provides the following constants which are useful for reading |
| 260 | and writing to platform dependent files: |
| 261 | |
| 262 | |
| 263 | .. data:: BOM |
| 264 | BOM_BE |
| 265 | BOM_LE |
| 266 | BOM_UTF8 |
| 267 | BOM_UTF16 |
| 268 | BOM_UTF16_BE |
| 269 | BOM_UTF16_LE |
| 270 | BOM_UTF32 |
| 271 | BOM_UTF32_BE |
| 272 | BOM_UTF32_LE |
| 273 | |
| 274 | These constants define various encodings of the Unicode byte order mark (BOM) |
| 275 | used in UTF-16 and UTF-32 data streams to indicate the byte order used in the |
| 276 | stream or file and in UTF-8 as a Unicode signature. :const:`BOM_UTF16` is either |
| 277 | :const:`BOM_UTF16_BE` or :const:`BOM_UTF16_LE` depending on the platform's |
| 278 | native byte order, :const:`BOM` is an alias for :const:`BOM_UTF16`, |
| 279 | :const:`BOM_LE` for :const:`BOM_UTF16_LE` and :const:`BOM_BE` for |
| 280 | :const:`BOM_UTF16_BE`. The others represent the BOM in UTF-8 and UTF-32 |
| 281 | encodings. |
| 282 | |
| 283 | |
| 284 | .. _codec-base-classes: |
| 285 | |
| 286 | Codec Base Classes |
| 287 | ------------------ |
| 288 | |
| 289 | The :mod:`codecs` module defines a set of base classes which define the |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 290 | interface and can also be used to easily write your own codecs for use in |
| 291 | Python. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | |
| 293 | Each codec has to define four interfaces to make it usable as codec in Python: |
| 294 | stateless encoder, stateless decoder, stream reader and stream writer. The |
| 295 | stream reader and writers typically reuse the stateless encoder/decoder to |
| 296 | implement the file protocols. |
| 297 | |
| 298 | The :class:`Codec` class defines the interface for stateless encoders/decoders. |
| 299 | |
| 300 | To simplify and standardize error handling, the :meth:`encode` and |
| 301 | :meth:`decode` methods may implement different error handling schemes by |
| 302 | providing the *errors* string argument. The following string values are defined |
| 303 | and implemented by all standard Python codecs: |
| 304 | |
| 305 | +-------------------------+-----------------------------------------------+ |
| 306 | | Value | Meaning | |
| 307 | +=========================+===============================================+ |
| 308 | | ``'strict'`` | Raise :exc:`UnicodeError` (or a subclass); | |
| 309 | | | this is the default. | |
| 310 | +-------------------------+-----------------------------------------------+ |
| 311 | | ``'ignore'`` | Ignore the character and continue with the | |
| 312 | | | next. | |
| 313 | +-------------------------+-----------------------------------------------+ |
| 314 | | ``'replace'`` | Replace with a suitable replacement | |
| 315 | | | character; Python will use the official | |
| 316 | | | U+FFFD REPLACEMENT CHARACTER for the built-in | |
| 317 | | | Unicode codecs on decoding and '?' on | |
| 318 | | | encoding. | |
| 319 | +-------------------------+-----------------------------------------------+ |
| 320 | | ``'xmlcharrefreplace'`` | Replace with the appropriate XML character | |
| 321 | | | reference (only for encoding). | |
| 322 | +-------------------------+-----------------------------------------------+ |
| 323 | | ``'backslashreplace'`` | Replace with backslashed escape sequences | |
| 324 | | | (only for encoding). | |
| 325 | +-------------------------+-----------------------------------------------+ |
Martin v. Löwis | 3d2eca0 | 2009-06-29 06:35:26 +0000 | [diff] [blame] | 326 | | ``'surrogateescape'`` | Replace byte with surrogate U+DCxx, as defined| |
| 327 | | | in :pep:`383`. | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 328 | +-------------------------+-----------------------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 329 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 330 | In addition, the following error handlers are specific to a single codec: |
| 331 | |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 332 | +-------------------+---------+-------------------------------------------+ |
| 333 | | Value | Codec | Meaning | |
| 334 | +===================+=========+===========================================+ |
| 335 | |``'surrogatepass'``| utf-8 | Allow encoding and decoding of surrogate | |
| 336 | | | | codes in UTF-8. | |
| 337 | +-------------------+---------+-------------------------------------------+ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 338 | |
| 339 | .. versionadded:: 3.1 |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 340 | The ``'surrogateescape'`` and ``'surrogatepass'`` error handlers. |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 341 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 342 | The set of allowed values can be extended via :meth:`register_error`. |
| 343 | |
| 344 | |
| 345 | .. _codec-objects: |
| 346 | |
| 347 | Codec Objects |
| 348 | ^^^^^^^^^^^^^ |
| 349 | |
| 350 | The :class:`Codec` class defines these methods which also define the function |
| 351 | interfaces of the stateless encoder and decoder: |
| 352 | |
| 353 | |
| 354 | .. method:: Codec.encode(input[, errors]) |
| 355 | |
| 356 | Encodes the object *input* and returns a tuple (output object, length consumed). |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 357 | Encoding converts a string object to a bytes object using a particular |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 358 | character set encoding (e.g., ``cp1252`` or ``iso-8859-1``). |
| 359 | |
| 360 | *errors* defines the error handling to apply. It defaults to ``'strict'`` |
| 361 | handling. |
| 362 | |
| 363 | The method may not store state in the :class:`Codec` instance. Use |
| 364 | :class:`StreamCodec` for codecs which have to keep state in order to make |
| 365 | encoding/decoding efficient. |
| 366 | |
| 367 | The encoder must be able to handle zero length input and return an empty object |
| 368 | of the output object type in this situation. |
| 369 | |
| 370 | |
| 371 | .. method:: Codec.decode(input[, errors]) |
| 372 | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 373 | Decodes the object *input* and returns a tuple (output object, length |
| 374 | consumed). Decoding converts a bytes object encoded using a particular |
| 375 | character set encoding to a string object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 376 | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 377 | *input* must be a bytes object or one which provides the read-only character |
| 378 | buffer interface -- for example, buffer objects and memory mapped files. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 379 | |
| 380 | *errors* defines the error handling to apply. It defaults to ``'strict'`` |
| 381 | handling. |
| 382 | |
| 383 | The method may not store state in the :class:`Codec` instance. Use |
| 384 | :class:`StreamCodec` for codecs which have to keep state in order to make |
| 385 | encoding/decoding efficient. |
| 386 | |
| 387 | The decoder must be able to handle zero length input and return an empty object |
| 388 | of the output object type in this situation. |
| 389 | |
| 390 | The :class:`IncrementalEncoder` and :class:`IncrementalDecoder` classes provide |
| 391 | the basic interface for incremental encoding and decoding. Encoding/decoding the |
| 392 | input isn't done with one call to the stateless encoder/decoder function, but |
| 393 | with multiple calls to the :meth:`encode`/:meth:`decode` method of the |
| 394 | incremental encoder/decoder. The incremental encoder/decoder keeps track of the |
| 395 | encoding/decoding process during method calls. |
| 396 | |
| 397 | The joined output of calls to the :meth:`encode`/:meth:`decode` method is the |
| 398 | same as if all the single inputs were joined into one, and this input was |
| 399 | encoded/decoded with the stateless encoder/decoder. |
| 400 | |
| 401 | |
| 402 | .. _incremental-encoder-objects: |
| 403 | |
| 404 | IncrementalEncoder Objects |
| 405 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 406 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 407 | The :class:`IncrementalEncoder` class is used for encoding an input in multiple |
| 408 | steps. It defines the following methods which every incremental encoder must |
| 409 | define in order to be compatible with the Python codec registry. |
| 410 | |
| 411 | |
| 412 | .. class:: IncrementalEncoder([errors]) |
| 413 | |
| 414 | Constructor for an :class:`IncrementalEncoder` instance. |
| 415 | |
| 416 | All incremental encoders must provide this constructor interface. They are free |
| 417 | to add additional keyword arguments, but only the ones defined here are used by |
| 418 | the Python codec registry. |
| 419 | |
| 420 | The :class:`IncrementalEncoder` may implement different error handling schemes |
| 421 | by providing the *errors* keyword argument. These parameters are predefined: |
| 422 | |
| 423 | * ``'strict'`` Raise :exc:`ValueError` (or a subclass); this is the default. |
| 424 | |
| 425 | * ``'ignore'`` Ignore the character and continue with the next. |
| 426 | |
| 427 | * ``'replace'`` Replace with a suitable replacement character |
| 428 | |
| 429 | * ``'xmlcharrefreplace'`` Replace with the appropriate XML character reference |
| 430 | |
| 431 | * ``'backslashreplace'`` Replace with backslashed escape sequences. |
| 432 | |
| 433 | The *errors* argument will be assigned to an attribute of the same name. |
| 434 | Assigning to this attribute makes it possible to switch between different error |
| 435 | handling strategies during the lifetime of the :class:`IncrementalEncoder` |
| 436 | object. |
| 437 | |
| 438 | The set of allowed values for the *errors* argument can be extended with |
| 439 | :func:`register_error`. |
| 440 | |
| 441 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 442 | .. method:: encode(object[, final]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 443 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 444 | Encodes *object* (taking the current state of the encoder into account) |
| 445 | and returns the resulting encoded object. If this is the last call to |
| 446 | :meth:`encode` *final* must be true (the default is false). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 447 | |
| 448 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 449 | .. method:: reset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 450 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 451 | Reset the encoder to the initial state. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 452 | |
| 453 | |
| 454 | .. method:: IncrementalEncoder.getstate() |
| 455 | |
| 456 | Return the current state of the encoder which must be an integer. The |
| 457 | implementation should make sure that ``0`` is the most common state. (States |
| 458 | that are more complicated than integers can be converted into an integer by |
| 459 | marshaling/pickling the state and encoding the bytes of the resulting string |
| 460 | into an integer). |
| 461 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 462 | |
| 463 | .. method:: IncrementalEncoder.setstate(state) |
| 464 | |
| 465 | Set the state of the encoder to *state*. *state* must be an encoder state |
| 466 | returned by :meth:`getstate`. |
| 467 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 468 | |
| 469 | .. _incremental-decoder-objects: |
| 470 | |
| 471 | IncrementalDecoder Objects |
| 472 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 473 | |
| 474 | The :class:`IncrementalDecoder` class is used for decoding an input in multiple |
| 475 | steps. It defines the following methods which every incremental decoder must |
| 476 | define in order to be compatible with the Python codec registry. |
| 477 | |
| 478 | |
| 479 | .. class:: IncrementalDecoder([errors]) |
| 480 | |
| 481 | Constructor for an :class:`IncrementalDecoder` instance. |
| 482 | |
| 483 | All incremental decoders must provide this constructor interface. They are free |
| 484 | to add additional keyword arguments, but only the ones defined here are used by |
| 485 | the Python codec registry. |
| 486 | |
| 487 | The :class:`IncrementalDecoder` may implement different error handling schemes |
| 488 | by providing the *errors* keyword argument. These parameters are predefined: |
| 489 | |
| 490 | * ``'strict'`` Raise :exc:`ValueError` (or a subclass); this is the default. |
| 491 | |
| 492 | * ``'ignore'`` Ignore the character and continue with the next. |
| 493 | |
| 494 | * ``'replace'`` Replace with a suitable replacement character. |
| 495 | |
| 496 | The *errors* argument will be assigned to an attribute of the same name. |
| 497 | Assigning to this attribute makes it possible to switch between different error |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 498 | handling strategies during the lifetime of the :class:`IncrementalDecoder` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 499 | object. |
| 500 | |
| 501 | The set of allowed values for the *errors* argument can be extended with |
| 502 | :func:`register_error`. |
| 503 | |
| 504 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 505 | .. method:: decode(object[, final]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 506 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 507 | Decodes *object* (taking the current state of the decoder into account) |
| 508 | and returns the resulting decoded object. If this is the last call to |
| 509 | :meth:`decode` *final* must be true (the default is false). If *final* is |
| 510 | true the decoder must decode the input completely and must flush all |
| 511 | buffers. If this isn't possible (e.g. because of incomplete byte sequences |
| 512 | at the end of the input) it must initiate error handling just like in the |
| 513 | stateless case (which might raise an exception). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 514 | |
| 515 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 516 | .. method:: reset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 517 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 518 | Reset the decoder to the initial state. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 519 | |
| 520 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 521 | .. method:: getstate() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 522 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 523 | Return the current state of the decoder. This must be a tuple with two |
| 524 | items, the first must be the buffer containing the still undecoded |
| 525 | input. The second must be an integer and can be additional state |
| 526 | info. (The implementation should make sure that ``0`` is the most common |
| 527 | additional state info.) If this additional state info is ``0`` it must be |
| 528 | possible to set the decoder to the state which has no input buffered and |
| 529 | ``0`` as the additional state info, so that feeding the previously |
| 530 | buffered input to the decoder returns it to the previous state without |
| 531 | producing any output. (Additional state info that is more complicated than |
| 532 | integers can be converted into an integer by marshaling/pickling the info |
| 533 | and encoding the bytes of the resulting string into an integer.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 534 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 535 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 536 | .. method:: setstate(state) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 537 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 538 | Set the state of the encoder to *state*. *state* must be a decoder state |
| 539 | returned by :meth:`getstate`. |
| 540 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 541 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 542 | The :class:`StreamWriter` and :class:`StreamReader` classes provide generic |
| 543 | working interfaces which can be used to implement new encoding submodules very |
| 544 | easily. See :mod:`encodings.utf_8` for an example of how this is done. |
| 545 | |
| 546 | |
| 547 | .. _stream-writer-objects: |
| 548 | |
| 549 | StreamWriter Objects |
| 550 | ^^^^^^^^^^^^^^^^^^^^ |
| 551 | |
| 552 | The :class:`StreamWriter` class is a subclass of :class:`Codec` and defines the |
| 553 | following methods which every stream writer must define in order to be |
| 554 | compatible with the Python codec registry. |
| 555 | |
| 556 | |
| 557 | .. class:: StreamWriter(stream[, errors]) |
| 558 | |
| 559 | Constructor for a :class:`StreamWriter` instance. |
| 560 | |
| 561 | All stream writers must provide this constructor interface. They are free to add |
| 562 | additional keyword arguments, but only the ones defined here are used by the |
| 563 | Python codec registry. |
| 564 | |
| 565 | *stream* must be a file-like object open for writing binary data. |
| 566 | |
| 567 | The :class:`StreamWriter` may implement different error handling schemes by |
| 568 | providing the *errors* keyword argument. These parameters are predefined: |
| 569 | |
| 570 | * ``'strict'`` Raise :exc:`ValueError` (or a subclass); this is the default. |
| 571 | |
| 572 | * ``'ignore'`` Ignore the character and continue with the next. |
| 573 | |
| 574 | * ``'replace'`` Replace with a suitable replacement character |
| 575 | |
| 576 | * ``'xmlcharrefreplace'`` Replace with the appropriate XML character reference |
| 577 | |
| 578 | * ``'backslashreplace'`` Replace with backslashed escape sequences. |
| 579 | |
| 580 | The *errors* argument will be assigned to an attribute of the same name. |
| 581 | Assigning to this attribute makes it possible to switch between different error |
| 582 | handling strategies during the lifetime of the :class:`StreamWriter` object. |
| 583 | |
| 584 | The set of allowed values for the *errors* argument can be extended with |
| 585 | :func:`register_error`. |
| 586 | |
| 587 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 588 | .. method:: write(object) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 589 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 590 | Writes the object's contents encoded to the stream. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 591 | |
| 592 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 593 | .. method:: writelines(list) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 594 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 595 | Writes the concatenated list of strings to the stream (possibly by reusing |
| 596 | the :meth:`write` method). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 597 | |
| 598 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 599 | .. method:: reset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 600 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 601 | Flushes and resets the codec buffers used for keeping state. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 602 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 603 | Calling this method should ensure that the data on the output is put into |
| 604 | a clean state that allows appending of new fresh data without having to |
| 605 | rescan the whole stream to recover state. |
| 606 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 607 | |
| 608 | In addition to the above methods, the :class:`StreamWriter` must also inherit |
| 609 | all other methods and attributes from the underlying stream. |
| 610 | |
| 611 | |
| 612 | .. _stream-reader-objects: |
| 613 | |
| 614 | StreamReader Objects |
| 615 | ^^^^^^^^^^^^^^^^^^^^ |
| 616 | |
| 617 | The :class:`StreamReader` class is a subclass of :class:`Codec` and defines the |
| 618 | following methods which every stream reader must define in order to be |
| 619 | compatible with the Python codec registry. |
| 620 | |
| 621 | |
| 622 | .. class:: StreamReader(stream[, errors]) |
| 623 | |
| 624 | Constructor for a :class:`StreamReader` instance. |
| 625 | |
| 626 | All stream readers must provide this constructor interface. They are free to add |
| 627 | additional keyword arguments, but only the ones defined here are used by the |
| 628 | Python codec registry. |
| 629 | |
| 630 | *stream* must be a file-like object open for reading (binary) data. |
| 631 | |
| 632 | The :class:`StreamReader` may implement different error handling schemes by |
| 633 | providing the *errors* keyword argument. These parameters are defined: |
| 634 | |
| 635 | * ``'strict'`` Raise :exc:`ValueError` (or a subclass); this is the default. |
| 636 | |
| 637 | * ``'ignore'`` Ignore the character and continue with the next. |
| 638 | |
| 639 | * ``'replace'`` Replace with a suitable replacement character. |
| 640 | |
| 641 | The *errors* argument will be assigned to an attribute of the same name. |
| 642 | Assigning to this attribute makes it possible to switch between different error |
| 643 | handling strategies during the lifetime of the :class:`StreamReader` object. |
| 644 | |
| 645 | The set of allowed values for the *errors* argument can be extended with |
| 646 | :func:`register_error`. |
| 647 | |
| 648 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 649 | .. method:: read([size[, chars, [firstline]]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 650 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 651 | Decodes data from the stream and returns the resulting object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 652 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 653 | *chars* indicates the number of characters to read from the |
| 654 | stream. :func:`read` will never return more than *chars* characters, but |
| 655 | it might return less, if there are not enough characters available. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 656 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 657 | *size* indicates the approximate maximum number of bytes to read from the |
| 658 | stream for decoding purposes. The decoder can modify this setting as |
| 659 | appropriate. The default value -1 indicates to read and decode as much as |
| 660 | possible. *size* is intended to prevent having to decode huge files in |
| 661 | one step. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 662 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 663 | *firstline* indicates that it would be sufficient to only return the first |
| 664 | line, if there are decoding errors on later lines. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 665 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 666 | The method should use a greedy read strategy meaning that it should read |
| 667 | as much data as is allowed within the definition of the encoding and the |
| 668 | given size, e.g. if optional encoding endings or state markers are |
| 669 | available on the stream, these should be read too. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 670 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 671 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 672 | .. method:: readline([size[, keepends]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 673 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 674 | Read one line from the input stream and return the decoded data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 675 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 676 | *size*, if given, is passed as size argument to the stream's |
| 677 | :meth:`readline` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 678 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 679 | If *keepends* is false line-endings will be stripped from the lines |
| 680 | returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 681 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 682 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 683 | .. method:: readlines([sizehint[, keepends]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 684 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 685 | Read all lines available on the input stream and return them as a list of |
| 686 | lines. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 687 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 688 | Line-endings are implemented using the codec's decoder method and are |
| 689 | included in the list entries if *keepends* is true. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 690 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 691 | *sizehint*, if given, is passed as the *size* argument to the stream's |
| 692 | :meth:`read` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 693 | |
| 694 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 695 | .. method:: reset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 696 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 697 | Resets the codec buffers used for keeping state. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 698 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 699 | Note that no stream repositioning should take place. This method is |
| 700 | primarily intended to be able to recover from decoding errors. |
| 701 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 702 | |
| 703 | In addition to the above methods, the :class:`StreamReader` must also inherit |
| 704 | all other methods and attributes from the underlying stream. |
| 705 | |
| 706 | The next two base classes are included for convenience. They are not needed by |
| 707 | the codec registry, but may provide useful in practice. |
| 708 | |
| 709 | |
| 710 | .. _stream-reader-writer: |
| 711 | |
| 712 | StreamReaderWriter Objects |
| 713 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 714 | |
| 715 | The :class:`StreamReaderWriter` allows wrapping streams which work in both read |
| 716 | and write modes. |
| 717 | |
| 718 | The design is such that one can use the factory functions returned by the |
| 719 | :func:`lookup` function to construct the instance. |
| 720 | |
| 721 | |
| 722 | .. class:: StreamReaderWriter(stream, Reader, Writer, errors) |
| 723 | |
| 724 | Creates a :class:`StreamReaderWriter` instance. *stream* must be a file-like |
| 725 | object. *Reader* and *Writer* must be factory functions or classes providing the |
| 726 | :class:`StreamReader` and :class:`StreamWriter` interface resp. Error handling |
| 727 | is done in the same way as defined for the stream readers and writers. |
| 728 | |
| 729 | :class:`StreamReaderWriter` instances define the combined interfaces of |
| 730 | :class:`StreamReader` and :class:`StreamWriter` classes. They inherit all other |
| 731 | methods and attributes from the underlying stream. |
| 732 | |
| 733 | |
| 734 | .. _stream-recoder-objects: |
| 735 | |
| 736 | StreamRecoder Objects |
| 737 | ^^^^^^^^^^^^^^^^^^^^^ |
| 738 | |
| 739 | The :class:`StreamRecoder` provide a frontend - backend view of encoding data |
| 740 | which is sometimes useful when dealing with different encoding environments. |
| 741 | |
| 742 | The design is such that one can use the factory functions returned by the |
| 743 | :func:`lookup` function to construct the instance. |
| 744 | |
| 745 | |
| 746 | .. class:: StreamRecoder(stream, encode, decode, Reader, Writer, errors) |
| 747 | |
| 748 | Creates a :class:`StreamRecoder` instance which implements a two-way conversion: |
| 749 | *encode* and *decode* work on the frontend (the input to :meth:`read` and output |
| 750 | of :meth:`write`) while *Reader* and *Writer* work on the backend (reading and |
| 751 | writing to the stream). |
| 752 | |
| 753 | You can use these objects to do transparent direct recodings from e.g. Latin-1 |
| 754 | to UTF-8 and back. |
| 755 | |
| 756 | *stream* must be a file-like object. |
| 757 | |
| 758 | *encode*, *decode* must adhere to the :class:`Codec` interface. *Reader*, |
| 759 | *Writer* must be factory functions or classes providing objects of the |
| 760 | :class:`StreamReader` and :class:`StreamWriter` interface respectively. |
| 761 | |
| 762 | *encode* and *decode* are needed for the frontend translation, *Reader* and |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 763 | *Writer* for the backend translation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 764 | |
| 765 | Error handling is done in the same way as defined for the stream readers and |
| 766 | writers. |
| 767 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 768 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 769 | :class:`StreamRecoder` instances define the combined interfaces of |
| 770 | :class:`StreamReader` and :class:`StreamWriter` classes. They inherit all other |
| 771 | methods and attributes from the underlying stream. |
| 772 | |
| 773 | |
| 774 | .. _encodings-overview: |
| 775 | |
| 776 | Encodings and Unicode |
| 777 | --------------------- |
| 778 | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 779 | Strings are stored internally as sequences of codepoints (to be precise |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 780 | as :ctype:`Py_UNICODE` arrays). Depending on the way Python is compiled (either |
Georg Brandl | 52d168a | 2008-01-07 18:10:24 +0000 | [diff] [blame] | 781 | via :option:`--without-wide-unicode` or :option:`--with-wide-unicode`, with the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 782 | former being the default) :ctype:`Py_UNICODE` is either a 16-bit or 32-bit data |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 783 | type. Once a string object is used outside of CPU and memory, CPU endianness |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 784 | and how these arrays are stored as bytes become an issue. Transforming a |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 785 | string object into a sequence of bytes is called encoding and recreating the |
| 786 | string object from the sequence of bytes is known as decoding. There are many |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 787 | different methods for how this transformation can be done (these methods are |
| 788 | also called encodings). The simplest method is to map the codepoints 0-255 to |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 789 | the bytes ``0x0``-``0xff``. This means that a string object that contains |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 790 | codepoints above ``U+00FF`` can't be encoded with this method (which is called |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 791 | ``'latin-1'`` or ``'iso-8859-1'``). :func:`str.encode` will raise a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 792 | :exc:`UnicodeEncodeError` that looks like this: ``UnicodeEncodeError: 'latin-1' |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 793 | codec can't encode character '\u1234' in position 3: ordinal not in |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 794 | range(256)``. |
| 795 | |
| 796 | There's another group of encodings (the so called charmap encodings) that choose |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 797 | a different subset of all Unicode code points and how these codepoints are |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 798 | mapped to the bytes ``0x0``-``0xff``. To see how this is done simply open |
| 799 | e.g. :file:`encodings/cp1252.py` (which is an encoding that is used primarily on |
| 800 | Windows). There's a string constant with 256 characters that shows you which |
| 801 | character is mapped to which byte value. |
| 802 | |
| 803 | All of these encodings can only encode 256 of the 65536 (or 1114111) codepoints |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 804 | defined in Unicode. A simple and straightforward way that can store each Unicode |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 805 | code point, is to store each codepoint as two consecutive bytes. There are two |
| 806 | possibilities: Store the bytes in big endian or in little endian order. These |
| 807 | two encodings are called UTF-16-BE and UTF-16-LE respectively. Their |
| 808 | disadvantage is that if e.g. you use UTF-16-BE on a little endian machine you |
| 809 | will always have to swap bytes on encoding and decoding. UTF-16 avoids this |
| 810 | problem: Bytes will always be in natural endianness. When these bytes are read |
| 811 | by a CPU with a different endianness, then bytes have to be swapped though. To |
| 812 | be able to detect the endianness of a UTF-16 byte sequence, there's the so |
| 813 | called BOM (the "Byte Order Mark"). This is the Unicode character ``U+FEFF``. |
| 814 | This character will be prepended to every UTF-16 byte sequence. The byte swapped |
| 815 | version of this character (``0xFFFE``) is an illegal character that may not |
| 816 | appear in a Unicode text. So when the first character in an UTF-16 byte sequence |
| 817 | appears to be a ``U+FFFE`` the bytes have to be swapped on decoding. |
| 818 | Unfortunately upto Unicode 4.0 the character ``U+FEFF`` had a second purpose as |
| 819 | a ``ZERO WIDTH NO-BREAK SPACE``: A character that has no width and doesn't allow |
| 820 | a word to be split. It can e.g. be used to give hints to a ligature algorithm. |
| 821 | With Unicode 4.0 using ``U+FEFF`` as a ``ZERO WIDTH NO-BREAK SPACE`` has been |
| 822 | deprecated (with ``U+2060`` (``WORD JOINER``) assuming this role). Nevertheless |
| 823 | Unicode software still must be able to handle ``U+FEFF`` in both roles: As a BOM |
| 824 | it's a device to determine the storage layout of the encoded bytes, and vanishes |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 825 | once the byte sequence has been decoded into a string; as a ``ZERO WIDTH |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 826 | NO-BREAK SPACE`` it's a normal character that will be decoded like any other. |
| 827 | |
| 828 | There's another encoding that is able to encoding the full range of Unicode |
| 829 | characters: UTF-8. UTF-8 is an 8-bit encoding, which means there are no issues |
| 830 | with byte order in UTF-8. Each byte in a UTF-8 byte sequence consists of two |
| 831 | parts: Marker bits (the most significant bits) and payload bits. The marker bits |
| 832 | are a sequence of zero to six 1 bits followed by a 0 bit. Unicode characters are |
| 833 | encoded like this (with x being payload bits, which when concatenated give the |
| 834 | Unicode character): |
| 835 | |
| 836 | +-----------------------------------+----------------------------------------------+ |
| 837 | | Range | Encoding | |
| 838 | +===================================+==============================================+ |
| 839 | | ``U-00000000`` ... ``U-0000007F`` | 0xxxxxxx | |
| 840 | +-----------------------------------+----------------------------------------------+ |
| 841 | | ``U-00000080`` ... ``U-000007FF`` | 110xxxxx 10xxxxxx | |
| 842 | +-----------------------------------+----------------------------------------------+ |
| 843 | | ``U-00000800`` ... ``U-0000FFFF`` | 1110xxxx 10xxxxxx 10xxxxxx | |
| 844 | +-----------------------------------+----------------------------------------------+ |
| 845 | | ``U-00010000`` ... ``U-001FFFFF`` | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx | |
| 846 | +-----------------------------------+----------------------------------------------+ |
| 847 | | ``U-00200000`` ... ``U-03FFFFFF`` | 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx | |
| 848 | +-----------------------------------+----------------------------------------------+ |
| 849 | | ``U-04000000`` ... ``U-7FFFFFFF`` | 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx | |
| 850 | | | 10xxxxxx | |
| 851 | +-----------------------------------+----------------------------------------------+ |
| 852 | |
| 853 | The least significant bit of the Unicode character is the rightmost x bit. |
| 854 | |
| 855 | As UTF-8 is an 8-bit encoding no BOM is required and any ``U+FEFF`` character in |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 856 | the decoded string (even if it's the first character) is treated as a ``ZERO |
| 857 | WIDTH NO-BREAK SPACE``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 858 | |
| 859 | Without external information it's impossible to reliably determine which |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 860 | encoding was used for encoding a string. Each charmap encoding can |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 861 | decode any random byte sequence. However that's not possible with UTF-8, as |
| 862 | UTF-8 byte sequences have a structure that doesn't allow arbitrary byte |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 863 | sequences. To increase the reliability with which a UTF-8 encoding can be |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 864 | detected, Microsoft invented a variant of UTF-8 (that Python 2.5 calls |
| 865 | ``"utf-8-sig"``) for its Notepad program: Before any of the Unicode characters |
| 866 | is written to the file, a UTF-8 encoded BOM (which looks like this as a byte |
| 867 | sequence: ``0xef``, ``0xbb``, ``0xbf``) is written. As it's rather improbable |
| 868 | that any charmap encoded file starts with these byte values (which would e.g. |
| 869 | map to |
| 870 | |
| 871 | | LATIN SMALL LETTER I WITH DIAERESIS |
| 872 | | RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK |
| 873 | | INVERTED QUESTION MARK |
| 874 | |
| 875 | in iso-8859-1), this increases the probability that a utf-8-sig encoding can be |
| 876 | correctly guessed from the byte sequence. So here the BOM is not used to be able |
| 877 | to determine the byte order used for generating the byte sequence, but as a |
| 878 | signature that helps in guessing the encoding. On encoding the utf-8-sig codec |
| 879 | will write ``0xef``, ``0xbb``, ``0xbf`` as the first three bytes to the file. On |
| 880 | decoding utf-8-sig will skip those three bytes if they appear as the first three |
| 881 | bytes in the file. |
| 882 | |
| 883 | |
| 884 | .. _standard-encodings: |
| 885 | |
| 886 | Standard Encodings |
| 887 | ------------------ |
| 888 | |
| 889 | Python comes with a number of codecs built-in, either implemented as C functions |
| 890 | or with dictionaries as mapping tables. The following table lists the codecs by |
| 891 | name, together with a few common aliases, and the languages for which the |
| 892 | encoding is likely used. Neither the list of aliases nor the list of languages |
| 893 | is meant to be exhaustive. Notice that spelling alternatives that only differ in |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 894 | case or use a hyphen instead of an underscore are also valid aliases; therefore, |
| 895 | e.g. ``'utf-8'`` is a valid alias for the ``'utf_8'`` codec. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 896 | |
| 897 | Many of the character sets support the same languages. They vary in individual |
| 898 | characters (e.g. whether the EURO SIGN is supported or not), and in the |
| 899 | assignment of characters to code positions. For the European languages in |
| 900 | particular, the following variants typically exist: |
| 901 | |
| 902 | * an ISO 8859 codeset |
| 903 | |
| 904 | * a Microsoft Windows code page, which is typically derived from a 8859 codeset, |
| 905 | but replaces control characters with additional graphic characters |
| 906 | |
| 907 | * an IBM EBCDIC code page |
| 908 | |
| 909 | * an IBM PC code page, which is ASCII compatible |
| 910 | |
| 911 | +-----------------+--------------------------------+--------------------------------+ |
| 912 | | Codec | Aliases | Languages | |
| 913 | +=================+================================+================================+ |
| 914 | | ascii | 646, us-ascii | English | |
| 915 | +-----------------+--------------------------------+--------------------------------+ |
| 916 | | big5 | big5-tw, csbig5 | Traditional Chinese | |
| 917 | +-----------------+--------------------------------+--------------------------------+ |
| 918 | | big5hkscs | big5-hkscs, hkscs | Traditional Chinese | |
| 919 | +-----------------+--------------------------------+--------------------------------+ |
| 920 | | cp037 | IBM037, IBM039 | English | |
| 921 | +-----------------+--------------------------------+--------------------------------+ |
| 922 | | cp424 | EBCDIC-CP-HE, IBM424 | Hebrew | |
| 923 | +-----------------+--------------------------------+--------------------------------+ |
| 924 | | cp437 | 437, IBM437 | English | |
| 925 | +-----------------+--------------------------------+--------------------------------+ |
| 926 | | cp500 | EBCDIC-CP-BE, EBCDIC-CP-CH, | Western Europe | |
| 927 | | | IBM500 | | |
| 928 | +-----------------+--------------------------------+--------------------------------+ |
Amaury Forgeot d'Arc | ae6388d | 2009-07-15 19:21:18 +0000 | [diff] [blame] | 929 | | cp720 | | Arabic | |
| 930 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 931 | | cp737 | | Greek | |
| 932 | +-----------------+--------------------------------+--------------------------------+ |
| 933 | | cp775 | IBM775 | Baltic languages | |
| 934 | +-----------------+--------------------------------+--------------------------------+ |
| 935 | | cp850 | 850, IBM850 | Western Europe | |
| 936 | +-----------------+--------------------------------+--------------------------------+ |
| 937 | | cp852 | 852, IBM852 | Central and Eastern Europe | |
| 938 | +-----------------+--------------------------------+--------------------------------+ |
| 939 | | cp855 | 855, IBM855 | Bulgarian, Byelorussian, | |
| 940 | | | | Macedonian, Russian, Serbian | |
| 941 | +-----------------+--------------------------------+--------------------------------+ |
| 942 | | cp856 | | Hebrew | |
| 943 | +-----------------+--------------------------------+--------------------------------+ |
| 944 | | cp857 | 857, IBM857 | Turkish | |
| 945 | +-----------------+--------------------------------+--------------------------------+ |
| 946 | | cp860 | 860, IBM860 | Portuguese | |
| 947 | +-----------------+--------------------------------+--------------------------------+ |
| 948 | | cp861 | 861, CP-IS, IBM861 | Icelandic | |
| 949 | +-----------------+--------------------------------+--------------------------------+ |
| 950 | | cp862 | 862, IBM862 | Hebrew | |
| 951 | +-----------------+--------------------------------+--------------------------------+ |
| 952 | | cp863 | 863, IBM863 | Canadian | |
| 953 | +-----------------+--------------------------------+--------------------------------+ |
| 954 | | cp864 | IBM864 | Arabic | |
| 955 | +-----------------+--------------------------------+--------------------------------+ |
| 956 | | cp865 | 865, IBM865 | Danish, Norwegian | |
| 957 | +-----------------+--------------------------------+--------------------------------+ |
| 958 | | cp866 | 866, IBM866 | Russian | |
| 959 | +-----------------+--------------------------------+--------------------------------+ |
| 960 | | cp869 | 869, CP-GR, IBM869 | Greek | |
| 961 | +-----------------+--------------------------------+--------------------------------+ |
| 962 | | cp874 | | Thai | |
| 963 | +-----------------+--------------------------------+--------------------------------+ |
| 964 | | cp875 | | Greek | |
| 965 | +-----------------+--------------------------------+--------------------------------+ |
| 966 | | cp932 | 932, ms932, mskanji, ms-kanji | Japanese | |
| 967 | +-----------------+--------------------------------+--------------------------------+ |
| 968 | | cp949 | 949, ms949, uhc | Korean | |
| 969 | +-----------------+--------------------------------+--------------------------------+ |
| 970 | | cp950 | 950, ms950 | Traditional Chinese | |
| 971 | +-----------------+--------------------------------+--------------------------------+ |
| 972 | | cp1006 | | Urdu | |
| 973 | +-----------------+--------------------------------+--------------------------------+ |
| 974 | | cp1026 | ibm1026 | Turkish | |
| 975 | +-----------------+--------------------------------+--------------------------------+ |
| 976 | | cp1140 | ibm1140 | Western Europe | |
| 977 | +-----------------+--------------------------------+--------------------------------+ |
| 978 | | cp1250 | windows-1250 | Central and Eastern Europe | |
| 979 | +-----------------+--------------------------------+--------------------------------+ |
| 980 | | cp1251 | windows-1251 | Bulgarian, Byelorussian, | |
| 981 | | | | Macedonian, Russian, Serbian | |
| 982 | +-----------------+--------------------------------+--------------------------------+ |
| 983 | | cp1252 | windows-1252 | Western Europe | |
| 984 | +-----------------+--------------------------------+--------------------------------+ |
| 985 | | cp1253 | windows-1253 | Greek | |
| 986 | +-----------------+--------------------------------+--------------------------------+ |
| 987 | | cp1254 | windows-1254 | Turkish | |
| 988 | +-----------------+--------------------------------+--------------------------------+ |
| 989 | | cp1255 | windows-1255 | Hebrew | |
| 990 | +-----------------+--------------------------------+--------------------------------+ |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame^] | 991 | | cp1256 | windows-1256 | Arabic | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 992 | +-----------------+--------------------------------+--------------------------------+ |
| 993 | | cp1257 | windows-1257 | Baltic languages | |
| 994 | +-----------------+--------------------------------+--------------------------------+ |
| 995 | | cp1258 | windows-1258 | Vietnamese | |
| 996 | +-----------------+--------------------------------+--------------------------------+ |
| 997 | | euc_jp | eucjp, ujis, u-jis | Japanese | |
| 998 | +-----------------+--------------------------------+--------------------------------+ |
| 999 | | euc_jis_2004 | jisx0213, eucjis2004 | Japanese | |
| 1000 | +-----------------+--------------------------------+--------------------------------+ |
| 1001 | | euc_jisx0213 | eucjisx0213 | Japanese | |
| 1002 | +-----------------+--------------------------------+--------------------------------+ |
| 1003 | | euc_kr | euckr, korean, ksc5601, | Korean | |
| 1004 | | | ks_c-5601, ks_c-5601-1987, | | |
| 1005 | | | ksx1001, ks_x-1001 | | |
| 1006 | +-----------------+--------------------------------+--------------------------------+ |
| 1007 | | gb2312 | chinese, csiso58gb231280, euc- | Simplified Chinese | |
| 1008 | | | cn, euccn, eucgb2312-cn, | | |
| 1009 | | | gb2312-1980, gb2312-80, iso- | | |
| 1010 | | | ir-58 | | |
| 1011 | +-----------------+--------------------------------+--------------------------------+ |
| 1012 | | gbk | 936, cp936, ms936 | Unified Chinese | |
| 1013 | +-----------------+--------------------------------+--------------------------------+ |
| 1014 | | gb18030 | gb18030-2000 | Unified Chinese | |
| 1015 | +-----------------+--------------------------------+--------------------------------+ |
| 1016 | | hz | hzgb, hz-gb, hz-gb-2312 | Simplified Chinese | |
| 1017 | +-----------------+--------------------------------+--------------------------------+ |
| 1018 | | iso2022_jp | csiso2022jp, iso2022jp, | Japanese | |
| 1019 | | | iso-2022-jp | | |
| 1020 | +-----------------+--------------------------------+--------------------------------+ |
| 1021 | | iso2022_jp_1 | iso2022jp-1, iso-2022-jp-1 | Japanese | |
| 1022 | +-----------------+--------------------------------+--------------------------------+ |
| 1023 | | iso2022_jp_2 | iso2022jp-2, iso-2022-jp-2 | Japanese, Korean, Simplified | |
| 1024 | | | | Chinese, Western Europe, Greek | |
| 1025 | +-----------------+--------------------------------+--------------------------------+ |
| 1026 | | iso2022_jp_2004 | iso2022jp-2004, | Japanese | |
| 1027 | | | iso-2022-jp-2004 | | |
| 1028 | +-----------------+--------------------------------+--------------------------------+ |
| 1029 | | iso2022_jp_3 | iso2022jp-3, iso-2022-jp-3 | Japanese | |
| 1030 | +-----------------+--------------------------------+--------------------------------+ |
| 1031 | | iso2022_jp_ext | iso2022jp-ext, iso-2022-jp-ext | Japanese | |
| 1032 | +-----------------+--------------------------------+--------------------------------+ |
| 1033 | | iso2022_kr | csiso2022kr, iso2022kr, | Korean | |
| 1034 | | | iso-2022-kr | | |
| 1035 | +-----------------+--------------------------------+--------------------------------+ |
| 1036 | | latin_1 | iso-8859-1, iso8859-1, 8859, | West Europe | |
| 1037 | | | cp819, latin, latin1, L1 | | |
| 1038 | +-----------------+--------------------------------+--------------------------------+ |
| 1039 | | iso8859_2 | iso-8859-2, latin2, L2 | Central and Eastern Europe | |
| 1040 | +-----------------+--------------------------------+--------------------------------+ |
| 1041 | | iso8859_3 | iso-8859-3, latin3, L3 | Esperanto, Maltese | |
| 1042 | +-----------------+--------------------------------+--------------------------------+ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 1043 | | iso8859_4 | iso-8859-4, latin4, L4 | Baltic languages | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1044 | +-----------------+--------------------------------+--------------------------------+ |
| 1045 | | iso8859_5 | iso-8859-5, cyrillic | Bulgarian, Byelorussian, | |
| 1046 | | | | Macedonian, Russian, Serbian | |
| 1047 | +-----------------+--------------------------------+--------------------------------+ |
| 1048 | | iso8859_6 | iso-8859-6, arabic | Arabic | |
| 1049 | +-----------------+--------------------------------+--------------------------------+ |
| 1050 | | iso8859_7 | iso-8859-7, greek, greek8 | Greek | |
| 1051 | +-----------------+--------------------------------+--------------------------------+ |
| 1052 | | iso8859_8 | iso-8859-8, hebrew | Hebrew | |
| 1053 | +-----------------+--------------------------------+--------------------------------+ |
| 1054 | | iso8859_9 | iso-8859-9, latin5, L5 | Turkish | |
| 1055 | +-----------------+--------------------------------+--------------------------------+ |
| 1056 | | iso8859_10 | iso-8859-10, latin6, L6 | Nordic languages | |
| 1057 | +-----------------+--------------------------------+--------------------------------+ |
| 1058 | | iso8859_13 | iso-8859-13 | Baltic languages | |
| 1059 | +-----------------+--------------------------------+--------------------------------+ |
| 1060 | | iso8859_14 | iso-8859-14, latin8, L8 | Celtic languages | |
| 1061 | +-----------------+--------------------------------+--------------------------------+ |
| 1062 | | iso8859_15 | iso-8859-15 | Western Europe | |
| 1063 | +-----------------+--------------------------------+--------------------------------+ |
| 1064 | | johab | cp1361, ms1361 | Korean | |
| 1065 | +-----------------+--------------------------------+--------------------------------+ |
| 1066 | | koi8_r | | Russian | |
| 1067 | +-----------------+--------------------------------+--------------------------------+ |
| 1068 | | koi8_u | | Ukrainian | |
| 1069 | +-----------------+--------------------------------+--------------------------------+ |
| 1070 | | mac_cyrillic | maccyrillic | Bulgarian, Byelorussian, | |
| 1071 | | | | Macedonian, Russian, Serbian | |
| 1072 | +-----------------+--------------------------------+--------------------------------+ |
| 1073 | | mac_greek | macgreek | Greek | |
| 1074 | +-----------------+--------------------------------+--------------------------------+ |
| 1075 | | mac_iceland | maciceland | Icelandic | |
| 1076 | +-----------------+--------------------------------+--------------------------------+ |
| 1077 | | mac_latin2 | maclatin2, maccentraleurope | Central and Eastern Europe | |
| 1078 | +-----------------+--------------------------------+--------------------------------+ |
| 1079 | | mac_roman | macroman | Western Europe | |
| 1080 | +-----------------+--------------------------------+--------------------------------+ |
| 1081 | | mac_turkish | macturkish | Turkish | |
| 1082 | +-----------------+--------------------------------+--------------------------------+ |
| 1083 | | ptcp154 | csptcp154, pt154, cp154, | Kazakh | |
| 1084 | | | cyrillic-asian | | |
| 1085 | +-----------------+--------------------------------+--------------------------------+ |
| 1086 | | shift_jis | csshiftjis, shiftjis, sjis, | Japanese | |
| 1087 | | | s_jis | | |
| 1088 | +-----------------+--------------------------------+--------------------------------+ |
| 1089 | | shift_jis_2004 | shiftjis2004, sjis_2004, | Japanese | |
| 1090 | | | sjis2004 | | |
| 1091 | +-----------------+--------------------------------+--------------------------------+ |
| 1092 | | shift_jisx0213 | shiftjisx0213, sjisx0213, | Japanese | |
| 1093 | | | s_jisx0213 | | |
| 1094 | +-----------------+--------------------------------+--------------------------------+ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 1095 | | utf_32 | U32, utf32 | all languages | |
| 1096 | +-----------------+--------------------------------+--------------------------------+ |
| 1097 | | utf_32_be | UTF-32BE | all languages | |
| 1098 | +-----------------+--------------------------------+--------------------------------+ |
| 1099 | | utf_32_le | UTF-32LE | all languages | |
| 1100 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1101 | | utf_16 | U16, utf16 | all languages | |
| 1102 | +-----------------+--------------------------------+--------------------------------+ |
| 1103 | | utf_16_be | UTF-16BE | all languages (BMP only) | |
| 1104 | +-----------------+--------------------------------+--------------------------------+ |
| 1105 | | utf_16_le | UTF-16LE | all languages (BMP only) | |
| 1106 | +-----------------+--------------------------------+--------------------------------+ |
| 1107 | | utf_7 | U7, unicode-1-1-utf-7 | all languages | |
| 1108 | +-----------------+--------------------------------+--------------------------------+ |
| 1109 | | utf_8 | U8, UTF, utf8 | all languages | |
| 1110 | +-----------------+--------------------------------+--------------------------------+ |
| 1111 | | utf_8_sig | | all languages | |
| 1112 | +-----------------+--------------------------------+--------------------------------+ |
| 1113 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1114 | .. XXX fix here, should be in above table |
| 1115 | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 1116 | +--------------------+---------+---------------------------+ |
| 1117 | | Codec | Aliases | Purpose | |
| 1118 | +====================+=========+===========================+ |
| 1119 | | idna | | Implements :rfc:`3490`, | |
| 1120 | | | | see also | |
| 1121 | | | | :mod:`encodings.idna` | |
| 1122 | +--------------------+---------+---------------------------+ |
| 1123 | | mbcs | dbcs | Windows only: Encode | |
| 1124 | | | | operand according to the | |
| 1125 | | | | ANSI codepage (CP_ACP) | |
| 1126 | +--------------------+---------+---------------------------+ |
| 1127 | | palmos | | Encoding of PalmOS 3.5 | |
| 1128 | +--------------------+---------+---------------------------+ |
| 1129 | | punycode | | Implements :rfc:`3492` | |
| 1130 | +--------------------+---------+---------------------------+ |
| 1131 | | raw_unicode_escape | | Produce a string that is | |
| 1132 | | | | suitable as raw Unicode | |
| 1133 | | | | literal in Python source | |
| 1134 | | | | code | |
| 1135 | +--------------------+---------+---------------------------+ |
| 1136 | | undefined | | Raise an exception for | |
| 1137 | | | | all conversions. Can be | |
| 1138 | | | | used as the system | |
| 1139 | | | | encoding if no automatic | |
| 1140 | | | | coercion between byte and | |
| 1141 | | | | Unicode strings is | |
| 1142 | | | | desired. | |
| 1143 | +--------------------+---------+---------------------------+ |
| 1144 | | unicode_escape | | Produce a string that is | |
| 1145 | | | | suitable as Unicode | |
| 1146 | | | | literal in Python source | |
| 1147 | | | | code | |
| 1148 | +--------------------+---------+---------------------------+ |
| 1149 | | unicode_internal | | Return the internal | |
| 1150 | | | | representation of the | |
| 1151 | | | | operand | |
| 1152 | +--------------------+---------+---------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1153 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1154 | |
| 1155 | :mod:`encodings.idna` --- Internationalized Domain Names in Applications |
| 1156 | ------------------------------------------------------------------------ |
| 1157 | |
| 1158 | .. module:: encodings.idna |
| 1159 | :synopsis: Internationalized Domain Names implementation |
| 1160 | .. moduleauthor:: Martin v. Löwis |
| 1161 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1162 | This module implements :rfc:`3490` (Internationalized Domain Names in |
| 1163 | Applications) and :rfc:`3492` (Nameprep: A Stringprep Profile for |
| 1164 | Internationalized Domain Names (IDN)). It builds upon the ``punycode`` encoding |
| 1165 | and :mod:`stringprep`. |
| 1166 | |
| 1167 | These RFCs together define a protocol to support non-ASCII characters in domain |
| 1168 | names. A domain name containing non-ASCII characters (such as |
| 1169 | ``www.Alliancefrançaise.nu``) is converted into an ASCII-compatible encoding |
| 1170 | (ACE, such as ``www.xn--alliancefranaise-npb.nu``). The ACE form of the domain |
| 1171 | name is then used in all places where arbitrary characters are not allowed by |
| 1172 | the protocol, such as DNS queries, HTTP :mailheader:`Host` fields, and so |
| 1173 | on. This conversion is carried out in the application; if possible invisible to |
| 1174 | the user: The application should transparently convert Unicode domain labels to |
| 1175 | IDNA on the wire, and convert back ACE labels to Unicode before presenting them |
| 1176 | to the user. |
| 1177 | |
| 1178 | Python supports this conversion in several ways: The ``idna`` codec allows to |
| 1179 | convert between Unicode and the ACE. Furthermore, the :mod:`socket` module |
| 1180 | transparently converts Unicode host names to ACE, so that applications need not |
| 1181 | be concerned about converting host names themselves when they pass them to the |
| 1182 | socket module. On top of that, modules that have host names as function |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 1183 | parameters, such as :mod:`http.client` and :mod:`ftplib`, accept Unicode host |
| 1184 | names (:mod:`http.client` then also transparently sends an IDNA hostname in the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1185 | :mailheader:`Host` field if it sends that field at all). |
| 1186 | |
| 1187 | When receiving host names from the wire (such as in reverse name lookup), no |
| 1188 | automatic conversion to Unicode is performed: Applications wishing to present |
| 1189 | such host names to the user should decode them to Unicode. |
| 1190 | |
| 1191 | The module :mod:`encodings.idna` also implements the nameprep procedure, which |
| 1192 | performs certain normalizations on host names, to achieve case-insensitivity of |
| 1193 | international domain names, and to unify similar characters. The nameprep |
| 1194 | functions can be used directly if desired. |
| 1195 | |
| 1196 | |
| 1197 | .. function:: nameprep(label) |
| 1198 | |
| 1199 | Return the nameprepped version of *label*. The implementation currently assumes |
| 1200 | query strings, so ``AllowUnassigned`` is true. |
| 1201 | |
| 1202 | |
| 1203 | .. function:: ToASCII(label) |
| 1204 | |
| 1205 | Convert a label to ASCII, as specified in :rfc:`3490`. ``UseSTD3ASCIIRules`` is |
| 1206 | assumed to be false. |
| 1207 | |
| 1208 | |
| 1209 | .. function:: ToUnicode(label) |
| 1210 | |
| 1211 | Convert a label to Unicode, as specified in :rfc:`3490`. |
| 1212 | |
| 1213 | |
| 1214 | :mod:`encodings.utf_8_sig` --- UTF-8 codec with BOM signature |
| 1215 | ------------------------------------------------------------- |
| 1216 | |
| 1217 | .. module:: encodings.utf_8_sig |
| 1218 | :synopsis: UTF-8 codec with BOM signature |
| 1219 | .. moduleauthor:: Walter Dörwald |
| 1220 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1221 | This module implements a variant of the UTF-8 codec: On encoding a UTF-8 encoded |
| 1222 | BOM will be prepended to the UTF-8 encoded bytes. For the stateful encoder this |
| 1223 | is only done once (on the first write to the byte stream). For decoding an |
| 1224 | optional UTF-8 encoded BOM at the start of the data will be skipped. |
| 1225 | |