Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e73355

Browse files
committedJun 30, 2020
bpo-41171: Added companion "FromSpec" methods prefixed with "PyMetaType" to go with the "PyType" variants.
1 parent bd4a3f2 commit 5e73355

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed
 

‎Doc/c-api/type.rst

+22-4
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ The following functions and structs are used to create
142142
143143
.. c:function:: PyObject* PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
144144
145-
Creates and returns a heap type object from the *spec*
145+
Equivalent to ``PyMetaType_FromModuleAndSpec(module, &PyType_Type, spec, bases)``.
146+
147+
.. versionadded:: 3.9
148+
149+
.. c:function:: PyObject* PyMetaType_FromModuleAndSpec(PyObject *module, PyTypeObject *meta_type, PyType_Spec *spec, PyObject *bases)
150+
151+
Creates and returns a heap type object from the *spec* whose's metaclass is *meta_type*
146152
(:const:`Py_TPFLAGS_HEAPTYPE`).
147153
148154
If *bases* is a tuple, the created heap type contains all types contained
@@ -157,17 +163,29 @@ The following functions and structs are used to create
157163
158164
This function calls :c:func:`PyType_Ready` on the new type.
159165
160-
.. versionadded:: 3.9
166+
.. versionadded:: 3.10
161167
162168
.. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
163169
164-
Equivalent to ``PyType_FromModuleAndSpec(NULL, spec, bases)``.
170+
Equivalent to ``PyMetaType_FromSpecWithBases(&PyType_Type, spec, bases)``.
165171
166172
.. versionadded:: 3.3
167173
174+
.. c:function:: PyObject* PyMetaType_FromSpecWithBases(PyTypeObject *meta_type, PyType_Spec *spec, PyObject *bases)
175+
176+
Equivalent to ``PyMetaType_FromModuleAndSpec(NULL, meta_type, spec, bases)``.
177+
178+
.. versionadded:: 3.10
179+
168180
.. c:function:: PyObject* PyType_FromSpec(PyType_Spec *spec)
169181
170-
Equivalent to ``PyType_FromSpecWithBases(spec, NULL)``.
182+
Equivalent to ``PyMetaType_FromSpec(&PyType_Type, spec)``.
183+
184+
.. c:function:: PyObject* PyMetaType_FromSpec(PyTypeObject *meta_type, PyType_Spec *spec)
185+
186+
Equivalent to ``PyMetaType_FromSpecWithBases(meta_type, spec, NULL)``
187+
188+
.. versionadded:: 3.10
171189
172190
.. c:type:: PyType_Spec
173191

‎Include/object.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,18 @@ typedef struct{
225225
PyType_Slot *slots; /* terminated by slot==0. */
226226
} PyType_Spec;
227227

228-
PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
228+
#define PyType_FromSpec(spec) PyMetaType_FromSpec(&PyType_Type, spec)
229+
PyAPI_FUNC(PyObject*) PyMetaType_FromSpec(PyTypeObject*, PyType_Spec*);
229230
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
230-
PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
231+
#define PyType_FromSpecWithBases(spec, bases) PyMetaType_FromSpecWithBases(&PyType_Type, spec, bases)
232+
PyAPI_FUNC(PyObject*) PyMetaType_FromSpecWithBases(PyTypeObject*, PyType_Spec*, PyObject*);
231233
#endif
232234
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
233235
PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
234236
#endif
235237
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
236-
PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
238+
#define PyType_FromModuleAndSpec(module, spec, bases) PyMetaType_FromModuleAndSpec(module, &PyType_Type, spec, bases)
239+
PyAPI_FUNC(PyObject*) PyMetaType_FromModuleAndSpec(PyObject*, PyTypeObject*, PyType_Spec*, PyObject*);
237240
PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *);
238241
PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
239242
#endif

‎Objects/typeobject.c

+11-6
Original file line numberDiff line numberDiff line change
@@ -2880,14 +2880,19 @@ static const short slotoffsets[] = {
28802880
};
28812881

28822882
PyObject *
2883-
PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
2883+
PyMetaType_FromSpecWithBases(PyTypeObject *meta_type, PyType_Spec *spec, PyObject *bases)
28842884
{
2885-
return PyType_FromModuleAndSpec(NULL, spec, bases);
2885+
return PyMetaType_FromModuleAndSpec(NULL, meta_type, spec, bases);
28862886
}
28872887

28882888
PyObject *
2889-
PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
2889+
PyMetaType_FromModuleAndSpec(PyObject *module, PyTypeObject *meta_type, PyType_Spec *spec, PyObject *bases)
28902890
{
2891+
if (meta_type == NULL) {
2892+
PyErr_BadArgument();
2893+
return NULL;
2894+
}
2895+
28912896
PyHeapTypeObject *res;
28922897
PyObject *modname;
28932898
PyTypeObject *type, *base;
@@ -2924,7 +2929,7 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
29242929
}
29252930
}
29262931

2927-
res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, nmembers);
2932+
res = (PyHeapTypeObject*)PyType_GenericAlloc(meta_type, nmembers);
29282933
if (res == NULL)
29292934
return NULL;
29302935
res_start = (char*)res;
@@ -3099,9 +3104,9 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
30993104
}
31003105

31013106
PyObject *
3102-
PyType_FromSpec(PyType_Spec *spec)
3107+
PyMetaType_FromSpec(PyTypeObject *meta_type, PyType_Spec *spec)
31033108
{
3104-
return PyType_FromSpecWithBases(spec, NULL);
3109+
return PyMetaType_FromSpecWithBases(meta_type, spec, NULL);
31053110
}
31063111

31073112
void *

‎PC/python3dll.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,8 @@ EXPORT_FUNC(PyTuple_Pack)
541541
EXPORT_FUNC(PyTuple_SetItem)
542542
EXPORT_FUNC(PyTuple_Size)
543543
EXPORT_FUNC(PyType_ClearCache)
544-
EXPORT_FUNC(PyType_FromSpec)
545-
EXPORT_FUNC(PyType_FromSpecWithBases)
544+
EXPORT_FUNC(PyMetaType_FromSpec)
545+
EXPORT_FUNC(PyMetaType_FromSpecWithBases)
546546
EXPORT_FUNC(PyType_GenericAlloc)
547547
EXPORT_FUNC(PyType_GenericNew)
548548
EXPORT_FUNC(PyType_GetFlags)

0 commit comments

Comments
 (0)
Please sign in to comment.