diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2016-12-27 14:19:20 +0100 |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2016-12-27 14:19:20 +0100 |
commit | e10ca3a0fe10d825689179e9958c70aef01f4230 (patch) | |
tree | f7dc9b56ba188d143a616062ec9e47f434aa32a3 /Modules/_weakref.c | |
parent | 1fee5151f72e4e141eb37e7ab0da901d1b610f45 (diff) | |
download | cpython-git-e10ca3a0fe10d825689179e9958c70aef01f4230.tar.gz |
Issue #28427: old keys should not remove new values from
WeakValueDictionary when collecting from another thread.
Diffstat (limited to 'Modules/_weakref.c')
-rw-r--r-- | Modules/_weakref.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Modules/_weakref.c b/Modules/_weakref.c index 805d6d0985..f9c68d6a64 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -35,6 +35,46 @@ _weakref_getweakrefcount_impl(PyObject *module, PyObject *object) } +static int +is_dead_weakref(PyObject *value) +{ + if (!PyWeakref_Check(value)) { + PyErr_SetString(PyExc_TypeError, "not a weakref"); + return -1; + } + return PyWeakref_GET_OBJECT(value) == Py_None; +} + +/*[clinic input] + +_weakref._remove_dead_weakref -> object + + dct: object(subclass_of='&PyDict_Type') + key: object + / + +Atomically remove key from dict if it points to a dead weakref. +[clinic start generated code]*/ + +static PyObject * +_weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct, + PyObject *key) +/*[clinic end generated code: output=d9ff53061fcb875c input=19fc91f257f96a1d]*/ +{ + if (_PyDict_DelItemIf(dct, key, is_dead_weakref) < 0) { + if (PyErr_ExceptionMatches(PyExc_KeyError)) + /* This function is meant to allow safe weak-value dicts + with GC in another thread (see issue #28427), so it's + ok if the key doesn't exist anymore. + */ + PyErr_Clear(); + else + return NULL; + } + Py_RETURN_NONE; +} + + PyDoc_STRVAR(weakref_getweakrefs__doc__, "getweakrefs(object) -- return a list of all weak reference objects\n" "that point to 'object'."); @@ -88,6 +128,7 @@ weakref_proxy(PyObject *self, PyObject *args) static PyMethodDef weakref_functions[] = { _WEAKREF_GETWEAKREFCOUNT_METHODDEF + _WEAKREF__REMOVE_DEAD_WEAKREF_METHODDEF {"getweakrefs", weakref_getweakrefs, METH_O, weakref_getweakrefs__doc__}, {"proxy", weakref_proxy, METH_VARARGS, |