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. |
Antoine Pitrou | fbd4f80 | 2012-08-11 16:51:50 +0200 | [diff] [blame] | 6 | .. moduleauthor:: Marc-André Lemburg <mal@lemburg.com> |
| 7 | .. sectionauthor:: Marc-André Lemburg <mal@lemburg.com> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. sectionauthor:: Martin v. Löwis <martin@v.loewis.de> |
| 9 | |
Andrew Kuchling | 2e3743c | 2014-03-19 16:23:01 -0400 | [diff] [blame] | 10 | **Source code:** :source:`Lib/codecs.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
| 12 | .. index:: |
| 13 | single: Unicode |
| 14 | single: Codecs |
| 15 | pair: Codecs; encode |
| 16 | pair: Codecs; decode |
| 17 | single: streams |
| 18 | pair: stackable; streams |
| 19 | |
| 20 | This module defines base classes for standard Python codecs (encoders and |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 21 | decoders) and provides access to the internal Python codec registry, which |
| 22 | manages the codec and error handling lookup process. Most standard codecs |
| 23 | are :term:`text encodings <text encoding>`, which encode text to bytes, |
| 24 | but there are also codecs provided that encode text to text, and bytes to |
| 25 | bytes. Custom codecs may encode and decode between arbitrary types, but some |
| 26 | module features are restricted to use specifically with |
| 27 | :term:`text encodings <text encoding>`, or with codecs that encode to |
| 28 | :class:`bytes`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 30 | The module defines the following functions for encoding and decoding with |
| 31 | any codec: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
Nick Coghlan | 6cb2b5b | 2013-10-14 00:22:13 +1000 | [diff] [blame] | 33 | .. function:: encode(obj, encoding='utf-8', errors='strict') |
| 34 | |
| 35 | Encodes *obj* using the codec registered for *encoding*. |
| 36 | |
| 37 | *Errors* may be given to set the desired error handling scheme. The |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 38 | default error handler is ``'strict'`` meaning that encoding errors raise |
Nick Coghlan | 6cb2b5b | 2013-10-14 00:22:13 +1000 | [diff] [blame] | 39 | :exc:`ValueError` (or a more codec specific subclass, such as |
| 40 | :exc:`UnicodeEncodeError`). Refer to :ref:`codec-base-classes` for more |
| 41 | information on codec error handling. |
| 42 | |
| 43 | .. function:: decode(obj, encoding='utf-8', errors='strict') |
| 44 | |
| 45 | Decodes *obj* using the codec registered for *encoding*. |
| 46 | |
| 47 | *Errors* may be given to set the desired error handling scheme. The |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 48 | default error handler is ``'strict'`` meaning that decoding errors raise |
Nick Coghlan | 6cb2b5b | 2013-10-14 00:22:13 +1000 | [diff] [blame] | 49 | :exc:`ValueError` (or a more codec specific subclass, such as |
| 50 | :exc:`UnicodeDecodeError`). Refer to :ref:`codec-base-classes` for more |
| 51 | information on codec error handling. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 53 | The full details for each codec can also be looked up directly: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
| 55 | .. function:: lookup(encoding) |
| 56 | |
| 57 | Looks up the codec info in the Python codec registry and returns a |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 58 | :class:`CodecInfo` object as defined below. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 60 | Encodings are first looked up in the registry's cache. If not found, the list of |
| 61 | registered search functions is scanned. If no :class:`CodecInfo` object is |
| 62 | found, a :exc:`LookupError` is raised. Otherwise, the :class:`CodecInfo` object |
| 63 | is stored in the cache and returned to the caller. |
| 64 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 65 | .. class:: CodecInfo(encode, decode, streamreader=None, streamwriter=None, incrementalencoder=None, incrementaldecoder=None, name=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 67 | Codec details when looking up the codec registry. The constructor |
| 68 | arguments are stored in attributes of the same name: |
| 69 | |
| 70 | |
| 71 | .. attribute:: name |
| 72 | |
| 73 | The name of the encoding. |
| 74 | |
| 75 | |
| 76 | .. attribute:: encode |
| 77 | decode |
| 78 | |
| 79 | The stateless encoding and decoding functions. These must be |
| 80 | functions or methods which have the same interface as |
| 81 | the :meth:`~Codec.encode` and :meth:`~Codec.decode` methods of Codec |
| 82 | instances (see :ref:`Codec Interface <codec-objects>`). |
| 83 | The functions or methods are expected to work in a stateless mode. |
| 84 | |
| 85 | |
| 86 | .. attribute:: incrementalencoder |
| 87 | incrementaldecoder |
| 88 | |
| 89 | Incremental encoder and decoder classes or factory functions. |
| 90 | These have to provide the interface defined by the base classes |
| 91 | :class:`IncrementalEncoder` and :class:`IncrementalDecoder`, |
| 92 | respectively. Incremental codecs can maintain state. |
| 93 | |
| 94 | |
| 95 | .. attribute:: streamwriter |
| 96 | streamreader |
| 97 | |
| 98 | Stream writer and reader classes or factory functions. These have to |
| 99 | provide the interface defined by the base classes |
| 100 | :class:`StreamWriter` and :class:`StreamReader`, respectively. |
| 101 | Stream codecs can maintain state. |
| 102 | |
| 103 | To simplify access to the various codec components, the module provides |
| 104 | these additional functions which use :func:`lookup` for the codec lookup: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | |
| 106 | .. function:: getencoder(encoding) |
| 107 | |
| 108 | Look up the codec for the given encoding and return its encoder function. |
| 109 | |
| 110 | Raises a :exc:`LookupError` in case the encoding cannot be found. |
| 111 | |
| 112 | |
| 113 | .. function:: getdecoder(encoding) |
| 114 | |
| 115 | Look up the codec for the given encoding and return its decoder function. |
| 116 | |
| 117 | Raises a :exc:`LookupError` in case the encoding cannot be found. |
| 118 | |
| 119 | |
| 120 | .. function:: getincrementalencoder(encoding) |
| 121 | |
| 122 | Look up the codec for the given encoding and return its incremental encoder |
| 123 | class or factory function. |
| 124 | |
| 125 | Raises a :exc:`LookupError` in case the encoding cannot be found or the codec |
| 126 | doesn't support an incremental encoder. |
| 127 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
| 129 | .. function:: getincrementaldecoder(encoding) |
| 130 | |
| 131 | Look up the codec for the given encoding and return its incremental decoder |
| 132 | class or factory function. |
| 133 | |
| 134 | Raises a :exc:`LookupError` in case the encoding cannot be found or the codec |
| 135 | doesn't support an incremental decoder. |
| 136 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
| 138 | .. function:: getreader(encoding) |
| 139 | |
| 140 | Look up the codec for the given encoding and return its StreamReader class or |
| 141 | factory function. |
| 142 | |
| 143 | Raises a :exc:`LookupError` in case the encoding cannot be found. |
| 144 | |
| 145 | |
| 146 | .. function:: getwriter(encoding) |
| 147 | |
| 148 | Look up the codec for the given encoding and return its StreamWriter class or |
| 149 | factory function. |
| 150 | |
| 151 | Raises a :exc:`LookupError` in case the encoding cannot be found. |
| 152 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 153 | Custom codecs are made available by registering a suitable codec search |
| 154 | function: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 156 | .. function:: register(search_function) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 158 | Register a codec search function. Search functions are expected to take one |
| 159 | argument, being the encoding name in all lower case letters, and return a |
| 160 | :class:`CodecInfo` object. In case a search function cannot find |
| 161 | a given encoding, it should return ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
| 163 | .. note:: |
| 164 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 165 | Search function registration is not currently reversible, |
| 166 | which may cause problems in some cases, such as unit testing or |
| 167 | module reloading. |
| 168 | |
| 169 | While the builtin :func:`open` and the associated :mod:`io` module are the |
| 170 | recommended approach for working with encoded text files, this module |
| 171 | provides additional utility functions and classes that allow the use of a |
| 172 | wider range of codecs when working with binary files: |
| 173 | |
| 174 | .. function:: open(filename, mode='r', encoding=None, errors='strict', buffering=1) |
| 175 | |
| 176 | Open an encoded file using the given *mode* and return an instance of |
| 177 | :class:`StreamReaderWriter`, providing transparent encoding/decoding. |
| 178 | The default file mode is ``'r'``, meaning to open the file in read mode. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | |
Christian Heimes | 18c6689 | 2008-02-17 13:31:39 +0000 | [diff] [blame] | 180 | .. note:: |
| 181 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 182 | Underlying encoded files are always opened in binary mode. |
| 183 | No automatic conversion of ``'\n'`` is done on reading and writing. |
| 184 | The *mode* argument may be any binary mode acceptable to the built-in |
| 185 | :func:`open` function; the ``'b'`` is automatically added. |
Christian Heimes | 18c6689 | 2008-02-17 13:31:39 +0000 | [diff] [blame] | 186 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 187 | *encoding* specifies the encoding which is to be used for the file. |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 188 | Any encoding that encodes to and decodes from bytes is allowed, and |
| 189 | the data types supported by the file methods depend on the codec used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | |
| 191 | *errors* may be given to define the error handling. It defaults to ``'strict'`` |
| 192 | which causes a :exc:`ValueError` to be raised in case an encoding error occurs. |
| 193 | |
| 194 | *buffering* has the same meaning as for the built-in :func:`open` function. It |
| 195 | defaults to line buffered. |
| 196 | |
| 197 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 198 | .. function:: EncodedFile(file, data_encoding, file_encoding=None, errors='strict') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 200 | Return a :class:`StreamRecoder` instance, a wrapped version of *file* |
| 201 | which provides transparent transcoding. The original file is closed |
| 202 | when the wrapped version is closed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 204 | Data written to the wrapped file is decoded according to the given |
| 205 | *data_encoding* and then written to the original file as bytes using |
| 206 | *file_encoding*. Bytes read from the original file are decoded |
| 207 | according to *file_encoding*, and the result is encoded |
| 208 | using *data_encoding*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 210 | If *file_encoding* is not given, it defaults to *data_encoding*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 211 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 212 | *errors* may be given to define the error handling. It defaults to |
| 213 | ``'strict'``, which causes :exc:`ValueError` to be raised in case an encoding |
| 214 | error occurs. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 215 | |
| 216 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 217 | .. function:: iterencode(iterator, encoding, errors='strict', **kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | |
| 219 | Uses an incremental encoder to iteratively encode the input provided by |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 220 | *iterator*. This function is a :term:`generator`. |
| 221 | The *errors* argument (as well as any |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 222 | other keyword argument) is passed through to the incremental encoder. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 225 | .. function:: iterdecode(iterator, encoding, errors='strict', **kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 226 | |
| 227 | Uses an incremental decoder to iteratively decode the input provided by |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 228 | *iterator*. This function is a :term:`generator`. |
| 229 | The *errors* argument (as well as any |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 230 | other keyword argument) is passed through to the incremental decoder. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 232 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | The module also provides the following constants which are useful for reading |
| 234 | and writing to platform dependent files: |
| 235 | |
| 236 | |
| 237 | .. data:: BOM |
| 238 | BOM_BE |
| 239 | BOM_LE |
| 240 | BOM_UTF8 |
| 241 | BOM_UTF16 |
| 242 | BOM_UTF16_BE |
| 243 | BOM_UTF16_LE |
| 244 | BOM_UTF32 |
| 245 | BOM_UTF32_BE |
| 246 | BOM_UTF32_LE |
| 247 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 248 | These constants define various byte sequences, |
| 249 | being Unicode byte order marks (BOMs) for several encodings. They are |
| 250 | used in UTF-16 and UTF-32 data streams to indicate the byte order used, |
| 251 | and in UTF-8 as a Unicode signature. :const:`BOM_UTF16` is either |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 | :const:`BOM_UTF16_BE` or :const:`BOM_UTF16_LE` depending on the platform's |
| 253 | native byte order, :const:`BOM` is an alias for :const:`BOM_UTF16`, |
| 254 | :const:`BOM_LE` for :const:`BOM_UTF16_LE` and :const:`BOM_BE` for |
| 255 | :const:`BOM_UTF16_BE`. The others represent the BOM in UTF-8 and UTF-32 |
| 256 | encodings. |
| 257 | |
| 258 | |
| 259 | .. _codec-base-classes: |
| 260 | |
| 261 | Codec Base Classes |
| 262 | ------------------ |
| 263 | |
| 264 | The :mod:`codecs` module defines a set of base classes which define the |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 265 | interfaces for working with codec objects, and can also be used as the basis |
| 266 | for custom codec implementations. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
| 268 | Each codec has to define four interfaces to make it usable as codec in Python: |
| 269 | stateless encoder, stateless decoder, stream reader and stream writer. The |
| 270 | stream reader and writers typically reuse the stateless encoder/decoder to |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 271 | implement the file protocols. Codec authors also need to define how the |
| 272 | codec will handle encoding and decoding errors. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 274 | |
Nick Coghlan | f212636 | 2015-01-07 13:14:47 +1000 | [diff] [blame] | 275 | .. _surrogateescape: |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 276 | .. _error-handlers: |
| 277 | |
| 278 | Error Handlers |
| 279 | ^^^^^^^^^^^^^^ |
| 280 | |
| 281 | To simplify and standardize error handling, |
| 282 | codecs may implement different error handling schemes by |
| 283 | accepting the *errors* string argument. The following string values are |
| 284 | defined and implemented by all standard Python codecs: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 285 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 286 | .. tabularcolumns:: |l|L| |
| 287 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 288 | +-------------------------+-----------------------------------------------+ |
| 289 | | Value | Meaning | |
| 290 | +=========================+===============================================+ |
| 291 | | ``'strict'`` | Raise :exc:`UnicodeError` (or a subclass); | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 292 | | | this is the default. Implemented in | |
| 293 | | | :func:`strict_errors`. | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 294 | +-------------------------+-----------------------------------------------+ |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 295 | | ``'ignore'`` | Ignore the malformed data and continue | |
| 296 | | | without further notice. Implemented in | |
| 297 | | | :func:`ignore_errors`. | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 | +-------------------------+-----------------------------------------------+ |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 299 | |
| 300 | The following error handlers are only applicable to |
| 301 | :term:`text encodings <text encoding>`: |
| 302 | |
| 303 | +-------------------------+-----------------------------------------------+ |
| 304 | | Value | Meaning | |
| 305 | +=========================+===============================================+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 306 | | ``'replace'`` | Replace with a suitable replacement | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 307 | | | marker; Python will use the official | |
| 308 | | | ``U+FFFD`` REPLACEMENT CHARACTER for the | |
| 309 | | | built-in codecs on decoding, and '?' on | |
| 310 | | | encoding. Implemented in | |
| 311 | | | :func:`replace_errors`. | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 312 | +-------------------------+-----------------------------------------------+ |
| 313 | | ``'xmlcharrefreplace'`` | Replace with the appropriate XML character | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 314 | | | reference (only for encoding). Implemented | |
| 315 | | | in :func:`xmlcharrefreplace_errors`. | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 316 | +-------------------------+-----------------------------------------------+ |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 317 | | ``'backslashreplace'`` | Replace with backslashed escape sequences. | |
| 318 | | | Implemented in | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 319 | | | :func:`backslashreplace_errors`. | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 320 | +-------------------------+-----------------------------------------------+ |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 321 | | ``'namereplace'`` | Replace with ``\N{...}`` escape sequences | |
Nick Coghlan | f212636 | 2015-01-07 13:14:47 +1000 | [diff] [blame] | 322 | | | (only for encoding). Implemented in | |
| 323 | | | :func:`namereplace_errors`. | |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 324 | +-------------------------+-----------------------------------------------+ |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 325 | | ``'surrogateescape'`` | On decoding, replace byte with individual | |
| 326 | | | surrogate code ranging from ``U+DC80`` to | |
| 327 | | | ``U+DCFF``. This code will then be turned | |
| 328 | | | back into the same byte when the | |
| 329 | | | ``'surrogateescape'`` error handler is used | |
| 330 | | | when encoding the data. (See :pep:`383` for | |
| 331 | | | more.) | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 332 | +-------------------------+-----------------------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 333 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 334 | In addition, the following error handler is specific to the given codecs: |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 335 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 336 | +-------------------+------------------------+-------------------------------------------+ |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 337 | | Value | Codecs | Meaning | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 338 | +===================+========================+===========================================+ |
| 339 | |``'surrogatepass'``| utf-8, utf-16, utf-32, | Allow encoding and decoding of surrogate | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 340 | | | utf-16-be, utf-16-le, | codes. These codecs normally treat the | |
| 341 | | | utf-32-be, utf-32-le | presence of surrogates as an error. | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 342 | +-------------------+------------------------+-------------------------------------------+ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 343 | |
| 344 | .. versionadded:: 3.1 |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 345 | The ``'surrogateescape'`` and ``'surrogatepass'`` error handlers. |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 346 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 347 | .. versionchanged:: 3.4 |
| 348 | The ``'surrogatepass'`` error handlers now works with utf-16\* and utf-32\* codecs. |
| 349 | |
Berker Peksag | 87f6c22 | 2014-11-25 18:59:20 +0200 | [diff] [blame] | 350 | .. versionadded:: 3.5 |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 351 | The ``'namereplace'`` error handler. |
| 352 | |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 353 | .. versionchanged:: 3.5 |
| 354 | The ``'backslashreplace'`` error handlers now works with decoding and |
| 355 | translating. |
| 356 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 357 | The set of allowed values can be extended by registering a new named error |
| 358 | handler: |
| 359 | |
| 360 | .. function:: register_error(name, error_handler) |
| 361 | |
| 362 | Register the error handling function *error_handler* under the name *name*. |
| 363 | The *error_handler* argument will be called during encoding and decoding |
| 364 | in case of an error, when *name* is specified as the errors parameter. |
| 365 | |
| 366 | For encoding, *error_handler* will be called with a :exc:`UnicodeEncodeError` |
| 367 | instance, which contains information about the location of the error. The |
| 368 | error handler must either raise this or a different exception, or return a |
| 369 | tuple with a replacement for the unencodable part of the input and a position |
| 370 | where encoding should continue. The replacement may be either :class:`str` or |
| 371 | :class:`bytes`. If the replacement is bytes, the encoder will simply copy |
| 372 | them into the output buffer. If the replacement is a string, the encoder will |
| 373 | encode the replacement. Encoding continues on original input at the |
| 374 | specified position. Negative position values will be treated as being |
| 375 | relative to the end of the input string. If the resulting position is out of |
| 376 | bound an :exc:`IndexError` will be raised. |
| 377 | |
| 378 | Decoding and translating works similarly, except :exc:`UnicodeDecodeError` or |
| 379 | :exc:`UnicodeTranslateError` will be passed to the handler and that the |
| 380 | replacement from the error handler will be put into the output directly. |
| 381 | |
| 382 | |
| 383 | Previously registered error handlers (including the standard error handlers) |
| 384 | can be looked up by name: |
| 385 | |
| 386 | .. function:: lookup_error(name) |
| 387 | |
| 388 | Return the error handler previously registered under the name *name*. |
| 389 | |
| 390 | Raises a :exc:`LookupError` in case the handler cannot be found. |
| 391 | |
| 392 | The following standard error handlers are also made available as module level |
| 393 | functions: |
| 394 | |
| 395 | .. function:: strict_errors(exception) |
| 396 | |
| 397 | Implements the ``'strict'`` error handling: each encoding or |
| 398 | decoding error raises a :exc:`UnicodeError`. |
| 399 | |
| 400 | |
| 401 | .. function:: replace_errors(exception) |
| 402 | |
| 403 | Implements the ``'replace'`` error handling (for :term:`text encodings |
| 404 | <text encoding>` only): substitutes ``'?'`` for encoding errors |
| 405 | (to be encoded by the codec), and ``'\ufffd'`` (the Unicode replacement |
Georg Brandl | 7e91af3 | 2015-02-25 13:05:53 +0100 | [diff] [blame] | 406 | character) for decoding errors. |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 407 | |
| 408 | |
| 409 | .. function:: ignore_errors(exception) |
| 410 | |
| 411 | Implements the ``'ignore'`` error handling: malformed data is ignored and |
| 412 | encoding or decoding is continued without further notice. |
| 413 | |
| 414 | |
| 415 | .. function:: xmlcharrefreplace_errors(exception) |
| 416 | |
| 417 | Implements the ``'xmlcharrefreplace'`` error handling (for encoding with |
| 418 | :term:`text encodings <text encoding>` only): the |
| 419 | unencodable character is replaced by an appropriate XML character reference. |
| 420 | |
| 421 | |
| 422 | .. function:: backslashreplace_errors(exception) |
| 423 | |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 424 | Implements the ``'backslashreplace'`` error handling (for |
| 425 | :term:`text encodings <text encoding>` only): malformed data is |
| 426 | replaced by a backslashed escape sequence. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | |
Nick Coghlan | 582acb7 | 2015-01-07 00:37:01 +1000 | [diff] [blame] | 428 | .. function:: namereplace_errors(exception) |
| 429 | |
Nick Coghlan | f212636 | 2015-01-07 13:14:47 +1000 | [diff] [blame] | 430 | Implements the ``'namereplace'`` error handling (for encoding with |
| 431 | :term:`text encodings <text encoding>` only): the |
Nick Coghlan | 582acb7 | 2015-01-07 00:37:01 +1000 | [diff] [blame] | 432 | unencodable character is replaced by a ``\N{...}`` escape sequence. |
| 433 | |
| 434 | .. versionadded:: 3.5 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 435 | |
| 436 | |
| 437 | .. _codec-objects: |
| 438 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 439 | Stateless Encoding and Decoding |
| 440 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 441 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 442 | The base :class:`Codec` class defines these methods which also define the |
| 443 | function interfaces of the stateless encoder and decoder: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 444 | |
| 445 | |
| 446 | .. method:: Codec.encode(input[, errors]) |
| 447 | |
| 448 | Encodes the object *input* and returns a tuple (output object, length consumed). |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 449 | For instance, :term:`text encoding` converts |
| 450 | a string object to a bytes object using a particular |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 451 | character set encoding (e.g., ``cp1252`` or ``iso-8859-1``). |
| 452 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 453 | The *errors* argument defines the error handling to apply. |
| 454 | It defaults to ``'strict'`` handling. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 455 | |
| 456 | The method may not store state in the :class:`Codec` instance. Use |
Berker Peksag | 41ca828 | 2015-07-30 18:26:10 +0300 | [diff] [blame] | 457 | :class:`StreamWriter` for codecs which have to keep state in order to make |
| 458 | encoding efficient. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 459 | |
| 460 | The encoder must be able to handle zero length input and return an empty object |
| 461 | of the output object type in this situation. |
| 462 | |
| 463 | |
| 464 | .. method:: Codec.decode(input[, errors]) |
| 465 | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 466 | Decodes the object *input* and returns a tuple (output object, length |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 467 | consumed). For instance, for a :term:`text encoding`, decoding converts |
| 468 | a bytes object encoded using a particular |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 469 | character set encoding to a string object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 470 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 471 | For text encodings and bytes-to-bytes codecs, |
| 472 | *input* must be a bytes object or one which provides the read-only |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 473 | buffer interface -- for example, buffer objects and memory mapped files. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 474 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 475 | The *errors* argument defines the error handling to apply. |
| 476 | It defaults to ``'strict'`` handling. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 477 | |
| 478 | The method may not store state in the :class:`Codec` instance. Use |
Berker Peksag | 41ca828 | 2015-07-30 18:26:10 +0300 | [diff] [blame] | 479 | :class:`StreamReader` for codecs which have to keep state in order to make |
| 480 | decoding efficient. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 481 | |
| 482 | The decoder must be able to handle zero length input and return an empty object |
| 483 | of the output object type in this situation. |
| 484 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 485 | |
| 486 | Incremental Encoding and Decoding |
| 487 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 488 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 489 | The :class:`IncrementalEncoder` and :class:`IncrementalDecoder` classes provide |
| 490 | the basic interface for incremental encoding and decoding. Encoding/decoding the |
| 491 | input isn't done with one call to the stateless encoder/decoder function, but |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 492 | with multiple calls to the |
| 493 | :meth:`~IncrementalEncoder.encode`/:meth:`~IncrementalDecoder.decode` method of |
| 494 | the incremental encoder/decoder. The incremental encoder/decoder keeps track of |
| 495 | the encoding/decoding process during method calls. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 496 | |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 497 | The joined output of calls to the |
| 498 | :meth:`~IncrementalEncoder.encode`/:meth:`~IncrementalDecoder.decode` method is |
| 499 | the same as if all the single inputs were joined into one, and this input was |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 500 | encoded/decoded with the stateless encoder/decoder. |
| 501 | |
| 502 | |
| 503 | .. _incremental-encoder-objects: |
| 504 | |
| 505 | IncrementalEncoder Objects |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 506 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 507 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 508 | The :class:`IncrementalEncoder` class is used for encoding an input in multiple |
| 509 | steps. It defines the following methods which every incremental encoder must |
| 510 | define in order to be compatible with the Python codec registry. |
| 511 | |
| 512 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 513 | .. class:: IncrementalEncoder(errors='strict') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 514 | |
| 515 | Constructor for an :class:`IncrementalEncoder` instance. |
| 516 | |
| 517 | All incremental encoders must provide this constructor interface. They are free |
| 518 | to add additional keyword arguments, but only the ones defined here are used by |
| 519 | the Python codec registry. |
| 520 | |
| 521 | The :class:`IncrementalEncoder` may implement different error handling schemes |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 522 | by providing the *errors* keyword argument. See :ref:`error-handlers` for |
| 523 | possible values. |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 524 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 525 | The *errors* argument will be assigned to an attribute of the same name. |
| 526 | Assigning to this attribute makes it possible to switch between different error |
| 527 | handling strategies during the lifetime of the :class:`IncrementalEncoder` |
| 528 | object. |
| 529 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 530 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 531 | .. method:: encode(object[, final]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 532 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 533 | Encodes *object* (taking the current state of the encoder into account) |
| 534 | and returns the resulting encoded object. If this is the last call to |
| 535 | :meth:`encode` *final* must be true (the default is false). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 536 | |
| 537 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 538 | .. method:: reset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 539 | |
Victor Stinner | e15dce3 | 2011-05-30 22:56:00 +0200 | [diff] [blame] | 540 | Reset the encoder to the initial state. The output is discarded: call |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 541 | ``.encode(object, final=True)``, passing an empty byte or text string |
| 542 | if necessary, to reset the encoder and to get the output. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 543 | |
| 544 | |
| 545 | .. method:: IncrementalEncoder.getstate() |
| 546 | |
| 547 | Return the current state of the encoder which must be an integer. The |
| 548 | implementation should make sure that ``0`` is the most common state. (States |
| 549 | that are more complicated than integers can be converted into an integer by |
| 550 | marshaling/pickling the state and encoding the bytes of the resulting string |
| 551 | into an integer). |
| 552 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 553 | |
| 554 | .. method:: IncrementalEncoder.setstate(state) |
| 555 | |
| 556 | Set the state of the encoder to *state*. *state* must be an encoder state |
| 557 | returned by :meth:`getstate`. |
| 558 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 559 | |
| 560 | .. _incremental-decoder-objects: |
| 561 | |
| 562 | IncrementalDecoder Objects |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 563 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 564 | |
| 565 | The :class:`IncrementalDecoder` class is used for decoding an input in multiple |
| 566 | steps. It defines the following methods which every incremental decoder must |
| 567 | define in order to be compatible with the Python codec registry. |
| 568 | |
| 569 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 570 | .. class:: IncrementalDecoder(errors='strict') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 571 | |
| 572 | Constructor for an :class:`IncrementalDecoder` instance. |
| 573 | |
| 574 | All incremental decoders must provide this constructor interface. They are free |
| 575 | to add additional keyword arguments, but only the ones defined here are used by |
| 576 | the Python codec registry. |
| 577 | |
| 578 | The :class:`IncrementalDecoder` may implement different error handling schemes |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 579 | by providing the *errors* keyword argument. See :ref:`error-handlers` for |
| 580 | possible values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 581 | |
| 582 | The *errors* argument will be assigned to an attribute of the same name. |
| 583 | Assigning to this attribute makes it possible to switch between different error |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 584 | handling strategies during the lifetime of the :class:`IncrementalDecoder` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 585 | object. |
| 586 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 587 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 588 | .. method:: decode(object[, final]) |
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 | Decodes *object* (taking the current state of the decoder into account) |
| 591 | and returns the resulting decoded object. If this is the last call to |
| 592 | :meth:`decode` *final* must be true (the default is false). If *final* is |
| 593 | true the decoder must decode the input completely and must flush all |
| 594 | buffers. If this isn't possible (e.g. because of incomplete byte sequences |
| 595 | at the end of the input) it must initiate error handling just like in the |
| 596 | stateless case (which might raise an exception). |
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 | Reset the decoder to the initial state. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 602 | |
| 603 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 604 | .. method:: getstate() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 605 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 606 | Return the current state of the decoder. This must be a tuple with two |
| 607 | items, the first must be the buffer containing the still undecoded |
| 608 | input. The second must be an integer and can be additional state |
| 609 | info. (The implementation should make sure that ``0`` is the most common |
| 610 | additional state info.) If this additional state info is ``0`` it must be |
| 611 | possible to set the decoder to the state which has no input buffered and |
| 612 | ``0`` as the additional state info, so that feeding the previously |
| 613 | buffered input to the decoder returns it to the previous state without |
| 614 | producing any output. (Additional state info that is more complicated than |
| 615 | integers can be converted into an integer by marshaling/pickling the info |
| 616 | and encoding the bytes of the resulting string into an integer.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 617 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 618 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 619 | .. method:: setstate(state) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 620 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 621 | Set the state of the encoder to *state*. *state* must be a decoder state |
| 622 | returned by :meth:`getstate`. |
| 623 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 624 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 625 | Stream Encoding and Decoding |
| 626 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 627 | |
| 628 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 629 | The :class:`StreamWriter` and :class:`StreamReader` classes provide generic |
| 630 | working interfaces which can be used to implement new encoding submodules very |
| 631 | easily. See :mod:`encodings.utf_8` for an example of how this is done. |
| 632 | |
| 633 | |
| 634 | .. _stream-writer-objects: |
| 635 | |
| 636 | StreamWriter Objects |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 637 | ~~~~~~~~~~~~~~~~~~~~ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 638 | |
| 639 | The :class:`StreamWriter` class is a subclass of :class:`Codec` and defines the |
| 640 | following methods which every stream writer must define in order to be |
| 641 | compatible with the Python codec registry. |
| 642 | |
| 643 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 644 | .. class:: StreamWriter(stream, errors='strict') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 645 | |
| 646 | Constructor for a :class:`StreamWriter` instance. |
| 647 | |
| 648 | All stream writers must provide this constructor interface. They are free to add |
| 649 | additional keyword arguments, but only the ones defined here are used by the |
| 650 | Python codec registry. |
| 651 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 652 | The *stream* argument must be a file-like object open for writing |
| 653 | text or binary data, as appropriate for the specific codec. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 654 | |
| 655 | The :class:`StreamWriter` may implement different error handling schemes by |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 656 | providing the *errors* keyword argument. See :ref:`error-handlers` for |
| 657 | the standard error handlers the underlying stream codec may support. |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 658 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 659 | The *errors* argument will be assigned to an attribute of the same name. |
| 660 | Assigning to this attribute makes it possible to switch between different error |
| 661 | handling strategies during the lifetime of the :class:`StreamWriter` object. |
| 662 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 663 | .. method:: write(object) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 664 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 665 | Writes the object's contents encoded to the stream. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 666 | |
| 667 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 668 | .. method:: writelines(list) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 669 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 670 | Writes the concatenated list of strings to the stream (possibly by reusing |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 671 | the :meth:`write` method). The standard bytes-to-bytes codecs |
| 672 | do not support this method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 673 | |
| 674 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 675 | .. method:: reset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 676 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 677 | Flushes and resets the codec buffers used for keeping state. |
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 | Calling this method should ensure that the data on the output is put into |
| 680 | a clean state that allows appending of new fresh data without having to |
| 681 | rescan the whole stream to recover state. |
| 682 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 683 | |
| 684 | In addition to the above methods, the :class:`StreamWriter` must also inherit |
| 685 | all other methods and attributes from the underlying stream. |
| 686 | |
| 687 | |
| 688 | .. _stream-reader-objects: |
| 689 | |
| 690 | StreamReader Objects |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 691 | ~~~~~~~~~~~~~~~~~~~~ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 692 | |
| 693 | The :class:`StreamReader` class is a subclass of :class:`Codec` and defines the |
| 694 | following methods which every stream reader must define in order to be |
| 695 | compatible with the Python codec registry. |
| 696 | |
| 697 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 698 | .. class:: StreamReader(stream, errors='strict') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 699 | |
| 700 | Constructor for a :class:`StreamReader` instance. |
| 701 | |
| 702 | All stream readers must provide this constructor interface. They are free to add |
| 703 | additional keyword arguments, but only the ones defined here are used by the |
| 704 | Python codec registry. |
| 705 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 706 | The *stream* argument must be a file-like object open for reading |
| 707 | text or binary data, as appropriate for the specific codec. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 708 | |
| 709 | The :class:`StreamReader` may implement different error handling schemes by |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 710 | providing the *errors* keyword argument. See :ref:`error-handlers` for |
| 711 | the standard error handlers the underlying stream codec may support. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 712 | |
| 713 | The *errors* argument will be assigned to an attribute of the same name. |
| 714 | Assigning to this attribute makes it possible to switch between different error |
| 715 | handling strategies during the lifetime of the :class:`StreamReader` object. |
| 716 | |
| 717 | The set of allowed values for the *errors* argument can be extended with |
| 718 | :func:`register_error`. |
| 719 | |
| 720 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 721 | .. method:: read([size[, chars, [firstline]]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 722 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 723 | Decodes data from the stream and returns the resulting object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 724 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 725 | The *chars* argument indicates the number of decoded |
| 726 | code points or bytes to return. The :func:`read` method will |
| 727 | never return more data than requested, but it might return less, |
| 728 | if there is not enough available. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 729 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 730 | The *size* argument indicates the approximate maximum |
| 731 | number of encoded bytes or code points to read |
| 732 | for decoding. The decoder can modify this setting as |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 733 | appropriate. The default value -1 indicates to read and decode as much as |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 734 | possible. This parameter is intended to |
| 735 | prevent having to decode huge files in one step. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 736 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 737 | The *firstline* flag indicates that |
| 738 | it would be sufficient to only return the first |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 739 | line, if there are decoding errors on later lines. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 740 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 741 | The method should use a greedy read strategy meaning that it should read |
| 742 | as much data as is allowed within the definition of the encoding and the |
| 743 | given size, e.g. if optional encoding endings or state markers are |
| 744 | available on the stream, these should be read too. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 745 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 746 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 747 | .. method:: readline([size[, keepends]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 748 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 749 | Read one line from the input stream and return the decoded data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 750 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 751 | *size*, if given, is passed as size argument to the stream's |
Serhiy Storchaka | cca40ff | 2013-07-11 18:26:13 +0300 | [diff] [blame] | 752 | :meth:`read` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 753 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 754 | If *keepends* is false line-endings will be stripped from the lines |
| 755 | returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 756 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 757 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 758 | .. method:: readlines([sizehint[, keepends]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 759 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 760 | Read all lines available on the input stream and return them as a list of |
| 761 | lines. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 762 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 763 | Line-endings are implemented using the codec's decoder method and are |
| 764 | included in the list entries if *keepends* is true. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 765 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 766 | *sizehint*, if given, is passed as the *size* argument to the stream's |
| 767 | :meth:`read` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 768 | |
| 769 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 770 | .. method:: reset() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 771 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 772 | Resets the codec buffers used for keeping state. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 773 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 774 | Note that no stream repositioning should take place. This method is |
| 775 | primarily intended to be able to recover from decoding errors. |
| 776 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 777 | |
| 778 | In addition to the above methods, the :class:`StreamReader` must also inherit |
| 779 | all other methods and attributes from the underlying stream. |
| 780 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 781 | .. _stream-reader-writer: |
| 782 | |
| 783 | StreamReaderWriter Objects |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 784 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 785 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 786 | The :class:`StreamReaderWriter` is a convenience class that allows wrapping |
| 787 | streams which work in both read and write modes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 788 | |
| 789 | The design is such that one can use the factory functions returned by the |
| 790 | :func:`lookup` function to construct the instance. |
| 791 | |
| 792 | |
| 793 | .. class:: StreamReaderWriter(stream, Reader, Writer, errors) |
| 794 | |
| 795 | Creates a :class:`StreamReaderWriter` instance. *stream* must be a file-like |
| 796 | object. *Reader* and *Writer* must be factory functions or classes providing the |
| 797 | :class:`StreamReader` and :class:`StreamWriter` interface resp. Error handling |
| 798 | is done in the same way as defined for the stream readers and writers. |
| 799 | |
| 800 | :class:`StreamReaderWriter` instances define the combined interfaces of |
| 801 | :class:`StreamReader` and :class:`StreamWriter` classes. They inherit all other |
| 802 | methods and attributes from the underlying stream. |
| 803 | |
| 804 | |
| 805 | .. _stream-recoder-objects: |
| 806 | |
| 807 | StreamRecoder Objects |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 808 | ~~~~~~~~~~~~~~~~~~~~~ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 809 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 810 | The :class:`StreamRecoder` translates data from one encoding to another, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 811 | which is sometimes useful when dealing with different encoding environments. |
| 812 | |
| 813 | The design is such that one can use the factory functions returned by the |
| 814 | :func:`lookup` function to construct the instance. |
| 815 | |
| 816 | |
| 817 | .. class:: StreamRecoder(stream, encode, decode, Reader, Writer, errors) |
| 818 | |
| 819 | Creates a :class:`StreamRecoder` instance which implements a two-way conversion: |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 820 | *encode* and *decode* work on the frontend — the data visible to |
| 821 | code calling :meth:`read` and :meth:`write`, while *Reader* and *Writer* |
| 822 | work on the backend — the data in *stream*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 823 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 824 | You can use these objects to do transparent transcodings from e.g. Latin-1 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 825 | to UTF-8 and back. |
| 826 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 827 | The *stream* argument must be a file-like object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 828 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 829 | The *encode* and *decode* arguments must |
| 830 | adhere to the :class:`Codec` interface. *Reader* and |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 831 | *Writer* must be factory functions or classes providing objects of the |
| 832 | :class:`StreamReader` and :class:`StreamWriter` interface respectively. |
| 833 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 834 | Error handling is done in the same way as defined for the stream readers and |
| 835 | writers. |
| 836 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 837 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 838 | :class:`StreamRecoder` instances define the combined interfaces of |
| 839 | :class:`StreamReader` and :class:`StreamWriter` classes. They inherit all other |
| 840 | methods and attributes from the underlying stream. |
| 841 | |
| 842 | |
| 843 | .. _encodings-overview: |
| 844 | |
| 845 | Encodings and Unicode |
| 846 | --------------------- |
| 847 | |
Georg Brandl | 3be472b | 2015-01-14 08:26:30 +0100 | [diff] [blame] | 848 | Strings are stored internally as sequences of code points in |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 849 | range ``0x0``-``0x10FFFF``. (See :pep:`393` for |
| 850 | more details about the implementation.) |
| 851 | Once a string object is used outside of CPU and memory, endianness |
| 852 | and how these arrays are stored as bytes become an issue. As with other |
| 853 | codecs, serialising a string into a sequence of bytes is known as *encoding*, |
| 854 | and recreating the string from the sequence of bytes is known as *decoding*. |
| 855 | There are a variety of different text serialisation codecs, which are |
| 856 | collectivity referred to as :term:`text encodings <text encoding>`. |
| 857 | |
| 858 | The simplest text encoding (called ``'latin-1'`` or ``'iso-8859-1'``) maps |
Georg Brandl | 3be472b | 2015-01-14 08:26:30 +0100 | [diff] [blame] | 859 | the code points 0-255 to the bytes ``0x0``-``0xff``, which means that a string |
| 860 | object that contains code points above ``U+00FF`` can't be encoded with this |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 861 | codec. Doing so will raise a :exc:`UnicodeEncodeError` that looks |
| 862 | like the following (although the details of the error message may differ): |
| 863 | ``UnicodeEncodeError: 'latin-1' codec can't encode character '\u1234' in |
| 864 | position 3: ordinal not in range(256)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 865 | |
| 866 | There's another group of encodings (the so called charmap encodings) that choose |
Georg Brandl | 3be472b | 2015-01-14 08:26:30 +0100 | [diff] [blame] | 867 | a different subset of all Unicode code points and how these code points are |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 868 | mapped to the bytes ``0x0``-``0xff``. To see how this is done simply open |
| 869 | e.g. :file:`encodings/cp1252.py` (which is an encoding that is used primarily on |
| 870 | Windows). There's a string constant with 256 characters that shows you which |
| 871 | character is mapped to which byte value. |
| 872 | |
Georg Brandl | 3be472b | 2015-01-14 08:26:30 +0100 | [diff] [blame] | 873 | All of these encodings can only encode 256 of the 1114112 code points |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 874 | defined in Unicode. A simple and straightforward way that can store each Unicode |
Georg Brandl | 3be472b | 2015-01-14 08:26:30 +0100 | [diff] [blame] | 875 | code point, is to store each code point as four consecutive bytes. There are two |
Ezio Melotti | fbb3981 | 2011-10-25 10:40:38 +0300 | [diff] [blame] | 876 | possibilities: store the bytes in big endian or in little endian order. These |
| 877 | two encodings are called ``UTF-32-BE`` and ``UTF-32-LE`` respectively. Their |
| 878 | disadvantage is that if e.g. you use ``UTF-32-BE`` on a little endian machine you |
| 879 | will always have to swap bytes on encoding and decoding. ``UTF-32`` avoids this |
| 880 | problem: bytes will always be in natural endianness. When these bytes are read |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 881 | by a CPU with a different endianness, then bytes have to be swapped though. To |
Ezio Melotti | fbb3981 | 2011-10-25 10:40:38 +0300 | [diff] [blame] | 882 | be able to detect the endianness of a ``UTF-16`` or ``UTF-32`` byte sequence, |
| 883 | there's the so called BOM ("Byte Order Mark"). This is the Unicode character |
| 884 | ``U+FEFF``. This character can be prepended to every ``UTF-16`` or ``UTF-32`` |
| 885 | byte sequence. The byte swapped version of this character (``0xFFFE``) is an |
| 886 | illegal character that may not appear in a Unicode text. So when the |
| 887 | first character in an ``UTF-16`` or ``UTF-32`` byte sequence |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 888 | appears to be a ``U+FFFE`` the bytes have to be swapped on decoding. |
Ezio Melotti | fbb3981 | 2011-10-25 10:40:38 +0300 | [diff] [blame] | 889 | Unfortunately the character ``U+FEFF`` had a second purpose as |
| 890 | a ``ZERO WIDTH NO-BREAK SPACE``: a character that has no width and doesn't allow |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 891 | a word to be split. It can e.g. be used to give hints to a ligature algorithm. |
| 892 | With Unicode 4.0 using ``U+FEFF`` as a ``ZERO WIDTH NO-BREAK SPACE`` has been |
| 893 | deprecated (with ``U+2060`` (``WORD JOINER``) assuming this role). Nevertheless |
Ezio Melotti | fbb3981 | 2011-10-25 10:40:38 +0300 | [diff] [blame] | 894 | Unicode software still must be able to handle ``U+FEFF`` in both roles: as a BOM |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 895 | 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] | 896 | 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] | 897 | NO-BREAK SPACE`` it's a normal character that will be decoded like any other. |
| 898 | |
| 899 | There's another encoding that is able to encoding the full range of Unicode |
| 900 | characters: UTF-8. UTF-8 is an 8-bit encoding, which means there are no issues |
| 901 | with byte order in UTF-8. Each byte in a UTF-8 byte sequence consists of two |
Ezio Melotti | fbb3981 | 2011-10-25 10:40:38 +0300 | [diff] [blame] | 902 | parts: marker bits (the most significant bits) and payload bits. The marker bits |
Ezio Melotti | 222b208 | 2011-09-01 08:11:28 +0300 | [diff] [blame] | 903 | are a sequence of zero to four ``1`` bits followed by a ``0`` bit. Unicode characters are |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 904 | encoded like this (with x being payload bits, which when concatenated give the |
| 905 | Unicode character): |
| 906 | |
| 907 | +-----------------------------------+----------------------------------------------+ |
| 908 | | Range | Encoding | |
| 909 | +===================================+==============================================+ |
| 910 | | ``U-00000000`` ... ``U-0000007F`` | 0xxxxxxx | |
| 911 | +-----------------------------------+----------------------------------------------+ |
| 912 | | ``U-00000080`` ... ``U-000007FF`` | 110xxxxx 10xxxxxx | |
| 913 | +-----------------------------------+----------------------------------------------+ |
| 914 | | ``U-00000800`` ... ``U-0000FFFF`` | 1110xxxx 10xxxxxx 10xxxxxx | |
| 915 | +-----------------------------------+----------------------------------------------+ |
Ezio Melotti | 222b208 | 2011-09-01 08:11:28 +0300 | [diff] [blame] | 916 | | ``U-00010000`` ... ``U-0010FFFF`` | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 917 | +-----------------------------------+----------------------------------------------+ |
| 918 | |
| 919 | The least significant bit of the Unicode character is the rightmost x bit. |
| 920 | |
| 921 | 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] | 922 | the decoded string (even if it's the first character) is treated as a ``ZERO |
| 923 | WIDTH NO-BREAK SPACE``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 924 | |
| 925 | Without external information it's impossible to reliably determine which |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 926 | encoding was used for encoding a string. Each charmap encoding can |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 927 | decode any random byte sequence. However that's not possible with UTF-8, as |
| 928 | 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] | 929 | 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] | 930 | detected, Microsoft invented a variant of UTF-8 (that Python 2.5 calls |
| 931 | ``"utf-8-sig"``) for its Notepad program: Before any of the Unicode characters |
| 932 | is written to the file, a UTF-8 encoded BOM (which looks like this as a byte |
| 933 | sequence: ``0xef``, ``0xbb``, ``0xbf``) is written. As it's rather improbable |
| 934 | that any charmap encoded file starts with these byte values (which would e.g. |
| 935 | map to |
| 936 | |
| 937 | | LATIN SMALL LETTER I WITH DIAERESIS |
| 938 | | RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK |
| 939 | | INVERTED QUESTION MARK |
| 940 | |
Ezio Melotti | fbb3981 | 2011-10-25 10:40:38 +0300 | [diff] [blame] | 941 | in iso-8859-1), this increases the probability that a ``utf-8-sig`` encoding can be |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 942 | correctly guessed from the byte sequence. So here the BOM is not used to be able |
| 943 | to determine the byte order used for generating the byte sequence, but as a |
| 944 | signature that helps in guessing the encoding. On encoding the utf-8-sig codec |
| 945 | will write ``0xef``, ``0xbb``, ``0xbf`` as the first three bytes to the file. On |
Ezio Melotti | fbb3981 | 2011-10-25 10:40:38 +0300 | [diff] [blame] | 946 | decoding ``utf-8-sig`` will skip those three bytes if they appear as the first |
| 947 | three bytes in the file. In UTF-8, the use of the BOM is discouraged and |
| 948 | should generally be avoided. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 949 | |
| 950 | |
| 951 | .. _standard-encodings: |
| 952 | |
| 953 | Standard Encodings |
| 954 | ------------------ |
| 955 | |
| 956 | Python comes with a number of codecs built-in, either implemented as C functions |
| 957 | or with dictionaries as mapping tables. The following table lists the codecs by |
| 958 | name, together with a few common aliases, and the languages for which the |
| 959 | encoding is likely used. Neither the list of aliases nor the list of languages |
| 960 | 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] | 961 | case or use a hyphen instead of an underscore are also valid aliases; therefore, |
| 962 | 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] | 963 | |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 964 | .. impl-detail:: |
| 965 | |
| 966 | Some common encodings can bypass the codecs lookup machinery to |
| 967 | improve performance. These optimization opportunities are only |
| 968 | recognized by CPython for a limited set of aliases: utf-8, utf8, |
| 969 | latin-1, latin1, iso-8859-1, mbcs (Windows only), ascii, utf-16, |
| 970 | and utf-32. Using alternative spellings for these encodings may |
| 971 | result in slower execution. |
| 972 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 973 | Many of the character sets support the same languages. They vary in individual |
| 974 | characters (e.g. whether the EURO SIGN is supported or not), and in the |
| 975 | assignment of characters to code positions. For the European languages in |
| 976 | particular, the following variants typically exist: |
| 977 | |
| 978 | * an ISO 8859 codeset |
| 979 | |
| 980 | * a Microsoft Windows code page, which is typically derived from a 8859 codeset, |
| 981 | but replaces control characters with additional graphic characters |
| 982 | |
| 983 | * an IBM EBCDIC code page |
| 984 | |
| 985 | * an IBM PC code page, which is ASCII compatible |
| 986 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 987 | .. tabularcolumns:: |l|p{0.3\linewidth}|p{0.3\linewidth}| |
| 988 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 989 | +-----------------+--------------------------------+--------------------------------+ |
| 990 | | Codec | Aliases | Languages | |
| 991 | +=================+================================+================================+ |
| 992 | | ascii | 646, us-ascii | English | |
| 993 | +-----------------+--------------------------------+--------------------------------+ |
| 994 | | big5 | big5-tw, csbig5 | Traditional Chinese | |
| 995 | +-----------------+--------------------------------+--------------------------------+ |
| 996 | | big5hkscs | big5-hkscs, hkscs | Traditional Chinese | |
| 997 | +-----------------+--------------------------------+--------------------------------+ |
| 998 | | cp037 | IBM037, IBM039 | English | |
| 999 | +-----------------+--------------------------------+--------------------------------+ |
R David Murray | 47d083c | 2014-03-07 21:00:34 -0500 | [diff] [blame] | 1000 | | cp273 | 273, IBM273, csIBM273 | German | |
| 1001 | | | | | |
| 1002 | | | | .. versionadded:: 3.4 | |
| 1003 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1004 | | cp424 | EBCDIC-CP-HE, IBM424 | Hebrew | |
| 1005 | +-----------------+--------------------------------+--------------------------------+ |
| 1006 | | cp437 | 437, IBM437 | English | |
| 1007 | +-----------------+--------------------------------+--------------------------------+ |
| 1008 | | cp500 | EBCDIC-CP-BE, EBCDIC-CP-CH, | Western Europe | |
| 1009 | | | IBM500 | | |
| 1010 | +-----------------+--------------------------------+--------------------------------+ |
Amaury Forgeot d'Arc | ae6388d | 2009-07-15 19:21:18 +0000 | [diff] [blame] | 1011 | | cp720 | | Arabic | |
| 1012 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1013 | | cp737 | | Greek | |
| 1014 | +-----------------+--------------------------------+--------------------------------+ |
| 1015 | | cp775 | IBM775 | Baltic languages | |
| 1016 | +-----------------+--------------------------------+--------------------------------+ |
| 1017 | | cp850 | 850, IBM850 | Western Europe | |
| 1018 | +-----------------+--------------------------------+--------------------------------+ |
| 1019 | | cp852 | 852, IBM852 | Central and Eastern Europe | |
| 1020 | +-----------------+--------------------------------+--------------------------------+ |
| 1021 | | cp855 | 855, IBM855 | Bulgarian, Byelorussian, | |
| 1022 | | | | Macedonian, Russian, Serbian | |
| 1023 | +-----------------+--------------------------------+--------------------------------+ |
| 1024 | | cp856 | | Hebrew | |
| 1025 | +-----------------+--------------------------------+--------------------------------+ |
| 1026 | | cp857 | 857, IBM857 | Turkish | |
| 1027 | +-----------------+--------------------------------+--------------------------------+ |
Benjamin Peterson | 5a6214a | 2010-06-27 22:41:29 +0000 | [diff] [blame] | 1028 | | cp858 | 858, IBM858 | Western Europe | |
| 1029 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1030 | | cp860 | 860, IBM860 | Portuguese | |
| 1031 | +-----------------+--------------------------------+--------------------------------+ |
| 1032 | | cp861 | 861, CP-IS, IBM861 | Icelandic | |
| 1033 | +-----------------+--------------------------------+--------------------------------+ |
| 1034 | | cp862 | 862, IBM862 | Hebrew | |
| 1035 | +-----------------+--------------------------------+--------------------------------+ |
| 1036 | | cp863 | 863, IBM863 | Canadian | |
| 1037 | +-----------------+--------------------------------+--------------------------------+ |
| 1038 | | cp864 | IBM864 | Arabic | |
| 1039 | +-----------------+--------------------------------+--------------------------------+ |
| 1040 | | cp865 | 865, IBM865 | Danish, Norwegian | |
| 1041 | +-----------------+--------------------------------+--------------------------------+ |
| 1042 | | cp866 | 866, IBM866 | Russian | |
| 1043 | +-----------------+--------------------------------+--------------------------------+ |
| 1044 | | cp869 | 869, CP-GR, IBM869 | Greek | |
| 1045 | +-----------------+--------------------------------+--------------------------------+ |
| 1046 | | cp874 | | Thai | |
| 1047 | +-----------------+--------------------------------+--------------------------------+ |
| 1048 | | cp875 | | Greek | |
| 1049 | +-----------------+--------------------------------+--------------------------------+ |
| 1050 | | cp932 | 932, ms932, mskanji, ms-kanji | Japanese | |
| 1051 | +-----------------+--------------------------------+--------------------------------+ |
| 1052 | | cp949 | 949, ms949, uhc | Korean | |
| 1053 | +-----------------+--------------------------------+--------------------------------+ |
| 1054 | | cp950 | 950, ms950 | Traditional Chinese | |
| 1055 | +-----------------+--------------------------------+--------------------------------+ |
| 1056 | | cp1006 | | Urdu | |
| 1057 | +-----------------+--------------------------------+--------------------------------+ |
| 1058 | | cp1026 | ibm1026 | Turkish | |
| 1059 | +-----------------+--------------------------------+--------------------------------+ |
Serhiy Storchaka | be0c325 | 2013-11-23 18:52:23 +0200 | [diff] [blame] | 1060 | | cp1125 | 1125, ibm1125, cp866u, ruscii | Ukrainian | |
| 1061 | | | | | |
| 1062 | | | | .. versionadded:: 3.4 | |
| 1063 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1064 | | cp1140 | ibm1140 | Western Europe | |
| 1065 | +-----------------+--------------------------------+--------------------------------+ |
| 1066 | | cp1250 | windows-1250 | Central and Eastern Europe | |
| 1067 | +-----------------+--------------------------------+--------------------------------+ |
| 1068 | | cp1251 | windows-1251 | Bulgarian, Byelorussian, | |
| 1069 | | | | Macedonian, Russian, Serbian | |
| 1070 | +-----------------+--------------------------------+--------------------------------+ |
| 1071 | | cp1252 | windows-1252 | Western Europe | |
| 1072 | +-----------------+--------------------------------+--------------------------------+ |
| 1073 | | cp1253 | windows-1253 | Greek | |
| 1074 | +-----------------+--------------------------------+--------------------------------+ |
| 1075 | | cp1254 | windows-1254 | Turkish | |
| 1076 | +-----------------+--------------------------------+--------------------------------+ |
| 1077 | | cp1255 | windows-1255 | Hebrew | |
| 1078 | +-----------------+--------------------------------+--------------------------------+ |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 1079 | | cp1256 | windows-1256 | Arabic | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1080 | +-----------------+--------------------------------+--------------------------------+ |
| 1081 | | cp1257 | windows-1257 | Baltic languages | |
| 1082 | +-----------------+--------------------------------+--------------------------------+ |
| 1083 | | cp1258 | windows-1258 | Vietnamese | |
| 1084 | +-----------------+--------------------------------+--------------------------------+ |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 1085 | | cp65001 | | Windows only: Windows UTF-8 | |
| 1086 | | | | (``CP_UTF8``) | |
| 1087 | | | | | |
| 1088 | | | | .. versionadded:: 3.3 | |
| 1089 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1090 | | euc_jp | eucjp, ujis, u-jis | Japanese | |
| 1091 | +-----------------+--------------------------------+--------------------------------+ |
| 1092 | | euc_jis_2004 | jisx0213, eucjis2004 | Japanese | |
| 1093 | +-----------------+--------------------------------+--------------------------------+ |
| 1094 | | euc_jisx0213 | eucjisx0213 | Japanese | |
| 1095 | +-----------------+--------------------------------+--------------------------------+ |
| 1096 | | euc_kr | euckr, korean, ksc5601, | Korean | |
| 1097 | | | ks_c-5601, ks_c-5601-1987, | | |
| 1098 | | | ksx1001, ks_x-1001 | | |
| 1099 | +-----------------+--------------------------------+--------------------------------+ |
| 1100 | | gb2312 | chinese, csiso58gb231280, euc- | Simplified Chinese | |
| 1101 | | | cn, euccn, eucgb2312-cn, | | |
| 1102 | | | gb2312-1980, gb2312-80, iso- | | |
| 1103 | | | ir-58 | | |
| 1104 | +-----------------+--------------------------------+--------------------------------+ |
| 1105 | | gbk | 936, cp936, ms936 | Unified Chinese | |
| 1106 | +-----------------+--------------------------------+--------------------------------+ |
| 1107 | | gb18030 | gb18030-2000 | Unified Chinese | |
| 1108 | +-----------------+--------------------------------+--------------------------------+ |
| 1109 | | hz | hzgb, hz-gb, hz-gb-2312 | Simplified Chinese | |
| 1110 | +-----------------+--------------------------------+--------------------------------+ |
| 1111 | | iso2022_jp | csiso2022jp, iso2022jp, | Japanese | |
| 1112 | | | iso-2022-jp | | |
| 1113 | +-----------------+--------------------------------+--------------------------------+ |
| 1114 | | iso2022_jp_1 | iso2022jp-1, iso-2022-jp-1 | Japanese | |
| 1115 | +-----------------+--------------------------------+--------------------------------+ |
| 1116 | | iso2022_jp_2 | iso2022jp-2, iso-2022-jp-2 | Japanese, Korean, Simplified | |
| 1117 | | | | Chinese, Western Europe, Greek | |
| 1118 | +-----------------+--------------------------------+--------------------------------+ |
| 1119 | | iso2022_jp_2004 | iso2022jp-2004, | Japanese | |
| 1120 | | | iso-2022-jp-2004 | | |
| 1121 | +-----------------+--------------------------------+--------------------------------+ |
| 1122 | | iso2022_jp_3 | iso2022jp-3, iso-2022-jp-3 | Japanese | |
| 1123 | +-----------------+--------------------------------+--------------------------------+ |
| 1124 | | iso2022_jp_ext | iso2022jp-ext, iso-2022-jp-ext | Japanese | |
| 1125 | +-----------------+--------------------------------+--------------------------------+ |
| 1126 | | iso2022_kr | csiso2022kr, iso2022kr, | Korean | |
| 1127 | | | iso-2022-kr | | |
| 1128 | +-----------------+--------------------------------+--------------------------------+ |
| 1129 | | latin_1 | iso-8859-1, iso8859-1, 8859, | West Europe | |
| 1130 | | | cp819, latin, latin1, L1 | | |
| 1131 | +-----------------+--------------------------------+--------------------------------+ |
| 1132 | | iso8859_2 | iso-8859-2, latin2, L2 | Central and Eastern Europe | |
| 1133 | +-----------------+--------------------------------+--------------------------------+ |
| 1134 | | iso8859_3 | iso-8859-3, latin3, L3 | Esperanto, Maltese | |
| 1135 | +-----------------+--------------------------------+--------------------------------+ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 1136 | | iso8859_4 | iso-8859-4, latin4, L4 | Baltic languages | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1137 | +-----------------+--------------------------------+--------------------------------+ |
| 1138 | | iso8859_5 | iso-8859-5, cyrillic | Bulgarian, Byelorussian, | |
| 1139 | | | | Macedonian, Russian, Serbian | |
| 1140 | +-----------------+--------------------------------+--------------------------------+ |
| 1141 | | iso8859_6 | iso-8859-6, arabic | Arabic | |
| 1142 | +-----------------+--------------------------------+--------------------------------+ |
| 1143 | | iso8859_7 | iso-8859-7, greek, greek8 | Greek | |
| 1144 | +-----------------+--------------------------------+--------------------------------+ |
| 1145 | | iso8859_8 | iso-8859-8, hebrew | Hebrew | |
| 1146 | +-----------------+--------------------------------+--------------------------------+ |
| 1147 | | iso8859_9 | iso-8859-9, latin5, L5 | Turkish | |
| 1148 | +-----------------+--------------------------------+--------------------------------+ |
| 1149 | | iso8859_10 | iso-8859-10, latin6, L6 | Nordic languages | |
| 1150 | +-----------------+--------------------------------+--------------------------------+ |
Victor Stinner | bfd9767 | 2015-09-24 09:04:05 +0200 | [diff] [blame] | 1151 | | iso8859_11 | iso-8859-11, thai | Thai languages | |
| 1152 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 93dc9eb | 2010-03-14 10:56:14 +0000 | [diff] [blame] | 1153 | | iso8859_13 | iso-8859-13, latin7, L7 | Baltic languages | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1154 | +-----------------+--------------------------------+--------------------------------+ |
| 1155 | | iso8859_14 | iso-8859-14, latin8, L8 | Celtic languages | |
| 1156 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 93dc9eb | 2010-03-14 10:56:14 +0000 | [diff] [blame] | 1157 | | iso8859_15 | iso-8859-15, latin9, L9 | Western Europe | |
| 1158 | +-----------------+--------------------------------+--------------------------------+ |
| 1159 | | iso8859_16 | iso-8859-16, latin10, L10 | South-Eastern Europe | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1160 | +-----------------+--------------------------------+--------------------------------+ |
| 1161 | | johab | cp1361, ms1361 | Korean | |
| 1162 | +-----------------+--------------------------------+--------------------------------+ |
| 1163 | | koi8_r | | Russian | |
| 1164 | +-----------------+--------------------------------+--------------------------------+ |
Serhiy Storchaka | f0eeedf | 2015-05-12 23:24:19 +0300 | [diff] [blame] | 1165 | | koi8_t | | Tajik | |
| 1166 | | | | | |
| 1167 | | | | .. versionadded:: 3.5 | |
| 1168 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1169 | | koi8_u | | Ukrainian | |
| 1170 | +-----------------+--------------------------------+--------------------------------+ |
Serhiy Storchaka | ad8a1c3 | 2015-05-12 23:16:55 +0300 | [diff] [blame] | 1171 | | kz1048 | kz_1048, strk1048_2002, rk1048 | Kazakh | |
| 1172 | | | | | |
| 1173 | | | | .. versionadded:: 3.5 | |
| 1174 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1175 | | mac_cyrillic | maccyrillic | Bulgarian, Byelorussian, | |
| 1176 | | | | Macedonian, Russian, Serbian | |
| 1177 | +-----------------+--------------------------------+--------------------------------+ |
| 1178 | | mac_greek | macgreek | Greek | |
| 1179 | +-----------------+--------------------------------+--------------------------------+ |
| 1180 | | mac_iceland | maciceland | Icelandic | |
| 1181 | +-----------------+--------------------------------+--------------------------------+ |
| 1182 | | mac_latin2 | maclatin2, maccentraleurope | Central and Eastern Europe | |
| 1183 | +-----------------+--------------------------------+--------------------------------+ |
Benjamin Peterson | 23110e7 | 2010-08-21 02:54:44 +0000 | [diff] [blame] | 1184 | | mac_roman | macroman, macintosh | Western Europe | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1185 | +-----------------+--------------------------------+--------------------------------+ |
| 1186 | | mac_turkish | macturkish | Turkish | |
| 1187 | +-----------------+--------------------------------+--------------------------------+ |
| 1188 | | ptcp154 | csptcp154, pt154, cp154, | Kazakh | |
| 1189 | | | cyrillic-asian | | |
| 1190 | +-----------------+--------------------------------+--------------------------------+ |
| 1191 | | shift_jis | csshiftjis, shiftjis, sjis, | Japanese | |
| 1192 | | | s_jis | | |
| 1193 | +-----------------+--------------------------------+--------------------------------+ |
| 1194 | | shift_jis_2004 | shiftjis2004, sjis_2004, | Japanese | |
| 1195 | | | sjis2004 | | |
| 1196 | +-----------------+--------------------------------+--------------------------------+ |
| 1197 | | shift_jisx0213 | shiftjisx0213, sjisx0213, | Japanese | |
| 1198 | | | s_jisx0213 | | |
| 1199 | +-----------------+--------------------------------+--------------------------------+ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 1200 | | utf_32 | U32, utf32 | all languages | |
| 1201 | +-----------------+--------------------------------+--------------------------------+ |
| 1202 | | utf_32_be | UTF-32BE | all languages | |
| 1203 | +-----------------+--------------------------------+--------------------------------+ |
| 1204 | | utf_32_le | UTF-32LE | all languages | |
| 1205 | +-----------------+--------------------------------+--------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1206 | | utf_16 | U16, utf16 | all languages | |
| 1207 | +-----------------+--------------------------------+--------------------------------+ |
Victor Stinner | 53a9dd7 | 2010-12-08 22:25:45 +0000 | [diff] [blame] | 1208 | | utf_16_be | UTF-16BE | all languages | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1209 | +-----------------+--------------------------------+--------------------------------+ |
Victor Stinner | 53a9dd7 | 2010-12-08 22:25:45 +0000 | [diff] [blame] | 1210 | | utf_16_le | UTF-16LE | all languages | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1211 | +-----------------+--------------------------------+--------------------------------+ |
| 1212 | | utf_7 | U7, unicode-1-1-utf-7 | all languages | |
| 1213 | +-----------------+--------------------------------+--------------------------------+ |
| 1214 | | utf_8 | U8, UTF, utf8 | all languages | |
| 1215 | +-----------------+--------------------------------+--------------------------------+ |
| 1216 | | utf_8_sig | | all languages | |
| 1217 | +-----------------+--------------------------------+--------------------------------+ |
| 1218 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1219 | .. versionchanged:: 3.4 |
| 1220 | The utf-16\* and utf-32\* encoders no longer allow surrogate code points |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1221 | (``U+D800``--``U+DFFF``) to be encoded. |
| 1222 | The utf-32\* decoders no longer decode |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1223 | byte sequences that correspond to surrogate code points. |
| 1224 | |
| 1225 | |
Nick Coghlan | 650e322 | 2013-05-23 20:24:02 +1000 | [diff] [blame] | 1226 | Python Specific Encodings |
| 1227 | ------------------------- |
| 1228 | |
| 1229 | A number of predefined codecs are specific to Python, so their codec names have |
| 1230 | no meaning outside Python. These are listed in the tables below based on the |
| 1231 | expected input and output types (note that while text encodings are the most |
| 1232 | common use case for codecs, the underlying codec infrastructure supports |
| 1233 | arbitrary data transforms rather than just text encodings). For asymmetric |
| 1234 | codecs, the stated purpose describes the encoding direction. |
| 1235 | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1236 | Text Encodings |
| 1237 | ^^^^^^^^^^^^^^ |
| 1238 | |
Nick Coghlan | 650e322 | 2013-05-23 20:24:02 +1000 | [diff] [blame] | 1239 | The following codecs provide :class:`str` to :class:`bytes` encoding and |
| 1240 | :term:`bytes-like object` to :class:`str` decoding, similar to the Unicode text |
| 1241 | encodings. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1242 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 1243 | .. tabularcolumns:: |l|p{0.3\linewidth}|p{0.3\linewidth}| |
| 1244 | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 1245 | +--------------------+---------+---------------------------+ |
| 1246 | | Codec | Aliases | Purpose | |
| 1247 | +====================+=========+===========================+ |
| 1248 | | idna | | Implements :rfc:`3490`, | |
| 1249 | | | | see also | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1250 | | | | :mod:`encodings.idna`. | |
| 1251 | | | | Only ``errors='strict'`` | |
| 1252 | | | | is supported. | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 1253 | +--------------------+---------+---------------------------+ |
| 1254 | | mbcs | dbcs | Windows only: Encode | |
| 1255 | | | | operand according to the | |
| 1256 | | | | ANSI codepage (CP_ACP) | |
| 1257 | +--------------------+---------+---------------------------+ |
| 1258 | | palmos | | Encoding of PalmOS 3.5 | |
| 1259 | +--------------------+---------+---------------------------+ |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1260 | | punycode | | Implements :rfc:`3492`. | |
| 1261 | | | | Stateful codecs are not | |
| 1262 | | | | supported. | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 1263 | +--------------------+---------+---------------------------+ |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1264 | | raw_unicode_escape | | Latin-1 encoding with | |
| 1265 | | | | ``\uXXXX`` and | |
| 1266 | | | | ``\UXXXXXXXX`` for other | |
| 1267 | | | | code points. Existing | |
| 1268 | | | | backslashes are not | |
| 1269 | | | | escaped in any way. | |
| 1270 | | | | It is used in the Python | |
| 1271 | | | | pickle protocol. | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 1272 | +--------------------+---------+---------------------------+ |
| 1273 | | undefined | | Raise an exception for | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1274 | | | | all conversions, even | |
| 1275 | | | | empty strings. The error | |
| 1276 | | | | handler is ignored. | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 1277 | +--------------------+---------+---------------------------+ |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1278 | | unicode_escape | | Encoding suitable as the | |
| 1279 | | | | contents of a Unicode | |
| 1280 | | | | literal in ASCII-encoded | |
| 1281 | | | | Python source code, | |
| 1282 | | | | except that quotes are | |
| 1283 | | | | not escaped. Decodes from | |
| 1284 | | | | Latin-1 source code. | |
| 1285 | | | | Beware that Python source | |
| 1286 | | | | code actually uses UTF-8 | |
| 1287 | | | | by default. | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 1288 | +--------------------+---------+---------------------------+ |
| 1289 | | unicode_internal | | Return the internal | |
| 1290 | | | | representation of the | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1291 | | | | operand. Stateful codecs | |
| 1292 | | | | are not supported. | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 1293 | | | | | |
| 1294 | | | | .. deprecated:: 3.3 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1295 | | | | This representation is | |
| 1296 | | | | obsoleted by | |
| 1297 | | | | :pep:`393`. | |
Georg Brandl | 30c78d6 | 2008-05-11 14:52:00 +0000 | [diff] [blame] | 1298 | +--------------------+---------+---------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1299 | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1300 | .. _binary-transforms: |
| 1301 | |
| 1302 | Binary Transforms |
| 1303 | ^^^^^^^^^^^^^^^^^ |
| 1304 | |
| 1305 | The following codecs provide binary transforms: :term:`bytes-like object` |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1306 | to :class:`bytes` mappings. They are not supported by :meth:`bytes.decode` |
| 1307 | (which only produces :class:`str` output). |
Nick Coghlan | 650e322 | 2013-05-23 20:24:02 +1000 | [diff] [blame] | 1308 | |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 1309 | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1310 | .. tabularcolumns:: |l|L|L|L| |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 1311 | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1312 | +----------------------+------------------+------------------------------+------------------------------+ |
| 1313 | | Codec | Aliases | Purpose | Encoder / decoder | |
| 1314 | +======================+==================+==============================+==============================+ |
Martin Panter | 06171bd | 2015-09-12 00:34:28 +0000 | [diff] [blame] | 1315 | | base64_codec [#b64]_ | base64, base_64 | Convert operand to multiline | :meth:`base64.encodebytes` / | |
| 1316 | | | | MIME base64 (the result | :meth:`base64.decodebytes` | |
| 1317 | | | | always includes a trailing | | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1318 | | | | ``'\n'``) | | |
| 1319 | | | | | | |
| 1320 | | | | .. versionchanged:: 3.4 | | |
| 1321 | | | | accepts any | | |
| 1322 | | | | :term:`bytes-like object` | | |
| 1323 | | | | as input for encoding and | | |
| 1324 | | | | decoding | | |
| 1325 | +----------------------+------------------+------------------------------+------------------------------+ |
| 1326 | | bz2_codec | bz2 | Compress the operand | :meth:`bz2.compress` / | |
| 1327 | | | | using bz2 | :meth:`bz2.decompress` | |
| 1328 | +----------------------+------------------+------------------------------+------------------------------+ |
Martin Panter | 06171bd | 2015-09-12 00:34:28 +0000 | [diff] [blame] | 1329 | | hex_codec | hex | Convert operand to | :meth:`binascii.b2a_hex` / | |
| 1330 | | | | hexadecimal | :meth:`binascii.a2b_hex` | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1331 | | | | representation, with two | | |
| 1332 | | | | digits per byte | | |
| 1333 | +----------------------+------------------+------------------------------+------------------------------+ |
Martin Panter | 06171bd | 2015-09-12 00:34:28 +0000 | [diff] [blame] | 1334 | | quopri_codec | quopri, | Convert operand to MIME | :meth:`quopri.encode` with | |
| 1335 | | | quotedprintable, | quoted printable | ``quotetabs=True`` / | |
| 1336 | | | quoted_printable | | :meth:`quopri.decode` | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1337 | +----------------------+------------------+------------------------------+------------------------------+ |
| 1338 | | uu_codec | uu | Convert the operand using | :meth:`uu.encode` / | |
| 1339 | | | | uuencode | :meth:`uu.decode` | |
| 1340 | +----------------------+------------------+------------------------------+------------------------------+ |
| 1341 | | zlib_codec | zip, zlib | Compress the operand | :meth:`zlib.compress` / | |
| 1342 | | | | using gzip | :meth:`zlib.decompress` | |
| 1343 | +----------------------+------------------+------------------------------+------------------------------+ |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 1344 | |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 1345 | .. [#b64] In addition to :term:`bytes-like objects <bytes-like object>`, |
| 1346 | ``'base64_codec'`` also accepts ASCII-only instances of :class:`str` for |
| 1347 | decoding |
Nick Coghlan | 650e322 | 2013-05-23 20:24:02 +1000 | [diff] [blame] | 1348 | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1349 | .. versionadded:: 3.2 |
| 1350 | Restoration of the binary transforms. |
Nick Coghlan | 650e322 | 2013-05-23 20:24:02 +1000 | [diff] [blame] | 1351 | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1352 | .. versionchanged:: 3.4 |
| 1353 | Restoration of the aliases for the binary transforms. |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 1354 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 1355 | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1356 | .. _text-transforms: |
| 1357 | |
| 1358 | Text Transforms |
| 1359 | ^^^^^^^^^^^^^^^ |
| 1360 | |
| 1361 | The following codec provides a text transform: a :class:`str` to :class:`str` |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1362 | mapping. It is not supported by :meth:`str.encode` (which only produces |
| 1363 | :class:`bytes` output). |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1364 | |
| 1365 | .. tabularcolumns:: |l|l|L| |
| 1366 | |
| 1367 | +--------------------+---------+---------------------------+ |
| 1368 | | Codec | Aliases | Purpose | |
| 1369 | +====================+=========+===========================+ |
| 1370 | | rot_13 | rot13 | Returns the Caesar-cypher | |
| 1371 | | | | encryption of the operand | |
| 1372 | +--------------------+---------+---------------------------+ |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 1373 | |
| 1374 | .. versionadded:: 3.2 |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 1375 | Restoration of the ``rot_13`` text transform. |
| 1376 | |
| 1377 | .. versionchanged:: 3.4 |
| 1378 | Restoration of the ``rot13`` alias. |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 1379 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1380 | |
| 1381 | :mod:`encodings.idna` --- Internationalized Domain Names in Applications |
| 1382 | ------------------------------------------------------------------------ |
| 1383 | |
| 1384 | .. module:: encodings.idna |
| 1385 | :synopsis: Internationalized Domain Names implementation |
| 1386 | .. moduleauthor:: Martin v. Löwis |
| 1387 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1388 | This module implements :rfc:`3490` (Internationalized Domain Names in |
| 1389 | Applications) and :rfc:`3492` (Nameprep: A Stringprep Profile for |
| 1390 | Internationalized Domain Names (IDN)). It builds upon the ``punycode`` encoding |
| 1391 | and :mod:`stringprep`. |
| 1392 | |
| 1393 | These RFCs together define a protocol to support non-ASCII characters in domain |
| 1394 | names. A domain name containing non-ASCII characters (such as |
| 1395 | ``www.Alliancefrançaise.nu``) is converted into an ASCII-compatible encoding |
| 1396 | (ACE, such as ``www.xn--alliancefranaise-npb.nu``). The ACE form of the domain |
| 1397 | name is then used in all places where arbitrary characters are not allowed by |
| 1398 | the protocol, such as DNS queries, HTTP :mailheader:`Host` fields, and so |
| 1399 | on. This conversion is carried out in the application; if possible invisible to |
| 1400 | the user: The application should transparently convert Unicode domain labels to |
| 1401 | IDNA on the wire, and convert back ACE labels to Unicode before presenting them |
| 1402 | to the user. |
| 1403 | |
R David Murray | e0fd2f8 | 2011-04-13 14:12:18 -0400 | [diff] [blame] | 1404 | Python supports this conversion in several ways: the ``idna`` codec performs |
| 1405 | conversion between Unicode and ACE, separating an input string into labels |
| 1406 | based on the separator characters defined in `section 3.1`_ (1) of :rfc:`3490` |
| 1407 | and converting each label to ACE as required, and conversely separating an input |
| 1408 | byte string into labels based on the ``.`` separator and converting any ACE |
| 1409 | labels found into unicode. Furthermore, the :mod:`socket` module |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1410 | transparently converts Unicode host names to ACE, so that applications need not |
| 1411 | be concerned about converting host names themselves when they pass them to the |
| 1412 | 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] | 1413 | parameters, such as :mod:`http.client` and :mod:`ftplib`, accept Unicode host |
| 1414 | 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] | 1415 | :mailheader:`Host` field if it sends that field at all). |
| 1416 | |
R David Murray | e0fd2f8 | 2011-04-13 14:12:18 -0400 | [diff] [blame] | 1417 | .. _section 3.1: http://tools.ietf.org/html/rfc3490#section-3.1 |
| 1418 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1419 | When receiving host names from the wire (such as in reverse name lookup), no |
| 1420 | automatic conversion to Unicode is performed: Applications wishing to present |
| 1421 | such host names to the user should decode them to Unicode. |
| 1422 | |
| 1423 | The module :mod:`encodings.idna` also implements the nameprep procedure, which |
| 1424 | performs certain normalizations on host names, to achieve case-insensitivity of |
| 1425 | international domain names, and to unify similar characters. The nameprep |
| 1426 | functions can be used directly if desired. |
| 1427 | |
| 1428 | |
| 1429 | .. function:: nameprep(label) |
| 1430 | |
| 1431 | Return the nameprepped version of *label*. The implementation currently assumes |
| 1432 | query strings, so ``AllowUnassigned`` is true. |
| 1433 | |
| 1434 | |
| 1435 | .. function:: ToASCII(label) |
| 1436 | |
| 1437 | Convert a label to ASCII, as specified in :rfc:`3490`. ``UseSTD3ASCIIRules`` is |
| 1438 | assumed to be false. |
| 1439 | |
| 1440 | |
| 1441 | .. function:: ToUnicode(label) |
| 1442 | |
| 1443 | Convert a label to Unicode, as specified in :rfc:`3490`. |
| 1444 | |
| 1445 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1446 | :mod:`encodings.mbcs` --- Windows ANSI codepage |
| 1447 | ----------------------------------------------- |
| 1448 | |
| 1449 | .. module:: encodings.mbcs |
| 1450 | :synopsis: Windows ANSI codepage |
| 1451 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 1452 | Encode operand according to the ANSI codepage (CP_ACP). |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1453 | |
| 1454 | Availability: Windows only. |
| 1455 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 1456 | .. versionchanged:: 3.3 |
| 1457 | Support any error handler. |
| 1458 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1459 | .. versionchanged:: 3.2 |
| 1460 | Before 3.2, the *errors* argument was ignored; ``'replace'`` was always used |
| 1461 | to encode, and ``'ignore'`` to decode. |
| 1462 | |
| 1463 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1464 | :mod:`encodings.utf_8_sig` --- UTF-8 codec with BOM signature |
| 1465 | ------------------------------------------------------------- |
| 1466 | |
| 1467 | .. module:: encodings.utf_8_sig |
| 1468 | :synopsis: UTF-8 codec with BOM signature |
| 1469 | .. moduleauthor:: Walter Dörwald |
| 1470 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1471 | This module implements a variant of the UTF-8 codec: On encoding a UTF-8 encoded |
| 1472 | BOM will be prepended to the UTF-8 encoded bytes. For the stateful encoder this |
| 1473 | is only done once (on the first write to the byte stream). For decoding an |
| 1474 | optional UTF-8 encoded BOM at the start of the data will be skipped. |