GeezerVison is showing github.com — gh-125608: Trigger dictionary watchers when inline values change (#12… · 2union/ircpython@5989eb7
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Lib/test/test_capi/test_watchers.py +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -97,6 +97,23 @@ def test_dealloc(self): |
97 | 97 | del d |
98 | 98 | self.assert_events(["dealloc"]) |
99 | 99 |
|
| 100 | + def test_object_dict(self): |
| 101 | + class MyObj: pass |
| 102 | + o = MyObj() |
| 103 | + |
| 104 | + with self.watcher() as wid: |
| 105 | + self.watch(wid, o.__dict__) |
| 106 | + o.foo = "bar" |
| 107 | + o.foo = "baz" |
| 108 | + del o.foo |
| 109 | + self.assert_events(["new:foo:bar", "mod:foo:baz", "del:foo"]) |
| 110 | + |
| 111 | + with self.watcher() as wid: |
| 112 | + self.watch(wid, o.__dict__) |
| 113 | + for _ in range(100): |
| 114 | + o.foo = "bar" |
| 115 | + self.assert_events(["new:foo:bar"] + ["mod:foo:bar"] * 99) |
| 116 | + |
100 | 117 | def test_unwatch(self): |
101 | 118 | d = {} |
102 | 119 | with self.watcher() as wid: |
|
Collapse file
Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +Fix a bug where dictionary watchers (e.g., :c:func:`PyDict_Watch`) on an |
| 2 | +object's attribute dictionary (:attr:`~object.__dict__`) were not triggered |
| 3 | +when the object's attributes were modified. |
Collapse file
Copy file name to clipboardExpand all lines: Objects/dictobject.c +15-6Lines changed: 15 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6835,15 +6835,24 @@ store_instance_attr_lock_held(PyObject *obj, PyDictValues *values, |
6835 | 6835 | } |
6836 | 6836 |
|
6837 | 6837 | PyObject *old_value = values->values[ix]; |
| 6838 | + if (old_value == NULL && value == NULL) { |
| 6839 | + PyErr_Format(PyExc_AttributeError, |
| 6840 | + "'%.100s' object has no attribute '%U'", |
| 6841 | + Py_TYPE(obj)->tp_name, name); |
| 6842 | + return -1; |
| 6843 | + } |
| 6844 | + |
| 6845 | + if (dict) { |
| 6846 | + PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 6847 | + PyDict_WatchEvent event = (old_value == NULL ? PyDict_EVENT_ADDED : |
| 6848 | + value == NULL ? PyDict_EVENT_DELETED : |
| 6849 | + PyDict_EVENT_MODIFIED); |
| 6850 | + _PyDict_NotifyEvent(interp, event, dict, name, value); |
| 6851 | + } |
| 6852 | + |
6838 | 6853 | FT_ATOMIC_STORE_PTR_RELEASE(values->values[ix], Py_XNewRef(value)); |
6839 | 6854 |
|
6840 | 6855 | if (old_value == NULL) { |
6841 | | - if (value == NULL) { |
6842 | | - PyErr_Format(PyExc_AttributeError, |
6843 | | - "'%.100s' object has no attribute '%U'", |
6844 | | - Py_TYPE(obj)->tp_name, name); |
6845 | | - return -1; |
6846 | | - } |
6847 | 6856 | _PyDictValues_AddToInsertionOrder(values, ix); |
6848 | 6857 | if (dict) { |
6849 | 6858 | assert(dict->ma_values == values); |
|