Skip to content

Commit 9f7a1df

Browse files
committed
gh-105927: type_from_ref() uses _PyWeakref_GET_REF()
type_from_ref() now returns a strong reference to the type, instead of a borrowed reference: replace PyWeakref_GET_OBJECT() with _PyWeakref_GET_REF().
1 parent fb1e691 commit 9f7a1df

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

Objects/typeobject.c

+21-13
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
#include "Python.h"
44
#include "pycore_call.h"
55
#include "pycore_code.h" // CO_FAST_FREE
6-
#include "pycore_symtable.h" // _Py_Mangle()
76
#include "pycore_dict.h" // _PyDict_KeysSize()
7+
#include "pycore_frame.h" // _PyInterpreterFrame
88
#include "pycore_initconfig.h" // _PyStatus_OK()
9+
#include "pycore_long.h" // _PyLong_IsNegative()
910
#include "pycore_memoryobject.h" // _PyMemoryView_FromBufferProc()
1011
#include "pycore_moduleobject.h" // _PyModule_GetDef()
1112
#include "pycore_object.h" // _PyType_HasFeature()
12-
#include "pycore_long.h" // _PyLong_IsNegative()
1313
#include "pycore_pyerrors.h" // _PyErr_Occurred()
1414
#include "pycore_pystate.h" // _PyThreadState_GET()
15+
#include "pycore_symtable.h" // _Py_Mangle()
1516
#include "pycore_typeobject.h" // struct type_cache
1617
#include "pycore_unionobject.h" // _Py_union_type_or
17-
#include "pycore_frame.h" // _PyInterpreterFrame
18+
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
1819
#include "opcode.h" // MAKE_CELL
1920
#include "structmember.h" // PyMemberDef
2021

@@ -75,13 +76,10 @@ slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value);
7576
static inline PyTypeObject *
7677
type_from_ref(PyObject *ref)
7778
{
78-
assert(PyWeakref_CheckRef(ref));
79-
PyObject *obj = PyWeakref_GET_OBJECT(ref); // borrowed ref
80-
assert(obj != NULL);
81-
if (obj == Py_None) {
79+
PyObject *obj = _PyWeakref_GET_REF(ref);
80+
if (obj == NULL) {
8281
return NULL;
8382
}
84-
assert(PyType_Check(obj));
8583
return _PyType_CAST(obj);
8684
}
8785

@@ -450,15 +448,17 @@ _PyType_GetSubclasses(PyTypeObject *self)
450448
Py_ssize_t i = 0;
451449
PyObject *ref; // borrowed ref
452450
while (PyDict_Next(subclasses, &i, NULL, &ref)) {
453-
PyTypeObject *subclass = type_from_ref(ref); // borrowed
451+
PyTypeObject *subclass = type_from_ref(ref);
454452
if (subclass == NULL) {
455453
continue;
456454
}
457455

458456
if (PyList_Append(list, _PyObject_CAST(subclass)) < 0) {
459457
Py_DECREF(list);
458+
Py_DECREF(subclass);
460459
return NULL;
461460
}
461+
Py_DECREF(subclass);
462462
}
463463
return list;
464464
}
@@ -778,11 +778,12 @@ PyType_Modified(PyTypeObject *type)
778778
Py_ssize_t i = 0;
779779
PyObject *ref;
780780
while (PyDict_Next(subclasses, &i, NULL, &ref)) {
781-
PyTypeObject *subclass = type_from_ref(ref); // borrowed
781+
PyTypeObject *subclass = type_from_ref(ref);
782782
if (subclass == NULL) {
783783
continue;
784784
}
785785
PyType_Modified(subclass);
786+
Py_DECREF(subclass);
786787
}
787788
}
788789

@@ -4989,12 +4990,13 @@ clear_static_tp_subclasses(PyTypeObject *type)
49894990
Py_ssize_t i = 0;
49904991
PyObject *key, *ref; // borrowed ref
49914992
while (PyDict_Next(subclasses, &i, &key, &ref)) {
4992-
PyTypeObject *subclass = type_from_ref(ref); // borrowed
4993+
PyTypeObject *subclass = type_from_ref(ref);
49934994
if (subclass == NULL) {
49944995
continue;
49954996
}
49964997
// All static builtin subtypes should have been finalized already.
49974998
assert(!(subclass->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN));
4999+
Py_DECREF(subclass);
49985000
}
49995001

50005002
clear_tp_subclasses(type);
@@ -7636,10 +7638,12 @@ get_subclasses_key(PyTypeObject *type, PyTypeObject *base)
76367638
PyObject *subclasses = lookup_tp_subclasses(base);
76377639
if (subclasses != NULL) {
76387640
while (PyDict_Next(subclasses, &i, &key, &ref)) {
7639-
PyTypeObject *subclass = type_from_ref(ref); // borrowed
7641+
PyTypeObject *subclass = type_from_ref(ref);
76407642
if (subclass == type) {
7643+
Py_DECREF(subclass);
76417644
return Py_NewRef(key);
76427645
}
7646+
Py_DECREF(subclass);
76437647
}
76447648
}
76457649
/* It wasn't found. */
@@ -10035,7 +10039,7 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name,
1003510039
Py_ssize_t i = 0;
1003610040
PyObject *ref;
1003710041
while (PyDict_Next(subclasses, &i, NULL, &ref)) {
10038-
PyTypeObject *subclass = type_from_ref(ref); // borrowed
10042+
PyTypeObject *subclass = type_from_ref(ref);
1003910043
if (subclass == NULL) {
1004010044
continue;
1004110045
}
@@ -10045,16 +10049,20 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name,
1004510049
if (dict != NULL && PyDict_Check(dict)) {
1004610050
int r = PyDict_Contains(dict, attr_name);
1004710051
if (r < 0) {
10052+
Py_DECREF(subclass);
1004810053
return -1;
1004910054
}
1005010055
if (r > 0) {
10056+
Py_DECREF(subclass);
1005110057
continue;
1005210058
}
1005310059
}
1005410060

1005510061
if (update_subclasses(subclass, attr_name, callback, data) < 0) {
10062+
Py_DECREF(subclass);
1005610063
return -1;
1005710064
}
10065+
Py_DECREF(subclass);
1005810066
}
1005910067
return 0;
1006010068
}

0 commit comments

Comments
 (0)