Macro expansion: intercept statement interpretation

Benjamin Niemann pink at odahoda.de
Tue Aug 24 18:07:22 EDT 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 http://docs.python.org/lib/examples-imp.html 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