From 66ba41b72971ded1f95f875fb3c6c34f57302715 Mon Sep 17 00:00:00 2001 From: Christopher Chavez Date: Tue, 5 Dec 2023 04:40:09 -0600 Subject: [PATCH 1/9] Do not cast _Py_HashPointer to hashfunc --- Objects/typeobject.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index aa00e04ad5e11b..b78d3ce210a85f 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -6555,6 +6555,12 @@ PyDoc_STRVAR(object_doc, "When called, it accepts no arguments and returns a new featureless\n" "instance that has no instance attributes and cannot be given any.\n"); +static Py_hash_t +object_hash(PyObject *obj) +{ + return _Py_HashPointer(obj); +} + PyTypeObject PyBaseObject_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "object", /* tp_name */ @@ -6569,7 +6575,7 @@ PyTypeObject PyBaseObject_Type = { 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ - (hashfunc)_Py_HashPointer, /* tp_hash */ + object_hash, /* tp_hash */ 0, /* tp_call */ object_str, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ From f98aee34a799e4ac1464fe8ec76ae92675df496e Mon Sep 17 00:00:00 2001 From: Christopher Chavez Date: Tue, 5 Dec 2023 04:44:20 -0600 Subject: [PATCH 2/9] Make type_repr() compatible with reprfunc --- Objects/typeobject.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index b78d3ce210a85f..dacd35e0f426e5 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1597,8 +1597,9 @@ static PyGetSetDef type_getsets[] = { }; static PyObject * -type_repr(PyTypeObject *type) +type_repr(PyObject *self) { + PyTypeObject *type = (PyTypeObject *)self; if (type->tp_name == NULL) { // type_repr() called before the type is fully initialized // by PyType_Ready(). @@ -5354,7 +5355,7 @@ PyTypeObject PyType_Type = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_as_async */ - (reprfunc)type_repr, /* tp_repr */ + type_repr, /* tp_repr */ &type_as_number, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ From c48671f22c18f5be05b514ec301f7b7301d3211a Mon Sep 17 00:00:00 2001 From: Christopher Chavez Date: Tue, 5 Dec 2023 04:46:38 -0600 Subject: [PATCH 3/9] Make type_dealloc() compatible with destructor --- Objects/typeobject.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index dacd35e0f426e5..b4e27abc7b85e8 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5070,8 +5070,10 @@ _PyStaticType_Dealloc(PyInterpreterState *interp, PyTypeObject *type) static void -type_dealloc(PyTypeObject *type) +type_dealloc(PyObject *self) { + PyTypeObject *type = (PyTypeObject *)self; + // Assert this is a heap-allocated type object _PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE); @@ -5350,7 +5352,7 @@ PyTypeObject PyType_Type = { "type", /* tp_name */ sizeof(PyHeapTypeObject), /* tp_basicsize */ sizeof(PyMemberDef), /* tp_itemsize */ - (destructor)type_dealloc, /* tp_dealloc */ + type_dealloc, /* tp_dealloc */ offsetof(PyTypeObject, tp_vectorcall), /* tp_vectorcall_offset */ 0, /* tp_getattr */ 0, /* tp_setattr */ From e32433ce2293be671390e7f19c0af8221dba7928 Mon Sep 17 00:00:00 2001 From: Christopher Chavez Date: Tue, 5 Dec 2023 04:49:06 -0600 Subject: [PATCH 4/9] Make type_call() compatible with ternaryfunc --- Objects/typeobject.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index b4e27abc7b85e8..5c389f3c4811fe 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1631,8 +1631,9 @@ type_repr(PyObject *self) } static PyObject * -type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) +type_call(PyObject *self, PyObject *args, PyObject *kwds) { + PyTypeObject *type = (PyTypeObject *)self; PyObject *obj; PyThreadState *tstate = _PyThreadState_GET(); @@ -5362,7 +5363,7 @@ PyTypeObject PyType_Type = { 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ - (ternaryfunc)type_call, /* tp_call */ + type_call, /* tp_call */ 0, /* tp_str */ (getattrofunc)_Py_type_getattro, /* tp_getattro */ (setattrofunc)type_setattro, /* tp_setattro */ From d4da68eb3e06dc19390b2a9d11966c3d31cbcff8 Mon Sep 17 00:00:00 2001 From: Christopher Chavez Date: Tue, 5 Dec 2023 04:53:43 -0600 Subject: [PATCH 5/9] Make type_traverse() compatible with traverseproc --- Objects/typeobject.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 5c389f3c4811fe..576911f4982ad3 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5261,8 +5261,10 @@ PyDoc_STRVAR(type_doc, "type(name, bases, dict, **kwds) -> a new type"); static int -type_traverse(PyTypeObject *type, visitproc visit, void *arg) +type_traverse(PyObject *self, visitproc visit, void *arg) { + PyTypeObject *type = (PyTypeObject *)self; + /* Because of type_is_gc(), the collector only calls this for heaptypes. */ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { @@ -5373,7 +5375,7 @@ PyTypeObject PyType_Type = { Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_ITEMS_AT_END, /* tp_flags */ type_doc, /* tp_doc */ - (traverseproc)type_traverse, /* tp_traverse */ + type_traverse, /* tp_traverse */ (inquiry)type_clear, /* tp_clear */ 0, /* tp_richcompare */ offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ From a8b30d91fba3dc086c20fdc563483cc53d3ce09c Mon Sep 17 00:00:00 2001 From: Christopher Chavez Date: Tue, 5 Dec 2023 04:59:06 -0600 Subject: [PATCH 6/9] Make type_clear() compatible with inquiry --- Objects/typeobject.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 576911f4982ad3..e9ffdb9155f23b 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5292,8 +5292,10 @@ type_traverse(PyObject *self, visitproc visit, void *arg) } static int -type_clear(PyTypeObject *type) +type_clear(PyObject *self) { + PyTypeObject *type = (PyTypeObject *)self; + /* Because of type_is_gc(), the collector only calls this for heaptypes. */ _PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE); @@ -5376,7 +5378,7 @@ PyTypeObject PyType_Type = { Py_TPFLAGS_ITEMS_AT_END, /* tp_flags */ type_doc, /* tp_doc */ type_traverse, /* tp_traverse */ - (inquiry)type_clear, /* tp_clear */ + type_clear, /* tp_clear */ 0, /* tp_richcompare */ offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ 0, /* tp_iter */ From c3f7da2723a242512ec402aaf8f43bb7d27e98d5 Mon Sep 17 00:00:00 2001 From: Christopher Chavez Date: Tue, 5 Dec 2023 05:00:58 -0600 Subject: [PATCH 7/9] Make type_is_gc() compatible with inquiry --- Objects/typeobject.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index e9ffdb9155f23b..6e763be5383195 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5342,9 +5342,9 @@ type_clear(PyObject *self) } static int -type_is_gc(PyTypeObject *type) +type_is_gc(PyObject *type) { - return type->tp_flags & Py_TPFLAGS_HEAPTYPE; + return ((PyTypeObject *)type)->tp_flags & Py_TPFLAGS_HEAPTYPE; } @@ -5395,7 +5395,7 @@ PyTypeObject PyType_Type = { 0, /* tp_alloc */ type_new, /* tp_new */ PyObject_GC_Del, /* tp_free */ - (inquiry)type_is_gc, /* tp_is_gc */ + type_is_gc, /* tp_is_gc */ .tp_vectorcall = type_vectorcall, }; From f68ce968ebf0a7befcb8f9fc206da7c5bb3d2527 Mon Sep 17 00:00:00 2001 From: Christopher Chavez Date: Tue, 5 Dec 2023 05:05:52 -0600 Subject: [PATCH 8/9] Make type_setattro() compatible with setattrofunc --- Objects/typeobject.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 6e763be5383195..86799a7547fc9a 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4925,8 +4925,9 @@ _Py_type_getattro(PyTypeObject *type, PyObject *name) } static int -type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) +type_setattro(PyObject *self, PyObject *name, PyObject *value) { + PyTypeObject *type = (PyTypeObject *)self; int res; if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) { PyErr_Format( @@ -5370,7 +5371,7 @@ PyTypeObject PyType_Type = { type_call, /* tp_call */ 0, /* tp_str */ (getattrofunc)_Py_type_getattro, /* tp_getattro */ - (setattrofunc)type_setattro, /* tp_setattro */ + type_setattro, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS | From f3eb7569e1c888afe2aa388217d35a194c004d68 Mon Sep 17 00:00:00 2001 From: Christopher Chavez Date: Tue, 5 Dec 2023 05:11:38 -0600 Subject: [PATCH 9/9] Make _Py_type_getattro() compatible with getattrofunc --- Include/internal/pycore_typeobject.h | 2 +- Objects/object.c | 2 +- Objects/typeobject.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Include/internal/pycore_typeobject.h b/Include/internal/pycore_typeobject.h index bbf8544b09f0fb..734b3bf7551aef 100644 --- a/Include/internal/pycore_typeobject.h +++ b/Include/internal/pycore_typeobject.h @@ -133,7 +133,7 @@ _PyType_IsReady(PyTypeObject *type) extern PyObject* _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int *suppress_missing_attribute); -extern PyObject* _Py_type_getattro(PyTypeObject *type, PyObject *name); +extern PyObject* _Py_type_getattro(PyObject *type, PyObject *name); extern PyObject* _Py_slot_tp_getattro(PyObject *self, PyObject *name); extern PyObject* _Py_slot_tp_getattr_hook(PyObject *self, PyObject *name); diff --git a/Objects/object.c b/Objects/object.c index d145674cb3ba34..046eaa43cabc27 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1196,7 +1196,7 @@ PyObject_GetOptionalAttr(PyObject *v, PyObject *name, PyObject **result) } return 0; } - if (tp->tp_getattro == (getattrofunc)_Py_type_getattro) { + if (tp->tp_getattro == _Py_type_getattro) { int supress_missing_attribute_exception = 0; *result = _Py_type_getattro_impl((PyTypeObject*)v, name, &supress_missing_attribute_exception); if (supress_missing_attribute_exception) { diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 86799a7547fc9a..5688ad71c3ef34 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4919,9 +4919,9 @@ _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int * suppress_missin /* This is similar to PyObject_GenericGetAttr(), but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ PyObject * -_Py_type_getattro(PyTypeObject *type, PyObject *name) +_Py_type_getattro(PyObject *type, PyObject *name) { - return _Py_type_getattro_impl(type, name, NULL); + return _Py_type_getattro_impl((PyTypeObject *)type, name, NULL); } static int @@ -5370,7 +5370,7 @@ PyTypeObject PyType_Type = { 0, /* tp_hash */ type_call, /* tp_call */ 0, /* tp_str */ - (getattrofunc)_Py_type_getattro, /* tp_getattro */ + _Py_type_getattro, /* tp_getattro */ type_setattro, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |