[issue39686] add dump_json to ast module

Pablo Galindo Salgado report at bugs.python.org
Wed Feb 19 13:15:31 EST 2020


Pablo Galindo Salgado <pablogsal at gmail.com> added the comment:

> There seems to be movement towards a general usage. For instance, take a look at clang, in particular the flag '-ast-dump=json'.

I don't think the clang argument holds because clang is a command-line tool after all and it makes sense that it can produce several outputs while the ast module is exposes APIs that you can further process inside the language. Having json from the clang output will require more than one tool if clang does not support it while doing it in Python only requires Python.

> it appears that they do so in non-standard ways.

Can you clarify what do you mean with that? 

> The implementation of ast.dump also uses recursion. I have tested ast.dump_json on sufficiently large source files and have not run into recursion depth exceeded issues.

This is not the primary argumet as by itself is weaker because this is an edge case but for instance, here is an example of ast.dump succeeding and your tool failing:


>>> x = ast.List()
>>> for _ in range(1010):
    ...:     x = ast.List(x)
    ...:

>>> ast.dump(x)
'List(elts=List(elts=List(elts=List(elts=List(elts=L......

>>> dump_json(x)
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-22-fadef4fb6a0d> in <module>
RecursionError: maximum recursion depth exceeded while calling a Python object

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39686>
_______________________________________


More information about the Python-bugs-list mailing list