alexmojaki / pure_eval

Safely evaluate AST nodes without side effects
MIT License
42 stars 15 forks source link

TypeError for malformed metaclass example #11

Closed aroberge closed 2 years ago

aroberge commented 2 years ago

In https://github.com/ipython/ipython/issues/13481, the following example used to show a fatal error in IPython:

class X(type):
    def __prepare__(cls, *args, **kwargs):
        return []
class Y(metaclass=X):
    pass

If I try the same example with friendly-traceback, I also get a fatal error, with the following as part of a long traceback:

    During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "LOCAL:\pure_eval\core.py", line 445, in group_expressions
    for node, value in expressions:
  File "FRIENDLY:\info_variables.py", line 119, in <genexpr>
    for nodes, obj in group_expressions(
  File "LOCAL:\pure_eval\core.py", line 358, in find_expressions
    value = self[node]
  File "LOCAL:\pure_eval\core.py", line 68, in __getitem__
    self._cache[node] = result = self._handle(node)
  File "LOCAL:\pure_eval\core.py", line 89, in _handle
    return self.names[node.id]
TypeError: list indices must be integers or slices, not str

In https://github.com/friendly-traceback/friendly-traceback/commit/276ec1b85f7c5949b0e5d1fb325b30b59b57d9c5, I've guarded against this type of fatal error.

I didn't see any evidence that the IPython crash is caused by pure_eval or an other library of yours, but I thought you might want to know about it - and possibly include some safeguards in pure_eval.

alexmojaki commented 2 years ago

Thanks for the report! Released a fix in 0.2.2.