Macro expansion: intercept statement interpretation

Benjamin Niemann pink at
Wed Aug 25 00:07:22 CEST 2004

Benjamin Niemann wrote:

> You could try to override __import__ to first parse the source for macro 
> definitions and do the expansions. Usage would then be:
> import DoMagicWithImportModule
> import ModuleWithMacros
> (Your code wouldn't work, because python throws a SyntaxError long 
> before it even tries to execute 'import MyCustomMacroLib')
> The code at could be 
> extended to first read the contents of the module (after find_module) 
> and passes the expanded source (e.g. as a StringIO) to load_module.
> This has of course the problem (that many implementations of macro 
> expansion share) that line numbers in exception dumps have not much 
> relation to lines of the unexpanded code...
If this actually works, a nice application could be rapid prototyping of 
syntax extensions to python ('import this module and you can test and 
see why my decorator syntax is better than yours' ;)

More information about the Python-list mailing list