@@ -193,11 +193,12 @@ The following functions and structs are used to create
193
193
.. c:function:: PyObject* PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module, PyType_Spec *spec, PyObject *bases)
194
194
195
195
Create and return a :ref: `heap type <heap-types >` from the *spec *
196
- (:const: `Py_TPFLAGS_HEAPTYPE `).
196
+ (see :const: `Py_TPFLAGS_HEAPTYPE `).
197
197
198
198
The metaclass *metaclass* is used to construct the resulting type object.
199
- When *metaclass* is ``NULL``, the default :c:type:`PyType_Type` is used
200
- instead. Note that metaclasses that override
199
+ When *metaclass* is ``NULL``, the metaclass is derived from *bases*
200
+ (or *Py_tp_base[s] * slots if *bases * is ``NULL ``, see below).
201
+ Note that metaclasses that override
201
202
:c:member:`~PyTypeObject.tp_new` are not supported.
202
203
203
204
The *bases* argument can be used to specify base classes; it can either
@@ -215,6 +216,19 @@ The following functions and structs are used to create
215
216
216
217
This function calls :c:func: `PyType_Ready ` on the new type.
217
218
219
+ Note that this function does *not * fully match the behavior of
220
+ calling :py:class: `type() <type> ` or using the :keyword: `class ` statement.
221
+ With user-provided base types or metaclasses, prefer
222
+ :ref: `calling <capi-call >` :py:class: `type ` (or the metaclass)
223
+ over ``PyType_From*`` functions.
224
+ Specifically:
225
+
226
+ * :py:meth:`~object.__new__` is not called on the new class
227
+ (and it must be set to ``type.__new__ ``).
228
+ * :py:meth:`~object.__init__` is not called on the new class.
229
+ * :py:meth:`~object.__init_subclass__` is not called on any bases.
230
+ * :py:meth:`~object.__set_name__` is not called on new descriptors.
231
+
218
232
.. versionadded:: 3.12
219
233
220
234
.. c:function:: PyObject* PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
@@ -228,17 +242,33 @@ The following functions and structs are used to create
228
242
The function now accepts a single class as the *bases * argument and
229
243
``NULL `` as the ``tp_doc `` slot.
230
244
245
+ .. versionchanged :: 3.12
246
+
247
+ The function now finds and uses a metaclass corresponding to the provided
248
+ base classes. Previously, only :class: `type ` instances were returned.
249
+
231
250
232
251
.. c :function :: PyObject* PyType_FromSpecWithBases (PyType_Spec *spec, PyObject *bases)
233
252
234
253
Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, bases) ``.
235
254
236
255
.. versionadded :: 3.3
237
256
257
+ .. versionchanged :: 3.12
258
+
259
+ The function now finds and uses a metaclass corresponding to the provided
260
+ base classes. Previously, only :class: `type ` instances were returned.
261
+
238
262
.. c :function :: PyObject* PyType_FromSpec (PyType_Spec *spec)
239
263
240
264
Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, NULL) ``.
241
265
266
+ .. versionchanged :: 3.12
267
+
268
+ The function now finds and uses a metaclass corresponding to the
269
+ base classes provided in *Py_tp_base[s] * slots.
270
+ Previously, only :class: `type ` instances were returned.
271
+
242
272
.. c :type :: PyType_Spec
243
273
244
274
Structure defining a type's behavior.
@@ -266,6 +296,8 @@ The following functions and structs are used to create
266
296
Array of :c:type: `PyType_Slot ` structures.
267
297
Terminated by the special slot value ``{0, NULL} ``.
268
298
299
+ Each slot ID should be specified at most once.
300
+
269
301
.. c :type :: PyType_Slot
270
302
271
303
Structure defining optional functionality of a type, containing a slot ID
0 commit comments