Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c8de883

Browse filesBrowse files
[3.11] gh-99184: Bypass instance attribute access in repr of weakref.ref (GH-99244) (#103789)
gh-99184: Bypass instance attribute access in `repr` of `weakref.ref` (GH-99244) (cherry picked from commit 58b6be3) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent a8489f7 commit c8de883
Copy full SHA for c8de883

File tree

3 files changed

+14
-4
lines changed
Filter options

3 files changed

+14
-4
lines changed

‎Lib/test/test_weakref.py

Copy file name to clipboardExpand all lines: Lib/test/test_weakref.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ def test_basic_ref(self):
116116
del o
117117
repr(wr)
118118

119+
def test_repr_failure_gh99184(self):
120+
class MyConfig(dict):
121+
def __getattr__(self, x):
122+
return self[x]
123+
124+
obj = MyConfig(offset=5)
125+
obj_weakref = weakref.ref(obj)
126+
127+
self.assertIn('MyConfig', repr(obj_weakref))
128+
self.assertIn('MyConfig', str(obj_weakref))
129+
119130
def test_basic_callback(self):
120131
self.check_basic_callback(C)
121132
self.check_basic_callback(create_function)
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Bypass instance attribute access of ``__name__`` in ``repr`` of
2+
:class:`weakref.ref`.

‎Objects/weakrefobject.c

Copy file name to clipboardExpand all lines: Objects/weakrefobject.c
+1-4Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,7 @@ weakref_repr(PyWeakReference *self)
170170
}
171171

172172
Py_INCREF(obj);
173-
if (_PyObject_LookupAttr(obj, &_Py_ID(__name__), &name) < 0) {
174-
Py_DECREF(obj);
175-
return NULL;
176-
}
173+
name = _PyObject_LookupSpecial(obj, &_Py_ID(__name__));
177174
if (name == NULL || !PyUnicode_Check(name)) {
178175
repr = PyUnicode_FromFormat(
179176
"<weakref at %p; to '%s' at %p>",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.