MCPcopy Index your code
hub / github.com/python/cpython / test_ref_repr

Method test_ref_repr

Lib/test/test_weakref.py:123–152  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

121
122 @support.cpython_only
123 def test_ref_repr(self):
124 obj = C()
125 ref = weakref.ref(obj)
126 regex = (
127 rf"<weakref at 0x[0-9a-fA-F]+; "
128 rf"to '{'' if __name__ == '__main__' else C.__module__ + '.'}{C.__qualname__}' "
129 rf"at 0x[0-9a-fA-F]+>"
130 )
131 self.assertRegex(repr(ref), regex)
132
133 obj = None
134 gc_collect()
135 self.assertRegex(repr(ref),
136 rf'<weakref at 0x[0-9a-fA-F]+; dead>')
137
138 # test type with __name__
139 class WithName:
140 @property
141 def __name__(self):
142 return "custom_name"
143
144 obj2 = WithName()
145 ref2 = weakref.ref(obj2)
146 regex = (
147 rf"<weakref at 0x[0-9a-fA-F]+; "
148 rf"to '{'' if __name__ == '__main__' else WithName.__module__ + '.'}"
149 rf"{WithName.__qualname__}' "
150 rf"at 0x[0-9a-fA-F]+ +\(custom_name\)>"
151 )
152 self.assertRegex(repr(ref2), regex)
153
154 def test_repr_failure_gh99184(self):
155 class MyConfig(dict):

Callers

nothing calls this directly

Calls 4

gc_collectFunction · 0.90
WithNameClass · 0.85
assertRegexMethod · 0.80
CClass · 0.70

Tested by

no test coverage detected