[Python-Dev] Enable access to the AST for Python code

Guido van Rossum guido at python.org
Thu May 21 04:31:13 CEST 2015


Hey Ben, this is probably a better topic for python-ideas. I'll warn you
that a hurdle for ideas like this is that ideally you don't want to support
this just for CPython. It's definitely cool though! (Using movie poster
style quotes you can turn this into a ringing endorsement: "definitely
cool" -- The BDFL. :-)

On Wed, May 20, 2015 at 7:26 PM, Ben Hoyt <benhoyt at gmail.com> wrote:

> Hi Python devs,
>
> Enabling access to the AST for compiled code would make some cool
> things possible (C# LINQ-style ORMs, for example), and not knowing too
> much about this part of Python internals, I'm wondering how possible
> and practical this would be.
>
> Context: PonyORM (http://ponyorm.com/) allows you to write regular
> Python generator expressions like this:
>
>     select(c for c in Customer if sum(c.orders.price) > 1000)
>
> which compile into and run SQL like this:
>
>     SELECT "c"."id"
>     FROM "Customer" "c"
>     LEFT JOIN "Order" "order-1" ON "c"."id" = "order-1"."customer"
>     GROUP BY "c"."id"
>     HAVING coalesce(SUM("order-1"."total_price"), 0) > 1000
>
> I think the Pythonic syntax here is beautiful. But the tricks PonyORM
> has to go to get it are ... not quite so beautiful. Because the AST is
> not available, PonyORM decompiles Python bytecode into an AST first,
> and then converts that to SQL. (More details on all that from author's
> EuroPython talk at http://pyvideo.org/video/2968)
>
> I believe PonyORM needs the AST just for generator expressions and
> lambda functions, but obviously if this kind of AST access feature
> were in Python it'd probably be more general.
>
> I believe C#'s LINQ provides something similar, where if you're
> developing a LINQ converter library (say LINQ to SQL), you essentially
> get the AST of the code ("expression tree") and the library can do
> what it wants with that.
>
> What would it take to enable this kind of AST access in Python? Is it
> possible? Is it a good idea?
>
> -Ben
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/guido%40python.org
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150520/dce3f816/attachment.html>


More information about the Python-Dev mailing list