23
23
This module defines base classes for standard Python codecs (encoders and
24
24
decoders) and provides access to the internal Python codec registry, which
25
25
manages the codec and error handling lookup process. Most standard codecs
26
- are :term: `text encodings <text encoding> `, which encode text to bytes,
27
- but there are also codecs provided that encode text to text, and bytes to
28
- bytes. Custom codecs may encode and decode between arbitrary types, but some
29
- module features are restricted to use specifically with
30
- :term: `text encodings <text encoding> `, or with codecs that encode to
26
+ are :term: `text encodings <text encoding> `, which encode text to bytes (and
27
+ decode bytes to text), but there are also codecs provided that encode text to
28
+ text, and bytes to bytes . Custom codecs may encode and decode between arbitrary
29
+ types, but some module features are restricted to be used specifically with
30
+ :term: `text encodings <text encoding> ` or with codecs that encode to
31
31
:class: `bytes `.
32
32
33
33
The module defines the following functions for encoding and decoding with
@@ -297,58 +297,56 @@ codec will handle encoding and decoding errors.
297
297
Error Handlers
298
298
^^^^^^^^^^^^^^
299
299
300
- To simplify and standardize error handling,
301
- codecs may implement different error handling schemes by
302
- accepting the *errors * string argument. The following string values are
303
- defined and implemented by all standard Python codecs:
300
+ To simplify and standardize error handling, codecs may implement different
301
+ error handling schemes by accepting the *errors * string argument:
304
302
305
- .. tabularcolumns :: |l|L|
306
-
307
- +-------------------------+-----------------------------------------------+
308
- | Value | Meaning |
309
- +=========================+===============================================+
310
- | ``'strict' `` | Raise :exc: `UnicodeError ` (or a subclass); |
311
- | | this is the default. Implemented in |
312
- | | :func: `strict_errors `. |
313
- +-------------------------+-----------------------------------------------+
314
- | ``'ignore' `` | Ignore the malformed data and continue |
315
- | | without further notice. Implemented in |
316
- | | :func: `ignore_errors `. |
317
- +-------------------------+-----------------------------------------------+
318
-
319
- The following error handlers are only applicable to
320
- :term: `text encodings <text encoding> `:
303
+ >>> ' German ß, ♬' .encode(encoding = ' ascii' , errors = ' backslashreplace' )
304
+ b'German \\xdf, \\u266c'
305
+ >>> ' German ß, ♬' .encode(encoding = ' ascii' , errors = ' xmlcharrefreplace' )
306
+ b'German ß, ♬'
321
307
322
308
.. index ::
309
+ pair: strict; error handler's name
310
+ pair: ignore; error handler's name
311
+ pair: replace; error handler's name
312
+ pair: backslashreplace; error handler's name
313
+ pair: surrogateescape; error handler's name
323
314
single: ? (question mark); replacement character
324
315
single: \ (backslash); escape sequence
325
316
single: \x ; escape sequence
326
317
single: \u ; escape sequence
327
318
single: \U ; escape sequence
328
- single: \N ; escape sequence
319
+
320
+ The following error handlers can be used with all Python
321
+ :ref: `standard-encodings ` codecs:
322
+
323
+ .. tabularcolumns :: |l|L|
329
324
330
325
+-------------------------+-----------------------------------------------+
331
326
| Value | Meaning |
332
327
+=========================+===============================================+
333
- | ``'replace' `` | Replace with a suitable replacement |
334
- | | marker; Python will use the official |
335
- | | ``U+FFFD `` REPLACEMENT CHARACTER for the |
336
- | | built-in codecs on decoding, and '?' on |
337
- | | encoding. Implemented in |
338
- | | :func: `replace_errors `. |
328
+ | ``'strict' `` | Raise :exc: `UnicodeError ` (or a subclass), |
329
+ | | this is the default. Implemented in |
330
+ | | :func: `strict_errors `. |
339
331
+-------------------------+-----------------------------------------------+
340
- | ``'xmlcharrefreplace' `` | Replace with the appropriate XML character |
341
- | | reference (only for encoding). Implemented |
342
- | | in :func: `xmlcharrefreplace_errors `. |
332
+ | ``'ignore' `` | Ignore the malformed data and continue without|
333
+ | | further notice. Implemented in |
334
+ | | :func: `ignore_errors `. |
335
+ +-------------------------+-----------------------------------------------+
336
+ | ``'replace' `` | Replace with a replacement marker. On |
337
+ | | encoding, use ``? `` (ASCII character). On |
338
+ | | decoding, use ``� `` (U+FFFD, the official |
339
+ | | REPLACEMENT CHARACTER). Implemented in |
340
+ | | :func: `replace_errors `. |
343
341
+-------------------------+-----------------------------------------------+
344
342
| ``'backslashreplace' `` | Replace with backslashed escape sequences. |
343
+ | | On encoding, use hexadecimal form of Unicode |
344
+ | | code point with formats ``\xhh `` ``\uxxxx `` |
345
+ | | ``\Uxxxxxxxx ``. On decoding, use hexadecimal |
346
+ | | form of byte value with format ``\xhh ``. |
345
347
| | Implemented in |
346
348
| | :func: `backslashreplace_errors `. |
347
349
+-------------------------+-----------------------------------------------+
348
- | ``'namereplace' `` | Replace with ``\N{...} `` escape sequences |
349
- | | (only for encoding). Implemented in |
350
- | | :func: `namereplace_errors `. |
351
- +-------------------------+-----------------------------------------------+
352
350
| ``'surrogateescape' `` | On decoding, replace byte with individual |
353
351
| | surrogate code ranging from ``U+DC80 `` to |
354
352
| | ``U+DCFF ``. This code will then be turned |
@@ -358,27 +356,55 @@ The following error handlers are only applicable to
358
356
| | more.) |
359
357
+-------------------------+-----------------------------------------------+
360
358
359
+ .. index ::
360
+ pair: xmlcharrefreplace; error handler's name
361
+ pair: namereplace; error handler's name
362
+ single: \N ; escape sequence
363
+
364
+ The following error handlers are only applicable to encoding (within
365
+ :term: `text encodings <text encoding> `):
366
+
367
+ +-------------------------+-----------------------------------------------+
368
+ | Value | Meaning |
369
+ +=========================+===============================================+
370
+ | ``'xmlcharrefreplace' `` | Replace with XML/HTML numeric character |
371
+ | | reference, which is a decimal form of Unicode |
372
+ | | code point with format ``&#num; `` Implemented |
373
+ | | in :func: `xmlcharrefreplace_errors `. |
374
+ +-------------------------+-----------------------------------------------+
375
+ | ``'namereplace' `` | Replace with ``\N{...} `` escape sequences, |
376
+ | | what appears in the braces is the Name |
377
+ | | property from Unicode Character Database. |
378
+ | | Implemented in :func: `namereplace_errors `. |
379
+ +-------------------------+-----------------------------------------------+
380
+
381
+ .. index ::
382
+ pair: surrogatepass; error handler's name
383
+
361
384
In addition, the following error handler is specific to the given codecs:
362
385
363
386
+-------------------+------------------------+-------------------------------------------+
364
387
| Value | Codecs | Meaning |
365
388
+===================+========================+===========================================+
366
- | ``'surrogatepass'``| utf-8, utf-16, utf-32, | Allow encoding and decoding of surrogate |
367
- | | utf-16-be, utf-16-le, | codes. These codecs normally treat the |
368
- | | utf-32-be, utf-32-le | presence of surrogates as an error. |
389
+ | ``'surrogatepass'``| utf-8, utf-16, utf-32, | Allow encoding and decoding surrogate code|
390
+ | | utf-16-be, utf-16-le, | point (``U+D800 `` - ``U+DFFF ``) as normal |
391
+ | | utf-32-be, utf-32-le | code point. Otherwise these codecs treat |
392
+ | | | the presence of surrogate code point in |
393
+ | | | :class: `str ` as an error. |
369
394
+-------------------+------------------------+-------------------------------------------+
370
395
371
396
.. versionadded :: 3.1
372
397
The ``'surrogateescape' `` and ``'surrogatepass' `` error handlers.
373
398
374
399
.. versionchanged :: 3.4
375
- The ``'surrogatepass' `` error handlers now works with utf-16\* and utf-32\* codecs.
400
+ The ``'surrogatepass' `` error handler now works with utf-16\* and utf-32\*
401
+ codecs.
376
402
377
403
.. versionadded :: 3.5
378
404
The ``'namereplace' `` error handler.
379
405
380
406
.. versionchanged :: 3.5
381
- The ``'backslashreplace' `` error handlers now works with decoding and
407
+ The ``'backslashreplace' `` error handler now works with decoding and
382
408
translating.
383
409
384
410
The set of allowed values can be extended by registering a new named error
@@ -421,42 +447,59 @@ functions:
421
447
422
448
.. function :: strict_errors(exception)
423
449
424
- Implements the ``'strict' `` error handling: each encoding or
425
- decoding error raises a :exc: `UnicodeError `.
450
+ Implements the ``'strict' `` error handling.
426
451
452
+ Each encoding or decoding error raises a :exc: `UnicodeError `.
427
453
428
- .. function :: replace_errors(exception)
429
454
430
- Implements the ``'replace' `` error handling (for :term: `text encodings
431
- <text encoding> ` only): substitutes ``'?' `` for encoding errors
432
- (to be encoded by the codec), and ``'\ufffd' `` (the Unicode replacement
433
- character) for decoding errors.
455
+ .. function :: ignore_errors(exception)
434
456
457
+ Implements the ``'ignore' `` error handling.
435
458
436
- .. function :: ignore_errors(exception)
459
+ Malformed data is ignored; encoding or decoding is continued without
460
+ further notice.
437
461
438
- Implements the ``'ignore' `` error handling: malformed data is ignored and
439
- encoding or decoding is continued without further notice.
440
462
463
+ .. function :: replace_errors(exception)
441
464
442
- .. function :: xmlcharrefreplace_errors(exception)
465
+ Implements the `` 'replace' `` error handling.
443
466
444
- Implements the ``'xmlcharrefreplace' `` error handling (for encoding with
445
- :term: `text encodings <text encoding> ` only): the
446
- unencodable character is replaced by an appropriate XML character reference.
467
+ Substitutes ``? `` (ASCII character) for encoding errors or ``� `` (U+FFFD,
468
+ the official REPLACEMENT CHARACTER) for decoding errors.
447
469
448
470
449
471
.. function :: backslashreplace_errors(exception)
450
472
451
- Implements the ``'backslashreplace' `` error handling (for
452
- :term: `text encodings <text encoding> ` only): malformed data is
453
- replaced by a backslashed escape sequence.
473
+ Implements the ``'backslashreplace' `` error handling.
474
+
475
+ Malformed data is replaced by a backslashed escape sequence.
476
+ On encoding, use the hexadecimal form of Unicode code point with formats
477
+ ``\xhh `` ``\uxxxx `` ``\Uxxxxxxxx ``. On decoding, use the hexadecimal form of
478
+ byte value with format ``\xhh ``.
479
+
480
+ .. versionchanged :: 3.5
481
+ Works with decoding and translating.
482
+
483
+
484
+ .. function :: xmlcharrefreplace_errors(exception)
485
+
486
+ Implements the ``'xmlcharrefreplace' `` error handling (for encoding within
487
+ :term: `text encoding ` only).
488
+
489
+ The unencodable character is replaced by an appropriate XML/HTML numeric
490
+ character reference, which is a decimal form of Unicode code point with
491
+ format ``&#num; `` .
492
+
454
493
455
494
.. function :: namereplace_errors(exception)
456
495
457
- Implements the ``'namereplace' `` error handling (for encoding with
458
- :term: `text encodings <text encoding> ` only): the
459
- unencodable character is replaced by a ``\N{...} `` escape sequence.
496
+ Implements the ``'namereplace' `` error handling (for encoding within
497
+ :term: `text encoding ` only).
498
+
499
+ The unencodable character is replaced by a ``\N{...} `` escape sequence. The
500
+ set of characters that appear in the braces is the Name property from
501
+ Unicode Character Database. For example, the German lowercase letter ``'ß' ``
502
+ will be converted to byte sequence ``\N{LATIN SMALL LETTER SHARP S} `` .
460
503
461
504
.. versionadded :: 3.5
462
505
@@ -470,7 +513,7 @@ The base :class:`Codec` class defines these methods which also define the
470
513
function interfaces of the stateless encoder and decoder:
471
514
472
515
473
- .. method :: Codec.encode(input[ , errors] )
516
+ .. method :: Codec.encode(input, errors='strict' )
474
517
475
518
Encodes the object *input * and returns a tuple (output object, length consumed).
476
519
For instance, :term: `text encoding ` converts
@@ -488,7 +531,7 @@ function interfaces of the stateless encoder and decoder:
488
531
of the output object type in this situation.
489
532
490
533
491
- .. method :: Codec.decode(input[ , errors] )
534
+ .. method :: Codec.decode(input, errors='strict' )
492
535
493
536
Decodes the object *input * and returns a tuple (output object, length
494
537
consumed). For instance, for a :term: `text encoding `, decoding converts
@@ -555,7 +598,7 @@ define in order to be compatible with the Python codec registry.
555
598
object.
556
599
557
600
558
- .. method :: encode(object[ , final] )
601
+ .. method :: encode(object, final=False )
559
602
560
603
Encodes *object * (taking the current state of the encoder into account)
561
604
and returns the resulting encoded object. If this is the last call to
@@ -612,7 +655,7 @@ define in order to be compatible with the Python codec registry.
612
655
object.
613
656
614
657
615
- .. method :: decode(object[ , final] )
658
+ .. method :: decode(object, final=False )
616
659
617
660
Decodes *object * (taking the current state of the decoder into account)
618
661
and returns the resulting decoded object. If this is the last call to
@@ -746,7 +789,7 @@ compatible with the Python codec registry.
746
789
:func: `register_error `.
747
790
748
791
749
- .. method :: read([ size[ , chars, [ firstline]]] )
792
+ .. method :: read(size=-1 , chars=-1, firstline=False )
750
793
751
794
Decodes data from the stream and returns the resulting object.
752
795
@@ -772,7 +815,7 @@ compatible with the Python codec registry.
772
815
available on the stream, these should be read too.
773
816
774
817
775
- .. method :: readline([ size[ , keepends]] )
818
+ .. method :: readline(size=None , keepends=True )
776
819
777
820
Read one line from the input stream and return the decoded data.
778
821
@@ -783,7 +826,7 @@ compatible with the Python codec registry.
783
826
returned.
784
827
785
828
786
- .. method :: readlines([ sizehint[ , keepends]] )
829
+ .. method :: readlines(sizehint=None , keepends=True )
787
830
788
831
Read all lines available on the input stream and return them as a list of
789
832
lines.
@@ -874,7 +917,7 @@ Encodings and Unicode
874
917
---------------------
875
918
876
919
Strings are stored internally as sequences of code points in
877
- range ``0x0 ``--``0x10FFFF ``. (See :pep: `393 ` for
920
+ range ``U+0000 ``--``U+10FFFF ``. (See :pep: `393 ` for
878
921
more details about the implementation.)
879
922
Once a string object is used outside of CPU and memory, endianness
880
923
and how these arrays are stored as bytes become an issue. As with other
@@ -955,7 +998,7 @@ encoding was used for encoding a string. Each charmap encoding can
955
998
decode any random byte sequence. However that's not possible with UTF-8, as
956
999
UTF-8 byte sequences have a structure that doesn't allow arbitrary byte
957
1000
sequences. To increase the reliability with which a UTF-8 encoding can be
958
- detected, Microsoft invented a variant of UTF-8 (that Python 2.5 calls
1001
+ detected, Microsoft invented a variant of UTF-8 (that Python calls
959
1002
``"utf-8-sig" ``) for its Notepad program: Before any of the Unicode characters
960
1003
is written to the file, a UTF-8 encoded BOM (which looks like this as a byte
961
1004
sequence: ``0xef ``, ``0xbb ``, ``0xbf ``) is written. As it's rather improbable
0 commit comments