Skip to content

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

Open
@XuehaiPan

Description

@XuehaiPan

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()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions