diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2023-04-24 18:57:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-24 18:57:36 (GMT) |
commit | 58b6be3791f55ceb550822ffd8664eca10fd89c4 (patch) | |
tree | 5bbe8d2c68d3227b33a1b81f1bef6a13b766aed4 | |
parent | 2aa22f72fbbabb4ca2a641c0546d25c45128c56f (diff) | |
download | cpython-58b6be3791f55ceb550822ffd8664eca10fd89c4.zip cpython-58b6be3791f55ceb550822ffd8664eca10fd89c4.tar.gz cpython-58b6be3791f55ceb550822ffd8664eca10fd89c4.tar.bz2 |
gh-99184: Bypass instance attribute access in `repr` of `weakref.ref` (#99244)
-rw-r--r-- | Lib/test/test_weakref.py | 11 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2022-11-08-12-36-25.gh-issue-99184.KIaqzz.rst | 2 | ||||
-rw-r--r-- | Objects/weakrefobject.c | 5 |
3 files changed, 14 insertions, 4 deletions
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 7c59207..1bc1d05 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -116,6 +116,17 @@ class ReferencesTestCase(TestBase): del o 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): self.check_basic_callback(C) self.check_basic_callback(create_function) diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-11-08-12-36-25.gh-issue-99184.KIaqzz.rst b/Misc/NEWS.d/next/Core and Builtins/2022-11-08-12-36-25.gh-issue-99184.KIaqzz.rst new file mode 100644 index 0000000..8007683 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-11-08-12-36-25.gh-issue-99184.KIaqzz.rst @@ -0,0 +1,2 @@ +Bypass instance attribute access of ``__name__`` in ``repr`` of +:class:`weakref.ref`. diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 5a3e49a..c1afe63 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -170,10 +170,7 @@ weakref_repr(PyWeakReference *self) } Py_INCREF(obj); - if (_PyObject_LookupAttr(obj, &_Py_ID(__name__), &name) < 0) { - Py_DECREF(obj); - return NULL; - } + name = _PyObject_LookupSpecial(obj, &_Py_ID(__name__)); if (name == NULL || !PyUnicode_Check(name)) { repr = PyUnicode_FromFormat( "<weakref at %p; to '%s' at %p>", |