Skip to content

Commit 3cf0199

Browse files
committed
gh-151416: Document the __fspath__ reentrancy hazard of the FS path C APIs
PyUnicode_FSConverter(), PyUnicode_FSDecoder(), and PyOS_FSPath() call an object's __fspath__() method during conversion, so the caller must protect the input object with a strong reference that this method cannot release. The docs previously described only the result reference semantics and gave no hint that passing a reference borrowed from a mutable container is unsafe.
1 parent 8da22af commit 3cf0199

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎Doc/c-api/sys.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Operating System Utilities
1616
:class:`str` or :class:`bytes` object. Otherwise :exc:`TypeError` is raised
1717
and ``NULL`` is returned.
1818
19+
.. note::
20+
21+
The caller must hold a :term:`strong reference` to *path* that
22+
its :meth:`~os.PathLike.__fspath__` method cannot release.
23+
1924
.. versionadded:: 3.6
2025
2126

‎Doc/c-api/unicode.rst‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,13 @@ conversion function:
925925
If *obj* is ``NULL``, the function releases a strong reference
926926
stored in the variable referred by *result* and returns ``1``.
927927
928+
.. note::
929+
930+
If *obj* is not a :class:`str` or :class:`bytes` instance, the
931+
conversion calls its :meth:`~os.PathLike.__fspath__` method. The
932+
caller must hold a :term:`strong reference` to *obj* that this
933+
method cannot release.
934+
928935
.. versionadded:: 3.1
929936
930937
.. versionchanged:: 3.6
@@ -952,6 +959,13 @@ conversion function:
952959
If *obj* is ``NULL``, release the strong reference
953960
to the object referred to by *result* and return ``1``.
954961
962+
.. note::
963+
964+
If *obj* is not a :class:`str` or :class:`bytes` instance, the
965+
conversion calls its :meth:`~os.PathLike.__fspath__` method. The
966+
caller must hold a :term:`strong reference` to *obj* that this
967+
method cannot release.
968+
955969
.. versionadded:: 3.2
956970
957971
.. versionchanged:: 3.6

0 commit comments

Comments
 (0)