|
1 | 1 | #include "Python.h" |
2 | 2 | #include "pycore_critical_section.h" |
| 3 | +#include "pycore_global_objects.h" // _Py_INTERP_STATIC_OBJECT() |
| 4 | +#include "pycore_interp_structs.h" // _PyInterpreterState_GET() |
3 | 5 | #include "pycore_lock.h" |
4 | 6 | #include "pycore_modsupport.h" // _PyArg_NoKwnames() |
5 | 7 | #include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR() |
@@ -72,7 +74,6 @@ init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback) |
72 | 74 | _PyObject_SetMaybeWeakref(ob); |
73 | 75 | _PyObject_SetMaybeWeakref((PyObject *)self); |
74 | 76 | #endif |
75 | | - self->is_subclass = 0; |
76 | 77 | } |
77 | 78 |
|
78 | 79 | // Clear the weakref and steal its callback into `callback`, if provided. |
@@ -1133,3 +1134,55 @@ _PyWeakref_IsDead(PyObject *weakref) |
1133 | 1134 | { |
1134 | 1135 | return _PyWeakref_IS_DEAD(weakref); |
1135 | 1136 | } |
| 1137 | + |
| 1138 | +int |
| 1139 | +_PyWeakref_InitSubclassSentinel(PyInterpreterState *interp) |
| 1140 | +{ |
| 1141 | + PyObject *code = (PyObject *)PyCode_NewEmpty( |
| 1142 | + "<generated>", |
| 1143 | + "<subclasses_weakref_sentinel>", |
| 1144 | + 0); |
| 1145 | + if (code == NULL) { |
| 1146 | + return -1; |
| 1147 | + } |
| 1148 | + |
| 1149 | + PyObject *globals = PyDict_New(); |
| 1150 | + if (globals == NULL) { |
| 1151 | + Py_DECREF(code); |
| 1152 | + return -1; |
| 1153 | + } |
| 1154 | + |
| 1155 | + PyObject *func = PyFunction_New(code, globals); |
| 1156 | + Py_DECREF(globals); |
| 1157 | + Py_DECREF(code); |
| 1158 | + if (func == NULL) { |
| 1159 | + return -1; |
| 1160 | + } |
| 1161 | + |
| 1162 | + _Py_INTERP_SINGLETON(interp, subclasses_weakref_sentinel) = func; |
| 1163 | + return 0; |
| 1164 | +} |
| 1165 | + |
| 1166 | +PyObject * |
| 1167 | +_PyWeakref_NewSubclassRef(PyObject *ob) |
| 1168 | +{ |
| 1169 | + PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 1170 | + if (interp != interp->runtime->interpreters.main) { |
| 1171 | + interp = interp->runtime->interpreters.main; |
| 1172 | + } |
| 1173 | + PyObject *func = _Py_INTERP_SINGLETON(interp, subclasses_weakref_sentinel); |
| 1174 | + return PyWeakref_NewRef(ob, func); |
| 1175 | +} |
| 1176 | + |
| 1177 | +int |
| 1178 | +_PyWeakref_IsSubclassRef(PyWeakReference *weakref) |
| 1179 | +{ |
| 1180 | + assert(weakref != NULL); |
| 1181 | + |
| 1182 | + PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 1183 | + if (interp != interp->runtime->interpreters.main) { |
| 1184 | + interp = interp->runtime->interpreters.main; |
| 1185 | + } |
| 1186 | + PyObject *func = _Py_INTERP_SINGLETON(interp, subclasses_weakref_sentinel); |
| 1187 | + return weakref->wr_callback == func; |
| 1188 | +} |
0 commit comments