From: Victor Stinner Date: Thu, 5 Mar 2026 11:31:29 +0000 (+0100) Subject: gh-141510: Don't accept frozendict in PyDict_Watch() (#145529) X-Git-Tag: v3.15.0a7~59 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0fe20fc1703c52c0b2597d70df6cad9b3e4056f0;p=thirdparty%2FPython%2Fcpython.git gh-141510: Don't accept frozendict in PyDict_Watch() (#145529) Don't accept frozendict in PyDict_Watch() and PyDict_Unwatch(). A frozendict cannot be modified, so it's not useful to watch for modifications. --- diff --git a/Lib/test/test_capi/test_watchers.py b/Lib/test/test_capi/test_watchers.py index bef72032513d..67595e3550b0 100644 --- a/Lib/test/test_capi/test_watchers.py +++ b/Lib/test/test_capi/test_watchers.py @@ -176,8 +176,9 @@ class TestDictWatchers(unittest.TestCase): def test_unwatch_non_dict(self): with self.watcher() as wid: - with self.assertRaisesRegex(ValueError, r"Cannot watch non-dictionary"): - self.unwatch(wid, 1) + for wrong_type in (frozendict(), 5, [123], object()): + with self.assertRaisesRegex(ValueError, r"Cannot watch non-dictionary"): + self.unwatch(wid, wrong_type) def test_unwatch_out_of_range_watcher_id(self): d = {} diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 2552216152f9..e0127f04249f 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -7912,7 +7912,7 @@ validate_watcher_id(PyInterpreterState *interp, int watcher_id) int PyDict_Watch(int watcher_id, PyObject* dict) { - if (!PyAnyDict_Check(dict)) { + if (!PyDict_Check(dict)) { PyErr_SetString(PyExc_ValueError, "Cannot watch non-dictionary"); return -1; } @@ -7927,7 +7927,7 @@ PyDict_Watch(int watcher_id, PyObject* dict) int PyDict_Unwatch(int watcher_id, PyObject* dict) { - if (!PyAnyDict_Check(dict)) { + if (!PyDict_Check(dict)) { PyErr_SetString(PyExc_ValueError, "Cannot watch non-dictionary"); return -1; }