FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-125608: Trigger dictionary watchers when inline values change by colesbury · Pull Request #125611 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 17 additions & 0 deletions Lib/test/test_capi/test_watchers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ def test_dealloc(self):
del d
self.assert_events(["dealloc"])

def test_object_dict(self):
class MyObj: pass
o = MyObj()

with self.watcher() as wid:
self.watch(wid, o.__dict__)
o.foo = "bar"
o.foo = "baz"
del o.foo
self.assert_events(["new:foo:bar", "mod:foo:baz", "del:foo"])

with self.watcher() as wid:
self.watch(wid, o.__dict__)
for _ in range(100):
o.foo = "bar"
self.assert_events(["new:foo:bar"] + ["mod:foo:bar"] * 99)

def test_unwatch(self):
d = {}
with self.watcher() as wid:
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a bug where dictionary watchers (e.g., :c:func:`PyDict_Watch`) on an
object's attribute dictionary (:attr:`~object.__dict__`) were not triggered
when the object's attributes were modified.
21 changes: 15 additions & 6 deletions Objects/dictobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6835,15 +6835,24 @@ store_instance_attr_lock_held(PyObject *obj, PyDictValues *values,
}

PyObject *old_value = values->values[ix];
if (old_value == NULL && value == NULL) {
PyErr_Format(PyExc_AttributeError,
"'%.100s' object has no attribute '%U'",
Py_TYPE(obj)->tp_name, name);
return -1;
}

if (dict) {
PyInterpreterState *interp = _PyInterpreterState_GET();
PyDict_WatchEvent event = (old_value == NULL ? PyDict_EVENT_ADDED :
value == NULL ? PyDict_EVENT_DELETED :
PyDict_EVENT_MODIFIED);
_PyDict_NotifyEvent(interp, event, dict, name, value);
}

FT_ATOMIC_STORE_PTR_RELEASE(values->values[ix], Py_XNewRef(value));

if (old_value == NULL) {
if (value == NULL) {
PyErr_Format(PyExc_AttributeError,
"'%.100s' object has no attribute '%U'",
Py_TYPE(obj)->tp_name, name);
return -1;
}
_PyDictValues_AddToInsertionOrder(values, ix);
if (dict) {
assert(dict->ma_values == values);
Expand Down

Back | FazBrowse Home | New Git URL