Skip to content

Commit 9e1f164

Browse files
authored
Docs: fill in descriptor C API docs (GH-146644)
1 parent 62a6e89 commit 9e1f164

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

Doc/c-api/descriptor.rst

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,31 @@ Descriptor Objects
88
"Descriptors" are objects that describe some attribute of an object. They are
99
found in the dictionary of type objects.
1010

11-
.. XXX document these!
12-
1311
.. c:function:: PyObject* PyDescr_NewGetSet(PyTypeObject *type, struct PyGetSetDef *getset)
1412
13+
Create a new get-set descriptor for extension type *type* from the
14+
:c:type:`PyGetSetDef` structure *getset*.
15+
16+
Get-set descriptors expose attributes implemented by C getter and setter
17+
functions rather than stored directly in the instance. This is the same kind
18+
of descriptor created for entries in :c:member:`~PyTypeObject.tp_getset`, and
19+
it appears in Python as a :class:`types.GetSetDescriptorType` object.
20+
21+
On success, return a :term:`strong reference` to the descriptor. Return
22+
``NULL`` with an exception set on failure.
23+
24+
.. c:function:: PyObject* PyDescr_NewMember(PyTypeObject *type, struct PyMemberDef *member)
1525
16-
.. c:function:: PyObject* PyDescr_NewMember(PyTypeObject *type, struct PyMemberDef *meth)
26+
Create a new member descriptor for extension type *type* from the
27+
:c:type:`PyMemberDef` structure *member*.
1728
29+
Member descriptors expose fields in the type's C struct as Python
30+
attributes. This is the same kind of descriptor created for entries in
31+
:c:member:`~PyTypeObject.tp_members`, and it appears in Python as a
32+
:class:`types.MemberDescriptorType` object.
33+
34+
On success, return a :term:`strong reference` to the descriptor. Return
35+
``NULL`` with an exception set on failure.
1836
1937
.. c:var:: PyTypeObject PyMemberDescr_Type
2038
@@ -30,22 +48,53 @@ found in the dictionary of type objects.
3048
The type object for get/set descriptor objects created from
3149
:c:type:`PyGetSetDef` structures. These descriptors implement attributes
3250
whose value is computed by C getter and setter functions, and are used
33-
for many built-in type attributes.
51+
for many built-in type attributes. They correspond to
52+
:class:`types.GetSetDescriptorType` objects in Python.
3453
3554
3655
.. c:function:: PyObject* PyDescr_NewMethod(PyTypeObject *type, struct PyMethodDef *meth)
3756
57+
Create a new method descriptor for extension type *type* from the
58+
:c:type:`PyMethodDef` structure *meth*.
59+
60+
Method descriptors expose C functions as methods on a type. This is the same
61+
kind of descriptor created for entries in
62+
:c:member:`~PyTypeObject.tp_methods`, and it appears in Python as a
63+
:class:`types.MethodDescriptorType` object.
64+
65+
On success, return a :term:`strong reference` to the descriptor. Return
66+
``NULL`` with an exception set on failure.
3867
3968
.. c:var:: PyTypeObject PyMethodDescr_Type
4069
4170
The type object for method descriptor objects created from
4271
:c:type:`PyMethodDef` structures. These descriptors expose C functions as
43-
methods on a type, and correspond to :class:`types.MemberDescriptorType`
72+
methods on a type, and correspond to :class:`types.MethodDescriptorType`
4473
objects in Python.
4574
4675
47-
.. c:function:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *wrapper, void *wrapped)
76+
.. c:struct:: wrapperbase
77+
78+
Describes a slot wrapper used by :c:func:`PyDescr_NewWrapper`.
4879
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.
83+
84+
.. c:function:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
85+
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*.
90+
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.
95+
96+
On success, return a :term:`strong reference` to the descriptor. Return
97+
``NULL`` with an exception set on failure.
4998
5099
.. c:var:: PyTypeObject PyWrapperDescr_Type
51100
@@ -58,6 +107,16 @@ found in the dictionary of type objects.
58107
59108
.. c:function:: PyObject* PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
60109
110+
Create a new class method descriptor for extension type *type* from the
111+
:c:type:`PyMethodDef` structure *method*.
112+
113+
Class method descriptors expose C methods that receive the class rather than
114+
an instance when accessed. This is the same kind of descriptor created for
115+
``METH_CLASS`` entries in :c:member:`~PyTypeObject.tp_methods`, and it
116+
appears in Python as a :class:`types.ClassMethodDescriptorType` object.
117+
118+
On success, return a :term:`strong reference` to the descriptor. Return
119+
``NULL`` with an exception set on failure.
61120
62121
.. c:function:: int PyDescr_IsData(PyObject *descr)
63122
@@ -66,8 +125,18 @@ found in the dictionary of type objects.
66125
no error checking.
67126
68127
69-
.. c:function:: PyObject* PyWrapper_New(PyObject *, PyObject *)
128+
.. c:function:: PyObject* PyWrapper_New(PyObject *d, PyObject *self)
129+
130+
Create a new bound wrapper object from the wrapper descriptor *d* and the
131+
instance *self*.
132+
133+
This is the bound form of a wrapper descriptor created by
134+
:c:func:`PyDescr_NewWrapper`. CPython creates these objects when a slot
135+
wrapper is accessed through an instance, and they appear in Python as
136+
:class:`types.MethodWrapperType` objects.
70137
138+
On success, return a :term:`strong reference` to the wrapper object. Return
139+
``NULL`` with an exception set on failure.
71140
72141
.. c:macro:: PyDescr_COMMON
73142
@@ -104,9 +173,9 @@ Built-in descriptors
104173
.. c:var:: PyTypeObject PyClassMethodDescr_Type
105174
106175
The type object for C-level class method descriptor objects.
107-
This is the type of the descriptors created for :func:`classmethod` defined in
108-
C extension types, and is the same object as :class:`classmethod`
109-
in Python.
176+
This is the type of the descriptors created for :func:`classmethod` defined
177+
in C extension types, and corresponds to
178+
:class:`types.ClassMethodDescriptorType` objects in Python.
110179
111180
112181
.. c:function:: PyObject *PyClassMethod_New(PyObject *callable)

Doc/tools/.nitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# as tested on the CI via check-warnings.py in reusable-docs.yml.
33
# Keep lines sorted lexicographically to help avoid merge conflicts.
44

5-
Doc/c-api/descriptor.rst
65
Doc/c-api/init_config.rst
76
Doc/c-api/intro.rst
87
Doc/c-api/stable.rst

0 commit comments

Comments
 (0)