summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_weakref.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_weakref.py')
-rw-r--r--Lib/test/test_weakref.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index 4cdd66d..d6470fb 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -116,6 +116,33 @@ class ReferencesTestCase(TestBase):
del o
repr(wr)
+ @support.cpython_only
+ def test_ref_repr(self):
+ obj = C()
+ ref = weakref.ref(obj)
+ self.assertRegex(repr(ref),
+ rf"<weakref at 0x[0-9a-fA-F]+; "
+ rf"to '{C.__module__}.{C.__qualname__}' "
+ rf"at 0x[0-9a-fA-F]+>")
+
+ obj = None
+ gc_collect()
+ self.assertRegex(repr(ref),
+ rf'<weakref at 0x[0-9a-fA-F]+; dead>')
+
+ # test type with __name__
+ class WithName:
+ @property
+ def __name__(self):
+ return "custom_name"
+
+ obj2 = WithName()
+ ref2 = weakref.ref(obj2)
+ self.assertRegex(repr(ref2),
+ rf"<weakref at 0x[0-9a-fA-F]+; "
+ rf"to '{WithName.__module__}.{WithName.__qualname__}' "
+ rf"at 0x[0-9a-fA-F]+ \(custom_name\)>")
+
def test_repr_failure_gh99184(self):
class MyConfig(dict):
def __getattr__(self, x):
@@ -195,6 +222,20 @@ class ReferencesTestCase(TestBase):
self.assertRaises(ReferenceError, bool, ref3)
self.assertEqual(self.cbcalled, 2)
+ @support.cpython_only
+ def test_proxy_repr(self):
+ obj = C()
+ ref = weakref.proxy(obj, self.callback)
+ self.assertRegex(repr(ref),
+ rf"<weakproxy at 0x[0-9a-fA-F]+; "
+ rf"to '{C.__module__}.{C.__qualname__}' "
+ rf"at 0x[0-9a-fA-F]+>")
+
+ obj = None
+ gc_collect()
+ self.assertRegex(repr(ref),
+ rf'<weakproxy at 0x[0-9a-fA-F]+; dead>')
+
def check_basic_ref(self, factory):
o = factory()
ref = weakref.ref(o)