@@ -995,6 +995,15 @@ PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
995
995
return type -> tp_alloc (type , 0 );
996
996
}
997
997
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
+
998
1007
/* Helpers for subtyping */
999
1008
1000
1009
static int
@@ -2270,6 +2279,10 @@ valid_identifier(PyObject *s)
2270
2279
static int
2271
2280
object_init (PyObject * self , PyObject * args , PyObject * kwds );
2272
2281
2282
+ /* Forward */
2283
+ static int
2284
+ add_tp_new_wrapper (PyTypeObject * type );
2285
+
2273
2286
static int
2274
2287
type_init (PyObject * cls , PyObject * args , PyObject * kwds )
2275
2288
{
@@ -4842,7 +4855,17 @@ add_getset(PyTypeObject *type, PyGetSetDef *gsp)
4842
4855
return 0 ;
4843
4856
}
4844
4857
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
4846
4869
inherit_special (PyTypeObject * type , PyTypeObject * base )
4847
4870
{
4848
4871
@@ -4867,10 +4890,15 @@ inherit_special(PyTypeObject *type, PyTypeObject *base)
4867
4890
inherit tp_new; static extension types that specify some
4868
4891
other built-in type as the default also
4869
4892
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
+ }
4874
4902
}
4875
4903
}
4876
4904
if (type -> tp_basicsize == 0 )
@@ -4903,6 +4931,8 @@ inherit_special(PyTypeObject *type, PyTypeObject *base)
4903
4931
type -> tp_flags |= Py_TPFLAGS_LIST_SUBCLASS ;
4904
4932
else if (PyType_IsSubtype (base , & PyDict_Type ))
4905
4933
type -> tp_flags |= Py_TPFLAGS_DICT_SUBCLASS ;
4934
+
4935
+ return 0 ;
4906
4936
}
4907
4937
4908
4938
static int
@@ -5193,7 +5223,8 @@ PyType_Ready(PyTypeObject *type)
5193
5223
5194
5224
/* Inherit special flags from dominant base */
5195
5225
if (type -> tp_base != NULL )
5196
- inherit_special (type , type -> tp_base );
5226
+ if (inherit_special (type , type -> tp_base ) < 0 )
5227
+ goto error ;
5197
5228
5198
5229
/* Initialize tp_dict properly */
5199
5230
bases = type -> tp_mro ;
0 commit comments