Skip to content

Commit c90f229

Browse files
committed
pythongh-105927: Use PyWeakref_GetRef()
1 parent b7997c9 commit c90f229

File tree

6 files changed

+50
-45
lines changed

6 files changed

+50
-45
lines changed

Modules/_abc.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -841,16 +841,17 @@ subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
841841
assert(i == registry_size);
842842

843843
for (i = 0; i < registry_size; i++) {
844-
PyObject *rkey = PyWeakref_GetObject(copy[i]);
845-
if (rkey == NULL) {
844+
if (!PyWeakref_Check(copy[i])) {
845+
PyErr_BadInternalCall();
846846
// Someone inject non-weakref type in the registry.
847847
ret = -1;
848848
break;
849849
}
850-
if (rkey == Py_None) {
850+
851+
PyObject *rkey = PyWeakref_GetRef(copy[i]);
852+
if (rkey == NULL) {
851853
continue;
852854
}
853-
Py_INCREF(rkey);
854855
int r = PyObject_IsSubclass(subclass, rkey);
855856
Py_DECREF(rkey);
856857
if (r < 0) {

Modules/_ctypes/_ctypes.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,18 @@ PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item)
238238
return result;
239239
}
240240

241-
PyObject *
242-
PyDict_GetItemProxy(PyObject *dict, PyObject *key)
241+
static PyObject *
242+
_PyDict_GetItemProxy(PyObject *dict, PyObject *key)
243243
{
244-
PyObject *result;
245244
PyObject *item = PyDict_GetItemWithError(dict, key);
246-
247-
if (item == NULL)
248-
return NULL;
249-
if (!PyWeakref_CheckProxy(item))
250-
return item;
251-
result = PyWeakref_GET_OBJECT(item);
252-
if (result == Py_None)
245+
if (item == NULL) {
253246
return NULL;
254-
return result;
247+
}
248+
249+
if (!PyWeakref_CheckProxy(item)) {
250+
return Py_NewRef(item);
251+
}
252+
return PyWeakref_GetRef(item);
255253
}
256254

257255
/******************************************************************/
@@ -4848,9 +4846,8 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
48484846
Py_DECREF(len);
48494847
if (!key)
48504848
return NULL;
4851-
result = PyDict_GetItemProxy(cache, key);
4849+
result = _PyDict_GetItemProxy(cache, key);
48524850
if (result) {
4853-
Py_INCREF(result);
48544851
Py_DECREF(key);
48554852
return result;
48564853
}

Modules/_ssl.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4393,14 +4393,13 @@ _servername_callback(SSL *s, int *al, void *args)
43934393
* will be passed. If both do not exist only then the C-level object is
43944394
* passed. */
43954395
if (ssl->owner)
4396-
ssl_socket = PyWeakref_GetObject(ssl->owner);
4396+
ssl_socket = PyWeakref_GetRef(ssl->owner);
43974397
else if (ssl->Socket)
4398-
ssl_socket = PyWeakref_GetObject(ssl->Socket);
4398+
ssl_socket = PyWeakref_GetRef(ssl->Socket);
43994399
else
4400-
ssl_socket = (PyObject *) ssl;
4400+
ssl_socket = Py_NewRef(ssl);
44014401

4402-
Py_INCREF(ssl_socket);
4403-
if (ssl_socket == Py_None)
4402+
if (ssl_socket == Py_NULL)
44044403
goto error;
44054404

44064405
if (servername == NULL) {

Modules/_ssl/debughelpers.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ _PySSL_msg_callback(int write_p, int version, int content_type,
1515
PyGILState_STATE threadstate;
1616
PyObject *res = NULL;
1717
PySSLSocket *ssl_obj = NULL; /* ssl._SSLSocket, borrowed ref */
18-
PyObject *ssl_socket = NULL; /* ssl.SSLSocket or ssl.SSLObject */
1918
int msg_type;
2019

2120
threadstate = PyGILState_Ensure();
@@ -27,13 +26,14 @@ _PySSL_msg_callback(int write_p, int version, int content_type,
2726
return;
2827
}
2928

29+
PyObject *ssl_socket; /* ssl.SSLSocket or ssl.SSLObject */
3030
if (ssl_obj->owner)
31-
ssl_socket = PyWeakref_GetObject(ssl_obj->owner);
31+
ssl_socket = PyWeakref_GetRef(ssl_obj->owner);
3232
else if (ssl_obj->Socket)
33-
ssl_socket = PyWeakref_GetObject(ssl_obj->Socket);
33+
ssl_socket = PyWeakref_GetRef(ssl_obj->Socket);
3434
else
35-
ssl_socket = (PyObject *)ssl_obj;
36-
Py_INCREF(ssl_socket);
35+
ssl_socket = (PyObject *)Py_NewRef(ssl_obj);
36+
assert(ssl_socket != NULL);
3737

3838
/* assume that OpenSSL verifies all payload and buf len is of sufficient
3939
length */

Modules/_threadmodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,14 +1025,14 @@ static PyObject *
10251025
_localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
10261026
{
10271027
assert(PyWeakref_CheckRef(localweakref));
1028-
PyObject *obj = PyWeakref_GET_OBJECT(localweakref);
1029-
if (obj == Py_None) {
1028+
PyObject *obj = PyWeakref_GetRef(localweakref);
1029+
if (obj == NULL) {
10301030
Py_RETURN_NONE;
10311031
}
10321032

10331033
/* If the thread-local object is still alive and not being cleared,
10341034
remove the corresponding local dict */
1035-
localobject *self = (localobject *)Py_NewRef(obj);
1035+
localobject *self = (localobject *)obj;
10361036
if (self->dummies != NULL) {
10371037
PyObject *ldict;
10381038
ldict = PyDict_GetItemWithError(self->dummies, dummyweakref);
@@ -1320,14 +1320,14 @@ release_sentinel(void *wr_raw)
13201320
/* Tricky: this function is called when the current thread state
13211321
is being deleted. Therefore, only simple C code can safely
13221322
execute here. */
1323-
PyObject *obj = PyWeakref_GET_OBJECT(wr);
1324-
lockobject *lock;
1325-
if (obj != Py_None) {
1326-
lock = (lockobject *) obj;
1323+
PyObject *obj = PyWeakref_GetRef(wr);
1324+
if (obj != NULL) {
1325+
lockobject *lock = (lockobject *) obj;
13271326
if (lock->locked) {
13281327
PyThread_release_lock(lock->lock_lock);
13291328
lock->locked = 0;
13301329
}
1330+
Py_DECREF(obj);
13311331
}
13321332
/* Deallocating a weakref with a NULL callback only calls
13331333
PyObject_GC_Del(), which can't call any Python code. */

Objects/typeobject.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
#include "pycore_code.h" // CO_FAST_FREE
66
#include "pycore_symtable.h" // _Py_Mangle()
77
#include "pycore_dict.h" // _PyDict_KeysSize()
8+
#include "pycore_frame.h" // _PyInterpreterFrame
89
#include "pycore_initconfig.h" // _PyStatus_OK()
10+
#include "pycore_long.h" // _PyLong_IsNegative()
911
#include "pycore_memoryobject.h" // _PyMemoryView_FromBufferProc()
1012
#include "pycore_moduleobject.h" // _PyModule_GetDef()
1113
#include "pycore_object.h" // _PyType_HasFeature()
12-
#include "pycore_long.h" // _PyLong_IsNegative()
1314
#include "pycore_pyerrors.h" // _PyErr_Occurred()
1415
#include "pycore_pystate.h" // _PyThreadState_GET()
1516
#include "pycore_typeobject.h" // struct type_cache
1617
#include "pycore_unionobject.h" // _Py_union_type_or
17-
#include "pycore_frame.h" // _PyInterpreterFrame
1818
#include "opcode.h" // MAKE_CELL
1919
#include "structmember.h" // PyMemberDef
2020

@@ -75,10 +75,8 @@ slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value);
7575
static inline PyTypeObject *
7676
type_from_ref(PyObject *ref)
7777
{
78-
assert(PyWeakref_CheckRef(ref));
79-
PyObject *obj = PyWeakref_GET_OBJECT(ref); // borrowed ref
80-
assert(obj != NULL);
81-
if (obj == Py_None) {
78+
PyObject *obj = PyWeakref_GetRef(ref);
79+
if (obj == NULL) {
8280
return NULL;
8381
}
8482
assert(PyType_Check(obj));
@@ -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)