Skip to content

Commit c8de883

Browse files
[3.11] pythongh-99184: Bypass instance attribute access in repr of weakref.ref (pythonGH-99244) (python#103789)
pythongh-99184: Bypass instance attribute access in `repr` of `weakref.ref` (pythonGH-99244) (cherry picked from commit 58b6be3) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent a8489f7 commit c8de883

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Diff for: Lib/test/test_weakref.py

+11
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)
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`.

Diff for: Objects/weakrefobject.c

+1-4
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)