def macro(name, filename, source, **identifiers):
"""macro(name, source, **identifiers)
This allows you to create macro-like behaviors in python. See
twisted.python.hook for an example of its usage.
"""
if not identifiers.has_key('name'):
identifiers['name'] = name
source = source % identifiers
codeplace = "<%s (macro)>" % filename
code = compile(source, codeplace, 'exec')
# shield your eyes!
sm = sys.modules
tprm = "twisted.python.reflect.macros"
if not sm.has_key(tprm):
macros = new.module(tprm)
sm[tprm] = macros
macros.count = 0
macros = sm[tprm]
macros.count += 1
macroname = 'macro_' + str(macros.count)
tprmm = tprm + '.' + macroname
mymod = new.module(tprmm)
sys.modules[tprmm] = mymod
setattr(macros, macroname, mymod)
dict = mymod.__dict__
# Before we go on, I guess I should explain why I just did that. Basically
# it's a gross hack to get epydoc to work right, but the general idea is
# that it will be a useful aid in debugging in _any_ app which expects
# sys.modules to have the same globals as some function. For example, it
# would be useful if you were foolishly trying to pickle a wrapped function
# directly from a class that had been hooked.
exec code in dict, dict
return dict[name]
评论列表
文章目录