Skip to content

Commit 8788a95

Browse files
miss-islingtonserhiy-storchakaStanFromIrelanderlend-aasland
authored
[3.12] gh-90241: Clarify documentation for PyUnicode_FSConverter and PyUnicode_FSDecoder (GH-128451) (GH-128543)
(cherry picked from commit 657d7b7) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Stan Ulbrych <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 498da39 commit 8788a95

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

Doc/c-api/arg.rst

+9-3
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ Other objects
319319

320320
.. _o_ampersand:
321321

322-
``O&`` (object) [*converter*, *anything*]
322+
``O&`` (object) [*converter*, *address*]
323323
Convert a Python object to a C variable through a *converter* function. This
324324
takes two arguments: the first is a function, the second is the address of a C
325325
variable (of arbitrary type), converted to :c:expr:`void *`. The *converter*
@@ -333,14 +333,20 @@ Other objects
333333
the conversion has failed. When the conversion fails, the *converter* function
334334
should raise an exception and leave the content of *address* unmodified.
335335

336-
If the *converter* returns ``Py_CLEANUP_SUPPORTED``, it may get called a
336+
.. c:macro:: Py_CLEANUP_SUPPORTED
337+
:no-typesetting:
338+
339+
If the *converter* returns :c:macro:`!Py_CLEANUP_SUPPORTED`, it may get called a
337340
second time if the argument parsing eventually fails, giving the converter a
338341
chance to release any memory that it had already allocated. In this second
339342
call, the *object* parameter will be ``NULL``; *address* will have the same value
340343
as in the original call.
341344

345+
Examples of converters: :c:func:`PyUnicode_FSConverter` and
346+
:c:func:`PyUnicode_FSDecoder`.
347+
342348
.. versionchanged:: 3.1
343-
``Py_CLEANUP_SUPPORTED`` was added.
349+
:c:macro:`!Py_CLEANUP_SUPPORTED` was added.
344350

345351
``p`` (:class:`bool`) [int]
346352
Tests the value passed in for truth (a boolean **p**\ redicate) and converts

Doc/c-api/unicode.rst

+27-8
Original file line numberDiff line numberDiff line change
@@ -770,33 +770,52 @@ Functions encoding to and decoding from the :term:`filesystem encoding and
770770
error handler` (:pep:`383` and :pep:`529`).
771771
772772
To encode file names to :class:`bytes` during argument parsing, the ``"O&"``
773-
converter should be used, passing :c:func:`PyUnicode_FSConverter` as the
773+
converter should be used, passing :c:func:`!PyUnicode_FSConverter` as the
774774
conversion function:
775775
776776
.. c:function:: int PyUnicode_FSConverter(PyObject* obj, void* result)
777777
778-
ParseTuple converter: encode :class:`str` objects -- obtained directly or
778+
:ref:`PyArg_Parse\* converter <arg-parsing>`: encode :class:`str` objects -- obtained directly or
779779
through the :class:`os.PathLike` interface -- to :class:`bytes` using
780780
:c:func:`PyUnicode_EncodeFSDefault`; :class:`bytes` objects are output as-is.
781-
*result* must be a :c:expr:`PyBytesObject*` which must be released when it is
782-
no longer used.
781+
*result* must be an address of a C variable of type :c:expr:`PyObject*`
782+
(or :c:expr:`PyBytesObject*`).
783+
On success, set the variable to a new :term:`strong reference` to
784+
a :ref:`bytes object <bytesobjects>` which must be released
785+
when it is no longer used and return a non-zero value
786+
(:c:macro:`Py_CLEANUP_SUPPORTED`).
787+
Embedded null bytes are not allowed in the result.
788+
On failure, return ``0`` with an exception set.
789+
790+
If *obj* is ``NULL``, the function releases a strong reference
791+
stored in the variable referred by *result* and returns ``1``.
783792
784793
.. versionadded:: 3.1
785794
786795
.. versionchanged:: 3.6
787796
Accepts a :term:`path-like object`.
788797
789798
To decode file names to :class:`str` during argument parsing, the ``"O&"``
790-
converter should be used, passing :c:func:`PyUnicode_FSDecoder` as the
799+
converter should be used, passing :c:func:`!PyUnicode_FSDecoder` as the
791800
conversion function:
792801
793802
.. c:function:: int PyUnicode_FSDecoder(PyObject* obj, void* result)
794803
795-
ParseTuple converter: decode :class:`bytes` objects -- obtained either
804+
:ref:`PyArg_Parse\* converter <arg-parsing>`: decode :class:`bytes` objects -- obtained either
796805
directly or indirectly through the :class:`os.PathLike` interface -- to
797806
:class:`str` using :c:func:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str`
798-
objects are output as-is. *result* must be a :c:expr:`PyUnicodeObject*` which
799-
must be released when it is no longer used.
807+
objects are output as-is.
808+
*result* must be an address of a C variable of type :c:expr:`PyObject*`
809+
(or :c:expr:`PyUnicodeObject*`).
810+
On success, set the variable to a new :term:`strong reference` to
811+
a :ref:`Unicode object <unicodeobjects>` which must be released
812+
when it is no longer used and return a non-zero value
813+
(:c:macro:`Py_CLEANUP_SUPPORTED`).
814+
Embedded null characters are not allowed in the result.
815+
On failure, return ``0`` with an exception set.
816+
817+
If *obj* is ``NULL``, release the strong reference
818+
to the object referred to by *result* and return ``1``.
800819
801820
.. versionadded:: 3.2
802821

0 commit comments

Comments
 (0)