]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-99184: Bypass instance attribute access in `repr` of `weakref.ref` (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 24 Apr 2023 19:58:55 +0000 (12:58 -0700)
committerGitHub <noreply@github.com>
Mon, 24 Apr 2023 19:58:55 +0000 (19:58 +0000)
gh-99184: Bypass instance attribute access in `repr` of `weakref.ref` (GH-99244)
(cherry picked from commit 58b6be3791f55ceb550822ffd8664eca10fd89c4)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
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 d26fc9e8d09d71c29992767f029ed5581efa3ff5..c76c92843cbcb1bcc1a6beb8bb0bad16d20d20b3 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>",