Skip to content

Commit aed4ad8

Browse files
gh-141004: Document missing descriptor C APIs (GH-159709)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 6839f8c commit aed4ad8

2 files changed

Lines changed: 43 additions & 26 deletions

File tree

‎Doc/c-api/descriptor.rst‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,38 +72,34 @@ found in the dictionary of type objects.
7272
methods on a type, and correspond to :class:`types.MethodDescriptorType`
7373
objects in Python.
7474
75+
.. c:var:: PyTypeObject PyWrapperDescr_Type
7576
76-
.. c:struct:: wrapperbase
77+
The type object for wrapper descriptor objects created by
78+
:c:func:`PyDescr_NewWrapper` and :c:func:`PyWrapper_New`.
7779
78-
Describes a slot wrapper used by :c:func:`PyDescr_NewWrapper`.
80+
Wrapper descriptors are used internally to expose special methods
81+
such as ``__repr__`` or ``__add__``, which are implemented by type slots.
82+
The type is exposed in Python as :class:`types.WrapperDescriptorType`.
7983
80-
Each ``wrapperbase`` record stores the Python-visible name and metadata for a
81-
special method implemented by a type slot, together with the wrapper
82-
function used to adapt that slot to Python's calling convention.
8384
84-
.. c:function:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
85+
.. `wrapperbase` & `PyWrapperFlag_KEYWORDS` show up in search and index,
86+
and link here, but are only referenced in prose:
8587
86-
Create a new wrapper descriptor for extension type *type* from the
87-
:c:struct:`wrapperbase` structure *base* and the wrapped slot function
88-
pointer
89-
*wrapped*.
88+
.. c:struct:: wrapperbase
89+
:no-typesetting:
9090
91-
Wrapper descriptors expose special methods implemented by type slots. This
92-
is the same kind of descriptor that CPython creates for slot-based special
93-
methods such as ``__repr__`` or ``__add__``, and it appears in Python as a
94-
:class:`types.WrapperDescriptorType` object.
91+
.. c:macro:: PyWrapperFlag_KEYWORDS
92+
:no-typesetting:
9593
96-
On success, return a :term:`strong reference` to the descriptor. Return
97-
``NULL`` with an exception set on failure.
94+
.. c:function:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
9895
99-
.. c:var:: PyTypeObject PyWrapperDescr_Type
96+
Create a new wrapper descriptor of type :c:var:`PyWrapperDescr_Type`.
10097
101-
The type object for wrapper descriptor objects created by
102-
:c:func:`PyDescr_NewWrapper` and :c:func:`PyWrapper_New`. Wrapper
103-
descriptors are used internally to expose special methods implemented
104-
via wrapper structures, and appear in Python as
105-
:class:`types.WrapperDescriptorType` objects.
98+
This function is meant for internal use only; it and
99+
related API (``struct wrapperbase`` and the ``PyWrapperFlag_KEYWORDS``
100+
flag) are exposed publicly for backwards compatibility reasons.
106101
102+
See :c:func:`PyCMethod_New` for wrapping a C function.
107103
108104
.. c:function:: PyObject* PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
109105
@@ -138,6 +134,30 @@ found in the dictionary of type objects.
138134
On success, return a :term:`strong reference` to the wrapper object. Return
139135
``NULL`` with an exception set on failure.
140136
137+
.. c:macro:: PyDescr_TYPE(descr)
138+
139+
Return a :term:`borrowed reference` to the type that the descriptor
140+
*descr* belongs to. This is accessible as :attr:`~object.__objclass__`
141+
in Python.
142+
143+
*descr* must be a descriptor object created by one of the
144+
``PyDescr_New*`` functions (an instance of
145+
:c:var:`PyMethodDescr_Type`, :c:var:`PyClassMethodDescr_Type`,
146+
:c:var:`PyMemberDescr_Type`, :c:var:`PyGetSetDescr_Type`, or
147+
:c:var:`PyWrapperDescr_Type`). If the passed descriptor is not
148+
an instance of one of these types, the behavior is undefined.
149+
150+
151+
.. c:macro:: PyDescr_NAME(descr)
152+
153+
Return the name of the descriptor *descr* as a :term:`borrowed reference`
154+
to a :class:`str` object. This is available as :attr:`~definition.__name__`
155+
in Python.
156+
157+
*descr* must be a descriptor object created by one of the ``PyDescr_New*``
158+
functions. If not, the behavior is undefined.
159+
160+
141161
.. c:macro:: PyDescr_COMMON
142162
143163
This is a macro including the common fields for a
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
# cpython/descrobject.h
2-
PyDescr_NAME
3-
PyDescr_TYPE
4-
PyWrapperFlag_KEYWORDS
1+
# Nothing here :)

0 commit comments

Comments
 (0)