Skip to content

Commit 46a3190

Browse files
authored
gh-105927: Avoid calling PyWeakref_GET_OBJECT() (#105997)
* Replace PyWeakref_GET_OBJECT() with _PyWeakref_GET_REF(). * _sqlite/blob.c now holds a strong reference to the blob object while calling close_blob(). * _xidregistry_find_type() now holds a strong reference to registered while using it.
1 parent c38da1e commit 46a3190

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

Modules/_sqlite/blob.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
#ifndef Py_BUILD_CORE_BUILTIN
2+
# define Py_BUILD_CORE_MODULE 1
3+
#endif
4+
15
#include "blob.h"
26
#include "util.h"
7+
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
38

49
#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
510
#include "clinic/blob.c.h"
@@ -97,10 +102,12 @@ pysqlite_close_all_blobs(pysqlite_Connection *self)
97102
{
98103
for (int i = 0; i < PyList_GET_SIZE(self->blobs); i++) {
99104
PyObject *weakref = PyList_GET_ITEM(self->blobs, i);
100-
PyObject *blob = PyWeakref_GetObject(weakref);
101-
if (!Py_IsNone(blob)) {
102-
close_blob((pysqlite_Blob *)blob);
105+
PyObject *blob = _PyWeakref_GET_REF(weakref);
106+
if (blob == NULL) {
107+
continue;
103108
}
109+
close_blob((pysqlite_Blob *)blob);
110+
Py_DECREF(blob);
104111
}
105112
}
106113

Objects/weakrefobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ weakref_vectorcall(PyObject *self, PyObject *const *args,
140140
if (!_PyArg_CheckPositional("weakref", nargs, 0, 0)) {
141141
return NULL;
142142
}
143-
return Py_NewRef(PyWeakref_GET_OBJECT(self));
143+
PyObject *obj = _PyWeakref_GET_REF(self);
144+
if (obj == NULL) {
145+
Py_RETURN_NONE;
146+
}
147+
return obj;
144148
}
145149

146150
static Py_hash_t

Python/pystate.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
1414
#include "pycore_pystate.h"
1515
#include "pycore_runtime_init.h" // _PyRuntimeState_INIT
16-
#include "pycore_sysmodule.h"
16+
#include "pycore_sysmodule.h" // _PySys_Audit()
17+
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
1718

1819
/* --------------------------------------------------------------------------
1920
CAUTION
@@ -2589,16 +2590,18 @@ _xidregistry_find_type(struct _xidregistry *xidregistry, PyTypeObject *cls)
25892590
{
25902591
struct _xidregitem *cur = xidregistry->head;
25912592
while (cur != NULL) {
2592-
PyObject *registered = PyWeakref_GetObject(cur->cls);
2593-
if (registered == Py_None) {
2593+
PyObject *registered = _PyWeakref_GET_REF(cur->cls);
2594+
if (registered == NULL) {
25942595
// The weakly ref'ed object was freed.
25952596
cur = _xidregistry_remove_entry(xidregistry, cur);
25962597
}
25972598
else {
25982599
assert(PyType_Check(registered));
25992600
if (registered == (PyObject *)cls) {
2601+
Py_DECREF(registered);
26002602
return cur;
26012603
}
2604+
Py_DECREF(registered);
26022605
cur = cur->next;
26032606
}
26042607
}

0 commit comments

Comments
 (0)