Macro expansion: intercept statement interpretation

Benjamin Niemann pink at odahoda.de
Wed Aug 25 00:00:18 CEST 2004


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...



More information about the Python-list mailing list