[Python-ideas] Retain string form of AST Numbers

Guido van Rossum guido at python.org
Sun Apr 6 00:08:38 CEST 2014


Lib2to3 has its own parser and tree so it can reconstruct the file, with
comments. AST JUST THROWS AWAY TOO MUCH.

On Saturday, April 5, 2014, Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, Apr 6, 2014 at 3:39 AM, Serhiy Storchaka <storchaka at gmail.com<javascript:;>>
> wrote:
> > What about strings?
> >
> >>>> print(ast.dump(ast.parse(r"x = '''\x0a\u000d''' r'x'")))
> > Module(body=[Assign(targets=[Name(id='x', ctx=Store())],
> > value=Str(s='\n\rx'))])
> >
> > Tuples?
> >
> >>>> print(ast.dump(ast.parse("x = (1, 2,)")))
> > Module(body=[Assign(targets=[Name(id='x', ctx=Store())],
> > value=Tuple(elts=[Num(n=1), Num(n=2)], ctx=Load()))])
>
> I don't have any concrete use cases for those, but the same could be
> done for every type of node as a general mechanism for recreating a
> file more exactly. Nick linked to a tracker issue with some discussion
> on docstrings, which could benefit from that. Or maybe it would be
> better to avoid the AST altogether and do a source-level translation,
> as mentioned in the blog post he also linked to.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org <javascript:;>
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (on iPad)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140405/5ec202a6/attachment.html>


More information about the Python-ideas mailing list