GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • Lib/test/test_capi
    • test_watchers.py
  • Misc/NEWS.d/next/C_API
    • 2024-10-16-19-28-23.gh-issue-125608.gTsU2g.rst
  • Objects
    • dictobject.c
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Lib/test/test_capi/test_watchers.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_capi/test_watchers.py
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ def test_dealloc(self):
9797
del d
9898
self.assert_events(["dealloc"])
9999

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+
100117
def test_unwatch(self):
101118
d = {}
102119
with self.watcher() as wid:
Collapse file

‎Misc/NEWS.d/next/C_API/2024-10-16-19-28-23.gh-issue-125608.gTsU2g.rst‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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

‎Objects/dictobject.c‎

Copy file name to clipboardExpand all lines: Objects/dictobject.c
+15-6Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6835,15 +6835,24 @@ store_instance_attr_lock_held(PyObject *obj, PyDictValues *values,
68356835
}
68366836

68376837
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+
68386853
FT_ATOMIC_STORE_PTR_RELEASE(values->values[ix], Py_XNewRef(value));
68396854

68406855
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-
}
68476856
_PyDictValues_AddToInsertionOrder(values, ix);
68486857
if (dict) {
68496858
assert(dict->ma_values == values);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.