Skip to content

Commit 0fe20fc

Browse files
authored
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.
1 parent 23a4e3b commit 0fe20fc

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

Lib/test/test_capi/test_watchers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ def test_watch_unassigned_watcher_id(self):
176176

177177
def test_unwatch_non_dict(self):
178178
with self.watcher() as wid:
179-
with self.assertRaisesRegex(ValueError, r"Cannot watch non-dictionary"):
180-
self.unwatch(wid, 1)
179+
for wrong_type in (frozendict(), 5, [123], object()):
180+
with self.assertRaisesRegex(ValueError, r"Cannot watch non-dictionary"):
181+
self.unwatch(wid, wrong_type)
181182

182183
def test_unwatch_out_of_range_watcher_id(self):
183184
d = {}

Objects/dictobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7912,7 +7912,7 @@ validate_watcher_id(PyInterpreterState *interp, int watcher_id)
79127912
int
79137913
PyDict_Watch(int watcher_id, PyObject* dict)
79147914
{
7915-
if (!PyAnyDict_Check(dict)) {
7915+
if (!PyDict_Check(dict)) {
79167916
PyErr_SetString(PyExc_ValueError, "Cannot watch non-dictionary");
79177917
return -1;
79187918
}
@@ -7927,7 +7927,7 @@ PyDict_Watch(int watcher_id, PyObject* dict)
79277927
int
79287928
PyDict_Unwatch(int watcher_id, PyObject* dict)
79297929
{
7930-
if (!PyAnyDict_Check(dict)) {
7930+
if (!PyDict_Check(dict)) {
79317931
PyErr_SetString(PyExc_ValueError, "Cannot watch non-dictionary");
79327932
return -1;
79337933
}

0 commit comments

Comments
 (0)