Skip to content

Draft: Attempt to fix symbol display infinite loop

finesse importer requested to merge fix/symbol-display into master

I subclassed Symbol in the packrat branch:

class Resolving(Symbol):
    """A special symbol that represents a symbol that is not yet resolved.

    This is used in the parser to support self-referencing parameters.

    An error is thrown if the value is attempted to be read.
    """
    def eval(self):
        raise RuntimeError(
            "an attempt has been made to read the value of a resolving symbol (hint: symbols "
            "should not evaluated until parsing has fully finished)"
        )

But when I call str(x) where x = Resolving(), I get an infinite loop because Symbol.__str__ calls display(self) but display then calls str on self and so on. This is because Resolving lacks an op or name attribute, and display therefore falls back on calling str of Resolving again:

def display(a):
    if hasattr(a, "op"):
        ...
    elif hasattr(a, "name"):  # Anything with a name attribute just display that
        return a.name
    else:
        return str(a)   # <---- Infinite loop here!

I tried changing the last line to use repr instead, and removed Symbol.__repr__ (which was also pointing to display) so that it would fall back to object.__repr__, but this makes a bunch of apparently unrelated tests fail (see the failed pipeline). Before proceeding I thought I'd make this MR to discuss with others how best to fix this. We could for instance require that Symbol subclasses have a name, then get rid of the str(a) part of display altogether.

Merge request reports