8000 bpo-34284: NullNew for old extension types · python/cpython@06f3144 · GitHub
[go: up one dir, main page]

Skip to content

Commit 06f3144

Browse files
committed
bpo-34284: NullNew for old extension types
1 parent 8f7bb10 commit 06f3144

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

Include/object.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
499499
PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
500500
PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
501501
PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
502-
PyObject *, PyObject *);
502+
PyObject *, PyObject *);
503+
PyAPI_FUNC(PyObject *) PyType_NullNew(PyTypeObject *,
504+
PyObject *, PyObject *);
503505
#ifndef Py_LIMITED_API
504506
PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
505507
PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NullNew for old extension types instead of just null

Objects/typeobject.c

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,15 @@ PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
995995
return type->tp_alloc(type, 0);
996996
}
997997

998+
PyObject *
999+
PyType_NullNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
1000+
{
1001+
PyErr_Format(PyExc_TypeError,
1002+
"cannot create '%.100s' instances",
1003+
type->tp_name);
1004+
return NULL;
1005+
}
1006+
9981007
/* Helpers for subtyping */
9991008

10001009
static int
@@ -2270,6 +2279,10 @@ valid_identifier(PyObject *s)
22702279
static int
22712280
object_init(PyObject *self, PyObject *args, PyObject *kwds);
22722281

2282+
/* Forward */
2283+
static int
2284+
add_tp_new_wrapper(PyTypeObject *type);
2285+
22732286
static int
22742287
type_init(PyObject *cls, PyObject *args, PyObject *kwds)
22752288
{
@@ -4842,7 +4855,17 @@ add_getset(PyTypeObject *type, PyGetSetDef *gsp)
48424855
return 0;
48434856
}
48444857

4845-
static void
4858+
static int
4859+
set_null_new(PyTypeObject *type)
4860+
{
4861+
type->tp_new = PyType_NullNew;
4862+
if (add_tp_new_wrapper(type) < 0)
4863+
return -1;
4864+
4865+
return 0;
4866+
}
4867+
4868+
static int
48464869
inherit_special(PyTypeObject *type, PyTypeObject *base)
48474870
{
48484871

@@ -4867,10 +4890,15 @@ inherit_special(PyTypeObject *type, PyTypeObject *base)
48674890
inherit tp_new; static extension types that specify some
48684891
other built-in type as the default also
48694892
inherit object.__new__. */
4870-
if (base != &PyBaseObject_Type ||
4871-
(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
4872-
if (type->tp_new == NULL)
4873-
type->tp_new = base->tp_new;
4893+
if (type->tp_new == NULL) {
4894+
if (base != &PyBaseObject_Type ||
4895+
(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
4896+
type->tp_new = base->tp_new;
4897+
}
4898+
else {
4899+
if (set_null_new(type) < 0)
4900+
return -1;
4901+
}
48744902
}
48754903
}
48764904
if (type->tp_basicsize == 0)
@@ -4903,6 +4931,8 @@ inherit_special(PyTypeObject *type, PyTypeObject *base)
49034931
type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
49044932
else if (PyType_IsSubtype(base, &PyDict_Type))
49054933
type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
4934+
4935+
return 0;
49064936
}
49074937

49084938
static int
@@ -5193,7 +5223,8 @@ PyType_Ready(PyTypeObject *type)
51935223

51945224
/* Inherit special flags from dominant base */
51955225
if (type->tp_base != NULL)
5196-
inherit_special(type, type->tp_base);
5226+
if (inherit_special(type, type->tp_base) < 0)
5227+
goto error;
51975228

51985229
/* Initialize tp_dict properly */
51995230
bases = type->tp_mro;

0 commit comments

Comments
 (0)
0