Skip to content

Commit b9bc179

Browse files
committed
gh-156098: Document the str form of PyUnicode_DecodeCharmap()'s mapping argument
1 parent 0023d5b commit b9bc179

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

Doc/c-api/unicode.rst

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,19 +1410,30 @@ This codec is special in that it can be used to implement many different codecs
14101410
included in the :mod:`!encodings` package). The codec uses mappings to encode and
14111411
decode characters. The mapping objects provided must support the
14121412
:meth:`~object.__getitem__` mapping interface; dictionaries and sequences work well.
1413+
For decoding, a :class:`str` can also be used directly as a lookup table; see
1414+
:c:func:`PyUnicode_DecodeCharmap`.
14131415
14141416
These are the mapping codec APIs:
14151417
14161418
.. c:function:: PyObject* PyUnicode_DecodeCharmap(const char *str, Py_ssize_t length, \
14171419
PyObject *mapping, const char *errors)
14181420
1419-
Create a Unicode object by decoding *size* bytes of the encoded string *str*
1420-
using the given *mapping* object. Return ``NULL`` if an exception was raised
1421-
by the codec.
1421+
Create a Unicode object by decoding *length* bytes of the encoded string
1422+
*str* using the given *mapping* object. Return ``NULL`` if an exception was
1423+
raised by the codec.
1424+
1425+
If *mapping* is ``NULL``, Latin-1 decoding will be applied.
1426+
1427+
If *mapping* is an exact :class:`str` object (not an instance of a
1428+
subclass), it is used as a decoding table: the byte with ordinal *b* is
1429+
decoded to the character ``mapping[b]``. Bytes whose ordinal is greater
1430+
than or equal to ``len(mapping)``, as well as bytes which map to
1431+
``'\ufffe'``, are treated as undefined mappings and cause an error. This
1432+
is the form used by the ``decoding_table`` of the modules in the
1433+
:mod:`!encodings` package.
14221434
1423-
If *mapping* is ``NULL``, Latin-1 decoding will be applied. Else
1424-
*mapping* must map bytes ordinals (integers in the range from 0 to 255)
1425-
to Unicode strings, integers (which are then interpreted as Unicode
1435+
Otherwise, *mapping* must map bytes ordinals (integers in the range from 0
1436+
to 255) to Unicode strings, integers (which are then interpreted as Unicode
14261437
ordinals) or ``None``. Unmapped data bytes -- ones which cause a
14271438
:exc:`LookupError`, as well as ones which get mapped to ``None``,
14281439
``0xFFFE`` or ``'\ufffe'``, are treated as undefined mappings and cause

0 commit comments

Comments
 (0)