gaogaotiantian / objprint

A library that can print Python objects in human readable format
Apache License 2.0
510 stars 43 forks source link

objstr and cyclic object graphs #105

Closed adrian-herscu closed 1 month ago

adrian-herscu commented 1 month ago

It seems that cyclic object graphs cause an infine recursion.

from objprint import *

config(skip_recursion=True)

class Foo:
    f1: int = 2
    f2: str = "kuku"
    f3: 'Foo'

    def __init__(self):
        self.f3 = Foo()

print(objstr(Foo()))

Output:

Traceback (most recent call last):
  File "/home/adrian-herscu/projects/pam-2/tests/python/scratch.py", line 15, in <module>
    print(objstr(Foo()))
                 ^^^^^
  File "/home/adrian-herscu/projects/pam-2/tests/python/scratch.py", line 12, in __init__
    self.f3 = Foo()
              ^^^^^
  File "/home/adrian-herscu/projects/pam-2/tests/python/scratch.py", line 12, in __init__
    self.f3 = Foo()
              ^^^^^
  File "/home/adrian-herscu/projects/pam-2/tests/python/scratch.py", line 12, in __init__
    self.f3 = Foo()
              ^^^^^
  [Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
gaogaotiantian commented 1 month ago

There are configurations to skip recursion or set the depth limit.

adrian-herscu commented 1 month ago

There are configurations to skip recursion or set the depth limit.

Updated description with example.

gaogaotiantian commented 1 month ago

Updated description with example.

Your example has nothing to do with objprint. That is a real infinite recursion. print(Foo()) will give you the same thing.

adrian-herscu commented 1 month ago

that's what happens after too many hours :) closing