]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-141510: Don't accept frozendict in PyDict_Watch() (#145529)
authorVictor Stinner <vstinner@python.org>
Thu, 5 Mar 2026 11:31:29 +0000 (12:31 +0100)
committerGitHub <noreply@github.com>
Thu, 5 Mar 2026 11:31:29 +0000 (12:31 +0100)
Don't accept frozendict in PyDict_Watch() and PyDict_Unwatch().
A frozendict cannot be modified, so it's not useful to watch for
modifications.

Lib/test/test_capi/test_watchers.py
Objects/dictobject.c

index bef72032513da5d13ae9ec7ce38abd7f50d59536..67595e3550b0ff23971360dff29225865ba02245 100644 (file)
@@ -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 = {}
index 2552216152f98d473499ce0d9eac44e10585db86..e0127f04249f6be4a5222bec9302f744a8dbe9c0 100644 (file)
@@ -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;
     }