brython-dev / brython

Brython (Browser Python) is an implementation of Python 3 running in the browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get KeyError exception while traversing AST and handling deprecation warnings

shlomil opened this issue · comments

Brython Version: 3.12.3.
While running the code below i get the following exception:

...
  File VFS.ast.py, line 517, in _n_getter
    warnings._deprecated(
  File VFS.warnings.py, line 524, in _deprecated
    if(_version[:2]>remove)or(_version[:2]==remove and _version[3]!="alpha"):
  File VFS.sys.py, line 16, in __getitem__
    raise KeyError(key)
KeyError: slice(None, 2, None)

The code:

code = """class A:..."""

def ast_to_dict(obj):
    if isinstance(obj, (int, float, str, bool)):
        return obj
    elif isinstance(obj, list):
        return [ast_to_dict(item) for item in obj]
    elif isinstance(obj, dict):
        return {key: ast_to_dict(value) for key, value in obj.items()}
    elif isinstance(obj, object):
        result = {}
        for attr_name in dir(obj):
            if (not callable(getattr(obj, attr_name))
                    and not attr_name.startswith("__") and
                    attr_name not in ['_attributes', "_fields"]):
                result[attr_name] = ast_to_dict(getattr(obj, attr_name))
        return result
    else:
        raise f"unhandled type {type(obj).name}"
        
        
import ast
_ast = ast.parse(code)
ast_to_dict(_ast)

It seems to have something to do with deprecation warnings.
Bug does not occur in Brython 3.12.0. It started appearing in the version 3.12.1.

Online exmaple can be found here (link to brython editor):
https://tinyurl.com/3r5dutwj

Thanks for the report @shlomil !