gh-99184: Bypass instance attribute access in `repr` of `weakref.ref` (#99244)

This commit is contained in:
Nikita Sobolev 2023-04-24 21:57:36 +03:00 committed by GitHub
parent 2aa22f72fb
commit 58b6be3791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 4 deletions

View File

@ -116,6 +116,17 @@ class ReferencesTestCase(TestBase):
del o del o
repr(wr) repr(wr)
def test_repr_failure_gh99184(self):
class MyConfig(dict):
def __getattr__(self, x):
return self[x]
obj = MyConfig(offset=5)
obj_weakref = weakref.ref(obj)
self.assertIn('MyConfig', repr(obj_weakref))
self.assertIn('MyConfig', str(obj_weakref))
def test_basic_callback(self): def test_basic_callback(self):
self.check_basic_callback(C) self.check_basic_callback(C)
self.check_basic_callback(create_function) self.check_basic_callback(create_function)

View File

@ -0,0 +1,2 @@
Bypass instance attribute access of ``__name__`` in ``repr`` of
:class:`weakref.ref`.

View File

@ -170,10 +170,7 @@ weakref_repr(PyWeakReference *self)
} }
Py_INCREF(obj); Py_INCREF(obj);
if (_PyObject_LookupAttr(obj, &_Py_ID(__name__), &name) < 0) { name = _PyObject_LookupSpecial(obj, &_Py_ID(__name__));
Py_DECREF(obj);
return NULL;
}
if (name == NULL || !PyUnicode_Check(name)) { if (name == NULL || !PyUnicode_Check(name)) {
repr = PyUnicode_FromFormat( repr = PyUnicode_FromFormat(
"<weakref at %p; to '%s' at %p>", "<weakref at %p; to '%s' at %p>",