Skip to content

Commit ae30a81

Browse files
committed
Closes #13833: document PyStructSequence C-API functions.
1 parent c96ef1f commit ae30a81

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Doc/c-api/tuple.rst

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,103 @@ Tuple Objects
108108
.. c:function:: int PyTuple_ClearFreeList()
109109
110110
Clear the free list. Return the total number of freed items.
111+
112+
113+
Struct Sequence Objects
114+
-----------------------
115+
116+
Struct sequence objects are the C equivalent of :func:`~collections.namedtuple`
117+
objects, i.e. a sequence whose items can also be accessed through attributes.
118+
To create a struct sequence, you first have to create a specific struct sequence
119+
type.
120+
121+
.. c:function:: PyTypeObject* PyStructSequence_NewType(PyStructSequence_Desc *desc)
122+
123+
Create a new struct sequence type from the data in *desc*, described below. Instances
124+
of the resulting type can be created with :c:func:`PyStructSequence_New`.
125+
126+
127+
.. c:function:: void PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
128+
129+
Initializes a struct sequence type *type* from *desc* in place.
130+
131+
132+
.. c:type:: PyStructSequence_Desc
133+
134+
Contains the meta information of a struct sequence type to create.
135+
136+
+-------------------+------------------------------+------------------------------------+
137+
| Field | C Type | Meaning |
138+
+===================+==============================+====================================+
139+
| ``name`` | ``char *`` | name of the struct sequence type |
140+
+-------------------+------------------------------+------------------------------------+
141+
| ``doc`` | ``char *`` | pointer to docstring for the type |
142+
| | | or NULL to omit |
143+
+-------------------+------------------------------+------------------------------------+
144+
| ``fields`` | ``PyStructSequence_Field *`` | pointer to *NULL*-terminated array |
145+
| | | with field names of the new type |
146+
+-------------------+------------------------------+------------------------------------+
147+
| ``n_in_sequence`` | ``int`` | number of fields visible to the |
148+
| | | Python side (if used as tuple) |
149+
+-------------------+------------------------------+------------------------------------+
150+
151+
152+
.. c:type:: PyStructSequence_Field
153+
154+
Describes a field of a struct sequence. As a struct sequence is modeled as a
155+
tuple, all fields are typed as :c:type:`PyObject\*`. The index in the
156+
:attr:`fields` array of the :c:type:`PyStructSequence_Desc` determines which
157+
field of the struct sequence is described.
158+
159+
+-----------+---------------+--------------------------------------+
160+
| Field | C Type | Meaning |
161+
+===========+===============+======================================+
162+
| ``name`` | ``char *`` | name for the field or *NULL* to end |
163+
| | | the list of named fields, set to |
164+
| | | PyStructSequence_UnnamedField to |
165+
| | | leave unnamed |
166+
+-----------+---------------+--------------------------------------+
167+
| ``doc`` | ``char *`` | field docstring or *NULL* to omit |
168+
+-----------+---------------+--------------------------------------+
169+
170+
171+
.. c:var:: char* PyStructSequence_UnnamedField
172+
173+
Special value for a field name to leave it unnamed.
174+
175+
176+
.. c:function:: PyObject* PyStructSequence_New(PyTypeObject *type)
177+
178+
Creates an instance of *type*, which must have been created with
179+
:c:func:`PyStructSequence_NewType`.
180+
181+
182+
.. c:function:: PyObject* PyStructSequence_GetItem(PyObject *p, Py_ssize_t pos)
183+
184+
Return the object at position *pos* in the struct sequence pointed to by *p*.
185+
No bounds checking is performed.
186+
187+
188+
.. c:function:: PyObject* PyStructSequence_GET_ITEM(PyObject *p, Py_ssize_t pos)
189+
190+
Macro equivalent of :c:func:`PyStructSequence_GetItem`.
191+
192+
193+
.. c:function:: void PyStructSequence_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
194+
195+
Sets the field at index *pos* of the struct sequence *p* to value *o*. Like
196+
:c:func:`PyTuple_SET_ITEM`, this should only be used to fill in brand new
197+
instances.
198+
199+
.. note::
200+
201+
This function "steals" a reference to *o*.
202+
203+
204+
.. c:function:: PyObject* PyStructSequence_SET_ITEM(PyObject *p, Py_ssize_t *pos, PyObject *o)
205+
206+
Macro equivalent of :c:func:`PyStructSequence_SetItem`.
207+
208+
.. note::
209+
210+
This function "steals" a reference to *o*.

0 commit comments

Comments
 (0)