https://github.com/python/cpython/commit/0fe20fc1703c52c0b2597d70df6cad9b3e4056f0
commit: 0fe20fc1703c52c0b2597d70df6cad9b3e4056f0
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-03-05T12:31:29+01:00
summary:

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.

files:
M Lib/test/test_capi/test_watchers.py
M Objects/dictobject.c

diff --git a/Lib/test/test_capi/test_watchers.py 
b/Lib/test/test_capi/test_watchers.py
index bef72032513da5..67595e3550b0ff 100644
--- a/Lib/test/test_capi/test_watchers.py
+++ b/Lib/test/test_capi/test_watchers.py
@@ -176,8 +176,9 @@ def test_watch_unassigned_watcher_id(self):
 
     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 2552216152f98d..e0127f04249f6b 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;
     }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to