]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99184: Bypass instance attribute access in `repr` of `weakref.ref` (#99244)
authorNikita Sobolev <mail@sobolevn.me>
Mon, 24 Apr 2023 18:57:36 +0000 (21:57 +0300)
committerGitHub <noreply@github.com>
Mon, 24 Apr 2023 18:57:36 +0000 (12:57 -0600)
Lib/test/test_weakref.py
Misc/NEWS.d/next/Core and Builtins/2022-11-08-12-36-25.gh-issue-99184.KIaqzz.rst [new file with mode: 0644]
Objects/weakrefobject.c

index 7c5920797d253806f02fde64f14efa10b1359407..1bc1d05f7daba9de0d8a0215a9f49837ef834a1a 100644 (file)
@@ -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 (file)
index 0000000..8007683
--- /dev/null
@@ -0,0 +1,2 @@
+Bypass instance attribute access of ``__name__`` in ``repr`` of
+:class:`weakref.ref`.
index 5a3e49a6fe45e33868ac735638e855ffb41f3eaf..c1afe63ecf66f62c67151d847386d1d8b8a1e2a6 100644 (file)
@@ -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>",