[Python-ideas] AST Transformation Hooks for Domain Specific Languages

Nick Coghlan ncoghlan at gmail.com
Sun Apr 10 09:37:40 CEST 2011


On Sun, Apr 10, 2011 at 8:13 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> I was thinking Eric meant function call time, but that is probably wrong, so
> ignore my comment.

Yeah, he was talking about function definition time rather than call
time. From the point-of-view of PYC file generation, both function
definition time and function call time are "runtime" events that
happen after the PYC file has been generated.

The typical flow of events is...

- module compilation (including PYC caching)
- module execution (including definition of top-level functions and classes)
- function execution (including definition of any nested functions and classes)

The latter two are both "runtime", but functions are special because
key events happens at all three stages in the chain:

- At compilation time, the compiler symbol table analysis and code
generation figures out all the variable scoping and decides whether to
emit local, nonlocal or global operations for variable access, and
which variables need to be stored in cells for correct access from
closures
- At function definition time, default arguments, annotations and
decorators are all evaluated, any cell references are linked up to the
relevant outer scopes and the function name is bound in the containing
scope
- At function call time, the arguments are populated and the actual
function body is executed

Generators add a fourth stage into the process, since they separate
generator construction time from iteration time (and don't check their
arguments until they start iterating).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list