GAST, daou naer !

Une implémentation portable et minimaliste du module ast

Délicatement assemblé sur Namek par serge-sans-paille

ast

  • 2.6
  • 2.7
  • 3.1
  • 3.2
  • 3.3
  • 3.4
  • 3.5

Gast!

/me

Serge « sans paille » Guelton

$ whoami
sguelton
  • Ingénieur R&D à QuarksLab en Compil' appliquée à la Sécu'
  • Chercheur associé à Télécom Bretagne
  • Co-Auteur du compilo pythran pour le calcul scientifique en Python

import ast

>>> import ast
>>> tree = ast.parse("'galette' + 'saucisse'")
>>> ast.dump(tree)
Module(body=[Expr(value=BinOp(left=Str(s='galette'),
... op=Add(), right=Str(s='saucisse')))])

Vu d'un arbre

Module
    Expr
        BinOp
            Str(s='galette')
            Add()
            Str(s='saucisse')

Problème à l'affichage

Python 2

(python2) >>> import ast
(python2) >>> tree = ast.parse("'print('yaouankiz')")
(python2) >>> ast.dump(tree)
Module(body=[Print(dest=None, values=[Str(s='yaouankiz')], nl=True)])
               

Python 3

(python3) >>> import ast
(python3) >>> tree = ast.parse("'print('yaouankiz')")
(python3) >>> ast.dump(tree)
Module(body=[Expr(value=Call(func=Name(id='print', ctx=Load()),
... "args=[Str(s='yaouankiz')], keywords=[], starargs=None, kwargs=None))])
               

Arbreizh Aventure

parcours d'arbre

class PP(ast.NodeVisitor):

    def __init__(self):
        self.depth = 0

    def generic_visit(self, node):
        print('{}{}'.format(' ' * self.depth, type(node).__name__))
        self.depth += 1
        super(PP, self).generic_visit(node)
        self.depth -= 1

Les visiteurs

ils ne sont pas nés d'hier

def visit(self, node):
    """Visit a node."""
    method = 'visit_' + node.__class__.__name__
    visitor = getattr(self, method, self.generic_visit)
    return visitor(node)
def generic_visit(self, node):
    """Called if no explicit visitor function exists for a node."""
    for field, value in iter_fields(node):
        if isinstance(value, list):
            for item in value:
                if isinstance(item, AST):
                    self.visit(item)
        elif isinstance(value, AST):
            self.visit(value)

Va Doué !

Comment parcourir indifférement des AST 2 et 3 ?

  • Nœuds ajoutés e.g. YieldFrom ou NameConstant
  • Nœuds supprimés e.g. Print ou TryFinally
  • Nœuds modifiés e.g. FunctionDef gagne un champs returns et ClassDef gagne un champs keywords

Fuuuuusion

  • Nœuds ajoutés : On les ajoute !
  • Nœuds supprimés : On les garde !
  • Nœuds modifiés : On les fusionne ! (pas tjrs facile)

Deux convertisseurs :

  • ast (python2) => gast
  • ast (python3) => gast

Interlude

Q & R

Q : Pourquoi ne pas convertire le nœud ast.Print en ast.Call ? Après tout, c'est pareil !

R : Parce que print = str et c'est l'enfer

Quizz

Comment se comporte (en Python 2):

code = '''
from __future__ import print_function
print(1)'''
ast.dump(ast.parse(code))

Bihan

Traduction automatique

def _generate_translators(to):

    class Translator(ast.NodeTransformer):
        'stuff that matters'
        # ...

    return Translator

AstToGAst = _generate_translators(gast)
GAstToAst = _generate_translators(ast)

Petit concentré d'introspection

def generic_visit(self, node):
    cls = type(node).__name__
    new_node = getattr(to, cls)()  # default constructor

    # generate fields recursively
    for field in node._fields:
        setattr(new_node, field,
                self._visit(getattr(node, field)))

    # copy any attributes
    for attr in getattr(node, '_attributes'):
        if hasattr(node, attr):
            setattr(new_node, attr,
                    getattr(node, attr))

    # job done!
    return new_node

Spécialisation

class Ast2ToGAst(AstToGAst):

    # stmt
    def visit_FunctionDef(self, node):
        new_node = gast.FunctionDef(
            self._visit(node.name),
            self._visit(node.args),
            self._visit(node.body),
            self._visit(node.decorator_list),
            None,  # returns
        )
        ast.copy_location(new_node, node)
        return new_node

Un cas exceptionnel

En Python 2

raise Except, Val, Tbk

En Python 3

raise Except(Val).with_traceback(Tbk)

Ce qui est faux si un zozo surcharge mal (ou dégage) with_traceback

Différences d'API

  • Toutes les fonctions/classes de ast sont dans gast
  • Mais elles ne fonctionnent que sur des arbres gast
  • gast.gast_to_ast et gast.ast_to_gast font ce que leur nom les prédispose à faire
  • gast.parse est équivalent à gast.ast_to_gast(ast.parse(*args))

Doc

Vous reprendrez bien un petit peu de grammaire?

module Python
{
    mod = Module(stmt* body)
        | Interactive(stmt* body)
        | Expression(expr body)

        -- not really an actual node but useful in Jython's typesystem.
        | Suite(stmt* body)

    stmt = FunctionDef(identifier name, arguments args,
                       stmt* body, expr* decorator_list, expr? returns)
          | AsyncFunctionDef(identifier name, arguments args,
                             stmt* body, expr* decorator_list, expr? returns)

          | ClassDef(identifier name,
             expr* bases,
             keyword* keywords,
             stmt* body,
             expr* decorator_list)
          | Return(expr? value)

          | Delete(expr* targets)
          | Assign(expr* targets, expr value)
          | AugAssign(expr target, operator op, expr value)

          -- not sure if bool is allowed, can always use int
          | Print(expr? dest, expr* values, bool nl)

          -- use 'orelse' because else is a keyword in target languages
          | For(expr target, expr iter, stmt* body, stmt* orelse)
          | AsyncFor(expr target, expr iter, stmt* body, stmt* orelse)
          | While(expr test, stmt* body, stmt* orelse)
          | If(expr test, stmt* body, stmt* orelse)
          | With(withitem* items, stmt* body)
          | AsyncWith(withitem* items, stmt* body)

          | Raise(expr? exc, expr? cause)
          | Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
          | Assert(expr test, expr? msg)

          | Import(alias* names)
          | ImportFrom(identifier? module, alias* names, int? level)

          | Global(identifier* names)
          | Nonlocal(identifier* names)
          | Expr(expr value)
          | Pass | Break | Continue

          -- XXX Jython will be different
          -- col_offset is the byte offset in the utf8 string the parser uses
          attributes (int lineno, int col_offset)

          -- BoolOp() can use left & right?
    expr = BoolOp(boolop op, expr* values)
         | BinOp(expr left, operator op, expr right)
         | UnaryOp(unaryop op, expr operand)
         | Lambda(arguments args, expr body)
         | IfExp(expr test, expr body, expr orelse)
         | Dict(expr* keys, expr* values)
         | Set(expr* elts)
         | ListComp(expr elt, comprehension* generators)
         | SetComp(expr elt, comprehension* generators)
         | DictComp(expr key, expr value, comprehension* generators)
         | GeneratorExp(expr elt, comprehension* generators)
         -- the grammar constrains where yield expressions can occur
         | Await(expr value)
         | Yield(expr? value)
         | YieldFrom(expr value)
         -- need sequences for compare to distinguish between
         -- x < 4 < 3 and (x < 4) < 3
         | Compare(expr left, cmpop* ops, expr* comparators)
         | Call(expr func, expr* args, keyword* keywords)
         | Num(object n) -- a number as a PyObject.
         | Str(string s) -- need to specify raw, unicode, etc?
         | Bytes(bytes s)
         | NameConstant(singleton value)
         | Ellipsis

         -- the following expression can appear in assignment context
         | Attribute(expr value, identifier attr, expr_context ctx)
         | Subscript(expr value, slice slice, expr_context ctx)
         | Starred(expr value, expr_context ctx)
         | Name(identifier id, expr_context ctx, expr? annotation)
         | List(expr* elts, expr_context ctx)
         | Tuple(expr* elts, expr_context ctx)

          -- col_offset is the byte offset in the utf8 string the parser uses
          attributes (int lineno, int col_offset)

    expr_context = Load | Store | Del | AugLoad | AugStore | Param

    slice = Slice(expr? lower, expr? upper, expr? step)
          | ExtSlice(slice* dims)
          | Index(expr value)

    boolop = And | Or

    operator = Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift
                 | RShift | BitOr | BitXor | BitAnd | FloorDiv

    unaryop = Invert | Not | UAdd | USub

    cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn

    comprehension = (expr target, expr iter, expr* ifs)

    excepthandler = ExceptHandler(expr? type, expr? name, stmt* body)
                    attributes (int lineno, int col_offset)

    arguments = (expr* args, expr? vararg, expr* kwonlyargs, expr* kw_defaults,
                 expr? kwarg, expr* defaults)

    -- keyword arguments supplied to call (NULL identifier for **kwargs)
    keyword = (identifier? arg, expr value)

    -- import name with optional 'as' alias.
    alias = (identifier name, identifier? asname)

    withitem = (expr context_expr, expr? optional_vars)
}

Application : Compil@eur

Permet d'écrire des analysuers de code Python portable :

Pythran
un optimiseur pour Python scientifique
Tog
un POC d'inférence de type

Et pourquoi pas asteroids, bandit, astoptimizer

Un pok pour tox

car tox est a(r)mo(u)r

[tox]
envlist = py27,py30,py31,py32,py33,py34,py35
[testenv]
deps=pytest-pep8
commands=py.test --pep8

Petit challenge : compiler une version fonctionnelle de chacun de ses interpreteurs

Trugarez

pip install gast
https://github.com/serge-sans-paille/gast

♥ Logilab ♥ pour avoir soufflé l'idée (ils embauchent)

♥ QuarksLab ♥ pour la liberté (on embauche)

☺ PyConFr ☺ & l'orga pour avoir choisi Roazhon !