nodes: prevent infinite recursion in repr
This commit is contained in:
parent
0c42eabde1
commit
c656a98d3e
1 changed files with 5 additions and 0 deletions
|
|
@ -18,6 +18,7 @@ class Node:
|
|||
|
||||
def __init__(self):
|
||||
self.context: semantic.Context | None = None
|
||||
self._repr_guard: bool = False
|
||||
|
||||
@abstractmethod
|
||||
def _values(self) -> list[Node | Any]:
|
||||
|
|
@ -32,11 +33,15 @@ class Node:
|
|||
return loc
|
||||
|
||||
def __repr__(self):
|
||||
if self._repr_guard:
|
||||
return self.__class__.__name__
|
||||
self._repr_guard = True
|
||||
vals = self._values()
|
||||
if type(vals) == list:
|
||||
vals = ", ".join(repr(val) for val in vals)
|
||||
else:
|
||||
vals = repr(vals)
|
||||
self._repr_guard = False
|
||||
return f"{self.__class__.__name__}({vals})"
|
||||
|
||||
def pprint(self, depth: int | None = None, indent: str = "\t"):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue