Skip to content

Commit 7f4fcc0

Browse files
committed
Correct more functions
1 parent 4ad5626 commit 7f4fcc0

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

Objects/weakrefobject.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ weakref_hash(PyWeakReference *self)
149149
PyErr_SetString(PyExc_TypeError, "weak object has gone away");
150150
return -1;
151151
}
152-
self->hash = PyObject_Hash(PyWeakref_GET_OBJECT(self));
152+
PyObject* obj = PyWeakref_GET_OBJECT(self);
153+
Py_INCREF(obj);
154+
self->hash = PyObject_Hash(obj);
155+
Py_DECREF(obj);
153156
return self->hash;
154157
}
155158

@@ -207,8 +210,14 @@ weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
207210
else
208211
Py_RETURN_FALSE;
209212
}
210-
return PyObject_RichCompare(PyWeakref_GET_OBJECT(self),
211-
PyWeakref_GET_OBJECT(other), op);
213+
PyObject* obj = PyWeakref_GET_OBJECT(self);
214+
PyObject* other_obj = PyWeakref_GET_OBJECT(other);
215+
Py_INCREF(obj);
216+
Py_INCREF(other);
217+
PyObject* res = PyObject_RichCompare(obj, other_obj, op);
218+
Py_DECREF(obj);
219+
Py_DECREF(other);
220+
return res;
212221
}
213222

214223
/* Given the head of an object's list of weak references, extract the
@@ -536,9 +545,13 @@ static int
536545
proxy_bool(PyWeakReference *proxy)
537546
{
538547
PyObject *o = PyWeakref_GET_OBJECT(proxy);
539-
if (!proxy_checkref(proxy))
548+
if (!proxy_checkref(proxy)) {
540549
return -1;
541-
return PyObject_IsTrue(o);
550+
}
551+
Py_INCREF(o);
552+
int res = PyObject_IsTrue(o);
553+
Py_DECREF(o);
554+
return res;
542555
}
543556

544557
static void
@@ -585,12 +598,12 @@ WRAP_BINARY(proxy_getitem, PyObject_GetItem)
585598
static int
586599
proxy_setitem(PyWeakReference *proxy, PyObject *key, PyObject *value)
587600
{
588-
int res = 0;
589601
if (!proxy_checkref(proxy))
590602
return -1;
591603

592604
PyObject *obj = PyWeakref_GET_OBJECT(proxy);
593605
Py_INCREF(obj);
606+
int res;
594607
if (value == NULL) {
595608
res = PyObject_DelItem(obj, key);
596609
} else {

0 commit comments

Comments
 (0)