Skip to content
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

[BUG] console.print failed to render weakref.proxy objects #3656

Open
2 tasks done
XuehaiPan opened this issue Mar 12, 2025 · 2 comments · May be fixed by #3684
Open
2 tasks done

[BUG] console.print failed to render weakref.proxy objects #3656

XuehaiPan opened this issue Mar 12, 2025 · 2 comments · May be fixed by #3684

Comments

@XuehaiPan
Copy link
Contributor

Describe the bug

Edit this with a clear and concise description of what the bug.

Provide a minimal code example that demonstrates the issue if you can. If the issue is visual in nature, consider posting a screenshot.

A weakref.proxy object can be used as the wrapped object when the object is not GC-ed. Then the object is finalized, the accessing the attributes of the weakref.proxy object will raise RefereenceError.

The weakref.proxy type has a proper __repr__ to handle the dead reference but rich traverse tries to inspec the proxy object and that will raise errors when the referenced object is gone.

In [1]: import weakref

In [2]: import rich

In [3]: class Foo:
   ...:     pass
   ...:     

In [4]: rich.print(weakref.ref(Foo()))
<weakref at 0x7f98d28b50d0; dead>

In [5]: rich.print(weakref.proxy(Foo()))
ReferenceError: weakly-referenced object no longer exists

In [6]: repr(weakref.ref(Foo()))
Out[6]: '<weakref at 0x7f98d0a80540; dead>'

In [7]: repr(weakref.proxy(Foo()))
Out[7]: '<weakproxy at 0x7f98d0a12070 to NoneType at 0x7f98d6516120>'

Platform

Click to expand

What platform (Win/Linux/Mac) are you running on? What terminal software are you using?

I may ask you to copy and paste the output of the following commands. It may save some time if you do it now.

If you're using Rich in a terminal:

python -m rich.diagnose
pip freeze | grep rich

If you're using Rich in a Jupyter Notebook, run the following snippet in a cell
and paste the output in your bug report.

from rich.diagnose import report
report()
Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
@XuehaiPan and others