Skip to content

Commit a545a86

Browse files
authored
gh-111178: Make slot functions in typeobject.c have compatible types (GH-112752)
1 parent 57b7e52 commit a545a86

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

Include/internal/pycore_typeobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ _PyType_IsReady(PyTypeObject *type)
133133

134134
extern PyObject* _Py_type_getattro_impl(PyTypeObject *type, PyObject *name,
135135
int *suppress_missing_attribute);
136-
extern PyObject* _Py_type_getattro(PyTypeObject *type, PyObject *name);
136+
extern PyObject* _Py_type_getattro(PyObject *type, PyObject *name);
137137

138138
extern PyObject* _Py_BaseObject_RichCompare(PyObject* self, PyObject* other, int op);
139139

Objects/object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ PyObject_GetOptionalAttr(PyObject *v, PyObject *name, PyObject **result)
11961196
}
11971197
return 0;
11981198
}
1199-
if (tp->tp_getattro == (getattrofunc)_Py_type_getattro) {
1199+
if (tp->tp_getattro == _Py_type_getattro) {
12001200
int supress_missing_attribute_exception = 0;
12011201
*result = _Py_type_getattro_impl((PyTypeObject*)v, name, &supress_missing_attribute_exception);
12021202
if (supress_missing_attribute_exception) {

Objects/typeobject.c

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,8 +1597,9 @@ static PyGetSetDef type_getsets[] = {
15971597
};
15981598

15991599
static PyObject *
1600-
type_repr(PyTypeObject *type)
1600+
type_repr(PyObject *self)
16011601
{
1602+
PyTypeObject *type = (PyTypeObject *)self;
16021603
if (type->tp_name == NULL) {
16031604
// type_repr() called before the type is fully initialized
16041605
// by PyType_Ready().
@@ -1630,8 +1631,9 @@ type_repr(PyTypeObject *type)
16301631
}
16311632

16321633
static PyObject *
1633-
type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
1634+
type_call(PyObject *self, PyObject *args, PyObject *kwds)
16341635
{
1636+
PyTypeObject *type = (PyTypeObject *)self;
16351637
PyObject *obj;
16361638
PyThreadState *tstate = _PyThreadState_GET();
16371639

@@ -4917,14 +4919,15 @@ _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int * suppress_missin
49174919
/* This is similar to PyObject_GenericGetAttr(),
49184920
but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
49194921
PyObject *
4920-
_Py_type_getattro(PyTypeObject *type, PyObject *name)
4922+
_Py_type_getattro(PyObject *type, PyObject *name)
49214923
{
4922-
return _Py_type_getattro_impl(type, name, NULL);
4924+
return _Py_type_getattro_impl((PyTypeObject *)type, name, NULL);
49234925
}
49244926

49254927
static int
4926-
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
4928+
type_setattro(PyObject *self, PyObject *name, PyObject *value)
49274929
{
4930+
PyTypeObject *type = (PyTypeObject *)self;
49284931
int res;
49294932
if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
49304933
PyErr_Format(
@@ -5069,8 +5072,10 @@ _PyStaticType_Dealloc(PyInterpreterState *interp, PyTypeObject *type)
50695072

50705073

50715074
static void
5072-
type_dealloc(PyTypeObject *type)
5075+
type_dealloc(PyObject *self)
50735076
{
5077+
PyTypeObject *type = (PyTypeObject *)self;
5078+
50745079
// Assert this is a heap-allocated type object
50755080
_PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE);
50765081

@@ -5257,8 +5262,10 @@ PyDoc_STRVAR(type_doc,
52575262
"type(name, bases, dict, **kwds) -> a new type");
52585263

52595264
static int
5260-
type_traverse(PyTypeObject *type, visitproc visit, void *arg)
5265+
type_traverse(PyObject *self, visitproc visit, void *arg)
52615266
{
5267+
PyTypeObject *type = (PyTypeObject *)self;
5268+
52625269
/* Because of type_is_gc(), the collector only calls this
52635270
for heaptypes. */
52645271
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
@@ -5286,8 +5293,10 @@ type_traverse(PyTypeObject *type, visitproc visit, void *arg)
52865293
}
52875294

52885295
static int
5289-
type_clear(PyTypeObject *type)
5296+
type_clear(PyObject *self)
52905297
{
5298+
PyTypeObject *type = (PyTypeObject *)self;
5299+
52915300
/* Because of type_is_gc(), the collector only calls this
52925301
for heaptypes. */
52935302
_PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE);
@@ -5334,9 +5343,9 @@ type_clear(PyTypeObject *type)
53345343
}
53355344

53365345
static int
5337-
type_is_gc(PyTypeObject *type)
5346+
type_is_gc(PyObject *type)
53385347
{
5339-
return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
5348+
return ((PyTypeObject *)type)->tp_flags & Py_TPFLAGS_HEAPTYPE;
53405349
}
53415350

53425351

@@ -5349,28 +5358,28 @@ PyTypeObject PyType_Type = {
53495358
"type", /* tp_name */
53505359
sizeof(PyHeapTypeObject), /* tp_basicsize */
53515360
sizeof(PyMemberDef), /* tp_itemsize */
5352-
(destructor)type_dealloc, /* tp_dealloc */
5361+
type_dealloc, /* tp_dealloc */
53535362
offsetof(PyTypeObject, tp_vectorcall), /* tp_vectorcall_offset */
53545363
0, /* tp_getattr */
53555364
0, /* tp_setattr */
53565365
0, /* tp_as_async */
5357-
(reprfunc)type_repr, /* tp_repr */
5366+
type_repr, /* tp_repr */
53585367
&type_as_number, /* tp_as_number */
53595368
0, /* tp_as_sequence */
53605369
0, /* tp_as_mapping */
53615370
0, /* tp_hash */
5362-
(ternaryfunc)type_call, /* tp_call */
5371+
type_call, /* tp_call */
53635372
0, /* tp_str */
5364-
(getattrofunc)_Py_type_getattro, /* tp_getattro */
5365-
(setattrofunc)type_setattro, /* tp_setattro */
5373+
_Py_type_getattro, /* tp_getattro */
5374+
type_setattro, /* tp_setattro */
53665375
0, /* tp_as_buffer */
53675376
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
53685377
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS |
53695378
Py_TPFLAGS_HAVE_VECTORCALL |
53705379
Py_TPFLAGS_ITEMS_AT_END, /* tp_flags */
53715380
type_doc, /* tp_doc */
5372-
(traverseproc)type_traverse, /* tp_traverse */
5373-
(inquiry)type_clear, /* tp_clear */
5381+
type_traverse, /* tp_traverse */
5382+
type_clear, /* tp_clear */
53745383
0, /* tp_richcompare */
53755384
offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
53765385
0, /* tp_iter */
@@ -5387,7 +5396,7 @@ PyTypeObject PyType_Type = {
53875396
0, /* tp_alloc */
53885397
type_new, /* tp_new */
53895398
PyObject_GC_Del, /* tp_free */
5390-
(inquiry)type_is_gc, /* tp_is_gc */
5399+
type_is_gc, /* tp_is_gc */
53915400
.tp_vectorcall = type_vectorcall,
53925401
};
53935402

@@ -6561,6 +6570,12 @@ PyDoc_STRVAR(object_doc,
65616570
"When called, it accepts no arguments and returns a new featureless\n"
65626571
"instance that has no instance attributes and cannot be given any.\n");
65636572

6573+
static Py_hash_t
6574+
object_hash(PyObject *obj)
6575+
{
6576+
return _Py_HashPointer(obj);
6577+
}
6578+
65646579
PyTypeObject PyBaseObject_Type = {
65656580
PyVarObject_HEAD_INIT(&PyType_Type, 0)
65666581
"object", /* tp_name */
@@ -6575,7 +6590,7 @@ PyTypeObject PyBaseObject_Type = {
65756590
0, /* tp_as_number */
65766591
0, /* tp_as_sequence */
65776592
0, /* tp_as_mapping */
6578-
(hashfunc)_Py_HashPointer, /* tp_hash */
6593+
object_hash, /* tp_hash */
65796594
0, /* tp_call */
65806595
object_str, /* tp_str */
65816596
PyObject_GenericGetAttr, /* tp_getattro */

0 commit comments

Comments
 (0)