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