Skip to content

Commit e51d358

Browse files
[3.14] gh-141004: Document missing descriptor C APIs (GH-159709) (GH-160137)
(cherry picked from commit aed4ad8) Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent ed5bcda commit e51d358

2 files changed

Lines changed: 42 additions & 25 deletions

File tree

‎Doc/c-api/descriptor.rst‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,38 +77,34 @@ found in the dictionary of type objects.
7777
methods on a type, and correspond to :class:`types.MethodDescriptorType`
7878
objects in Python.
7979
80+
.. c:var:: PyTypeObject PyWrapperDescr_Type
8081
81-
.. c:struct:: wrapperbase
82+
The type object for wrapper descriptor objects created by
83+
:c:func:`PyDescr_NewWrapper` and :c:func:`PyWrapper_New`.
8284
83-
Describes a slot wrapper used by :c:func:`PyDescr_NewWrapper`.
85+
Wrapper descriptors are used internally to expose special methods
86+
such as ``__repr__`` or ``__add__``, which are implemented by type slots.
87+
The type is exposed in Python as :class:`types.WrapperDescriptorType`.
8488
85-
Each ``wrapperbase`` record stores the Python-visible name and metadata for a
86-
special method implemented by a type slot, together with the wrapper
87-
function used to adapt that slot to Python's calling convention.
8889
89-
.. c:function:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
90+
.. `wrapperbase` & `PyWrapperFlag_KEYWORDS` show up in search and index,
91+
and link here, but are only referenced in prose:
9092
91-
Create a new wrapper descriptor for extension type *type* from the
92-
:c:struct:`wrapperbase` structure *base* and the wrapped slot function
93-
pointer
94-
*wrapped*.
93+
.. c:struct:: wrapperbase
94+
:no-typesetting:
9595
96-
Wrapper descriptors expose special methods implemented by type slots. This
97-
is the same kind of descriptor that CPython creates for slot-based special
98-
methods such as ``__repr__`` or ``__add__``, and it appears in Python as a
99-
:class:`types.WrapperDescriptorType` object.
96+
.. c:macro:: PyWrapperFlag_KEYWORDS
97+
:no-typesetting:
10098
101-
On success, return a :term:`strong reference` to the descriptor. Return
102-
``NULL`` with an exception set on failure.
99+
.. c:function:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
103100
104-
.. c:var:: PyTypeObject PyWrapperDescr_Type
101+
Create a new wrapper descriptor of type :c:var:`PyWrapperDescr_Type`.
105102
106-
The type object for wrapper descriptor objects created by
107-
:c:func:`PyDescr_NewWrapper` and :c:func:`PyWrapper_New`. Wrapper
108-
descriptors are used internally to expose special methods implemented
109-
via wrapper structures, and appear in Python as
110-
:class:`types.WrapperDescriptorType` objects.
103+
This function is meant for internal use only; it and
104+
related API (``struct wrapperbase`` and the ``PyWrapperFlag_KEYWORDS``
105+
flag) are exposed publicly for backwards compatibility reasons.
111106
107+
See :c:func:`PyCMethod_New` for wrapping a C function.
112108
113109
.. c:function:: PyObject* PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
114110
@@ -143,6 +139,30 @@ found in the dictionary of type objects.
143139
On success, return a :term:`strong reference` to the wrapper object. Return
144140
``NULL`` with an exception set on failure.
145141
142+
.. c:macro:: PyDescr_TYPE(descr)
143+
144+
Return a :term:`borrowed reference` to the type that the descriptor
145+
*descr* belongs to. This is accessible as :attr:`~object.__objclass__`
146+
in Python.
147+
148+
*descr* must be a descriptor object created by one of the
149+
``PyDescr_New*`` functions (an instance of
150+
:c:var:`PyMethodDescr_Type`, :c:var:`PyClassMethodDescr_Type`,
151+
:c:var:`PyMemberDescr_Type`, :c:var:`PyGetSetDescr_Type`, or
152+
:c:var:`PyWrapperDescr_Type`). If the passed descriptor is not
153+
an instance of one of these types, the behavior is undefined.
154+
155+
156+
.. c:macro:: PyDescr_NAME(descr)
157+
158+
Return the name of the descriptor *descr* as a :term:`borrowed reference`
159+
to a :class:`str` object. This is available as :attr:`~definition.__name__`
160+
in Python.
161+
162+
*descr* must be a descriptor object created by one of the ``PyDescr_New*``
163+
functions. If not, the behavior is undefined.
164+
165+
146166
Built-in descriptors
147167
^^^^^^^^^^^^^^^^^^^^
148168

‎Tools/check-c-api-docs/ignored_c_api.txt‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ PY_VERSION
2828
Py_UNICODE_SIZE
2929
# cpython/descrobject.h
3030
PyDescr_COMMON
31-
PyDescr_NAME
32-
PyDescr_TYPE
33-
PyWrapperFlag_KEYWORDS
3431
# 3.14 only
3532
Py_TPFLAGS_PREHEADER
3633
PyUnicode_AsDecodedObject

0 commit comments

Comments
 (0)