Skip to content

gh-123046: Fix regexp to catch cases where the module name is omitted from the weakref repr #123047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ def test_basic_ref(self):
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]+>")
regex = (
rf"<weakref at 0x[0-9a-fA-F]+; "
rf"to '{'' if __name__ == '__main__' else C.__module__ + '.'}{C.__qualname__}' "
rf"at 0x[0-9a-fA-F]+>"
)
self.assertRegex(repr(ref), regex)

obj = None
gc_collect()
Expand All @@ -141,10 +143,13 @@ def __name__(self):

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\)>")
regex = (
rf"<weakref at 0x[0-9a-fA-F]+; "
rf"to '{'' if __name__ == '__main__' else WithName.__module__ + '.'}"
rf"{WithName.__qualname__}' "
rf"at 0x[0-9a-fA-F]+ +\(custom_name\)>"
)
self.assertRegex(repr(ref2), regex)

def test_repr_failure_gh99184(self):
class MyConfig(dict):
Expand Down Expand Up @@ -229,10 +234,12 @@ def check(proxy):
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]+>")
regex = (
rf"<weakproxy at 0x[0-9a-fA-F]+; "
rf"to '{'' if __name__ == '__main__' else C.__module__ + '.'}{C.__qualname__}' "
rf"at 0x[0-9a-fA-F]+>"
)
self.assertRegex(repr(ref), regex)

obj = None
gc_collect()
Expand Down
Loading