summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorPetr Viktorin <encukou@gmail.com>2023-05-03 13:17:14 (GMT)
committerGitHub <noreply@github.com>2023-05-03 13:17:14 (GMT)
commit524a7f77fd8244835e382f076dd4a76404580bb3 (patch)
tree2c4cf20e01075972e4861d8cfc54ec5902af1233 /Objects
parent423d7faeb37b6c13b3ebbf9255165fefc651983e (diff)
downloadcpython-524a7f77fd8244835e382f076dd4a76404580bb3.zip
cpython-524a7f77fd8244835e382f076dd4a76404580bb3.tar.gz
cpython-524a7f77fd8244835e382f076dd4a76404580bb3.tar.bz2
gh-103968: Deprecate creating heap types whose metaclass has custom tp_new. (GH-103972)
(That's a mouthful of an edge case!) Co-authored-by: Barney Gale <barney.gale@gmail.com>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c38
1 files changed, 29 insertions, 9 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index cf0efe1..4ced04b 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -3950,9 +3950,10 @@ check_basicsize_includes_size_and_offsets(PyTypeObject* type)
return 1;
}
-PyObject *
-PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
- PyType_Spec *spec, PyObject *bases_in)
+static PyObject *
+_PyType_FromMetaclass_impl(
+ PyTypeObject *metaclass, PyObject *module,
+ PyType_Spec *spec, PyObject *bases_in, int _allow_tp_new)
{
/* Invariant: A non-NULL value in one of these means this function holds
* a strong reference or owns allocated memory.
@@ -4127,9 +4128,21 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
goto finally;
}
if (metaclass->tp_new != PyType_Type.tp_new) {
- PyErr_SetString(PyExc_TypeError,
- "Metaclasses with custom tp_new are not supported.");
- goto finally;
+ if (_allow_tp_new) {
+ if (PyErr_WarnFormat(
+ PyExc_DeprecationWarning, 1,
+ "Using PyType_Spec with metaclasses that have custom "
+ "tp_new is deprecated and will no longer be allowed in "
+ "Python 3.14.") < 0) {
+ goto finally;
+ }
+ }
+ else {
+ PyErr_SetString(
+ PyExc_TypeError,
+ "Metaclasses with custom tp_new are not supported.");
+ goto finally;
+ }
}
/* Calculate best base, and check that all bases are type objects */
@@ -4317,21 +4330,28 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
}
PyObject *
+PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
+ PyType_Spec *spec, PyObject *bases_in)
+{
+ return _PyType_FromMetaclass_impl(metaclass, module, spec, bases_in, 0);
+}
+
+PyObject *
PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
{
- return PyType_FromMetaclass(NULL, module, spec, bases);
+ return _PyType_FromMetaclass_impl(NULL, module, spec, bases, 1);
}
PyObject *
PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
{
- return PyType_FromMetaclass(NULL, NULL, spec, bases);
+ return _PyType_FromMetaclass_impl(NULL, NULL, spec, bases, 1);
}
PyObject *
PyType_FromSpec(PyType_Spec *spec)
{
- return PyType_FromMetaclass(NULL, NULL, spec, NULL);
+ return _PyType_FromMetaclass_impl(NULL, NULL, spec, NULL, 1);
}
PyObject *