FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159739: Release weak proxy operands on errors by jelle-openai · Pull Request #159968 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 14 additions & 0 deletions Lib/test/test_weakref.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,20 @@ class F(float):
self.assertEqual(p + 1.0, 3.0)
self.assertEqual(1.0 + p, 3.0) # this used to SEGV

def test_dead_proxy_operand_does_not_leak(self):
dead = weakref.proxy(Object(2))
gc_collect()
for operation in (operator.add, operator.eq, lambda a, b: pow(a, 2, b)):
with self.subTest(operation=operation):
live = Object(1)
ref = weakref.ref(live)
proxy = weakref.proxy(live)
with self.assertRaises(ReferenceError):
operation(proxy, dead)
del live
gc_collect()
self.assertIsNone(ref())

def test_callbacks_protected(self):
# Callbacks protected from already-set exceptions?
# Regression test for SF bug #478534.
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a reference leak in binary, ternary, and comparison operations on weak
reference proxies when another operand is a dead proxy.
54 changes: 39 additions & 15 deletions Objects/weakrefobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -543,19 +543,26 @@ proxy_check_ref(PyObject *obj)
}


/* If a parameter is a proxy, check that it is still "live" and wrap it,
* replacing the original value with the raw object. Raises ReferenceError
* if the param is a dead proxy.
/* Return a new reference to a live proxy's referent, or to the object itself
* if it is not a proxy. Raise ReferenceError for a dead proxy.
*/
static PyObject *
proxy_unwrap(PyObject *obj)
{
if (PyWeakref_CheckProxy(obj)) {
PyObject *referent = _PyWeakref_GET_REF(obj);
if (!proxy_check_ref(referent)) {
return NULL;
}
return referent;
}
return Py_NewRef(obj);
}

#define UNWRAP(o) \
if (PyWeakref_CheckProxy(o)) { \
o = _PyWeakref_GET_REF(o); \
if (!proxy_check_ref(o)) { \
return NULL; \
} \
} \
else { \
Py_INCREF(o); \
o = proxy_unwrap(o); \
if (o == NULL) { \
return NULL; \
}

#define WRAP_UNARY(method, generic) \
Expand All @@ -571,7 +578,11 @@ proxy_check_ref(PyObject *obj)
static PyObject * \
method(PyObject *x, PyObject *y) { \
UNWRAP(x); \
UNWRAP(y); \
y = proxy_unwrap(y); \
if (y == NULL) { \
Py_DECREF(x); \
return NULL; \
} \
PyObject* res = generic(x, y); \
Py_DECREF(x); \
Py_DECREF(y); \
Expand All @@ -585,9 +596,18 @@ proxy_check_ref(PyObject *obj)
static PyObject * \
method(PyObject *proxy, PyObject *v, PyObject *w) { \
UNWRAP(proxy); \
UNWRAP(v); \
v = proxy_unwrap(v); \
if (v == NULL) { \
Py_DECREF(proxy); \
return NULL; \
} \
if (w != NULL) { \
UNWRAP(w); \
w = proxy_unwrap(w); \
if (w == NULL) { \
Py_DECREF(proxy); \
Py_DECREF(v); \
return NULL; \
} \
} \
PyObject* res = generic(proxy, v, w); \
Py_DECREF(proxy); \
Expand Down Expand Up @@ -648,7 +668,11 @@ static PyObject *
proxy_richcompare(PyObject *proxy, PyObject *v, int op)
{
UNWRAP(proxy);
UNWRAP(v);
v = proxy_unwrap(v);
if (v == NULL) {
Py_DECREF(proxy);
return NULL;
}
PyObject* res = PyObject_RichCompare(proxy, v, op);
Py_DECREF(proxy);
Py_DECREF(v);
Expand Down
Loading

Back | FazBrowse Home | New Git URL