berkerpeksag / astor

Python AST read/write
https://pypi.org/project/astor/
BSD 3-Clause "New" or "Revised" License
810 stars 102 forks source link

Support for Python 3.6 Constant nodes #120

Closed chrisrink10 closed 5 years ago

chrisrink10 commented 6 years ago

In Python 3.6, the new ast.Constant node was introduced, ostensibly for the purposes of constant folding. However, in Python 3.8, it appears that it is being used to replace ast.Str, ast.Bytes, ast.Num, ast.Ellipsis, and ast.NameConstant.

Since astor doesn't include a visit_Constant method on its SourceGenerator, attempting to emit code generated with ast.Constant causes a lot of errors.

jmfrank63 commented 5 years ago

I just ran into this problem. Here is the error message on Travis CI (only fails for python 3.8-dev) just in case this might be helpful:

    def test_get_module_trees_by_dir(mod_path, source, non_pub_source):
        modules = get_module_trees_by_dir(mod_path)
        assert len(modules) == 2
>       sources = [astor.to_source(mod) for mod in modules]
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/trio/tests/tools/test_gen_exports.py:130: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/trio/tests/tools/test_gen_exports.py:130: in <listcomp>
    sources = [astor.to_source(mod) for mod in modules]
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/code_gen.py:52: in to_source
    generator.visit(node)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/node_util.py:143: in visit
    return visitor(node)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/code_gen.py:802: in visit_Module
    self.write(*node.body)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/code_gen.py:168: in write
    visit(item)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/node_util.py:143: in visit
    return visitor(node)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/code_gen.py:320: in visit_FunctionDef
    self.body(node.body)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/code_gen.py:218: in body
    self.write(*statements)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/code_gen.py:168: in write
    visit(item)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/node_util.py:143: in visit
    return visitor(node)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/code_gen.py:310: in visit_Expr
    self.generic_visit(node)
/opt/python/3.8-dev/lib/python3.8/ast.py:352: in generic_visit
    self.visit(value)
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/node_util.py:143: in visit
    return visitor(node)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
node = <_ast.Constant object at 0x7fcd26a5efd0>
    def abort_visit(node):  # XXX: self?
        msg = 'No defined handler for node of type %s'
>       raise AttributeError(msg % node.__class__.__name__)
E       AttributeError: No defined handler for node of type Constant
../../../../virtualenv/python3.8-dev/lib/python3.8/site-packages/astor/node_util.py:137: AttributeError