@contextmanager
def func_globals_inject(func, **overrides):
 if hasattr(func, 'im_func'):
  func = func.__func__
 func_globals = func.__globals__
 injected_func_globals = []
 overridden_func_globals = {}
 for override in overrides:
  if (override in func_globals):
   overridden_func_globals[override] = func_globals[override]
  else:
   injected_func_globals.append(override)
 func_globals.update(overrides)
 (yield)
 func_globals.update(overridden_func_globals)
 for injected in injected_func_globals:
  del func_globals[injected]

@ contextmanager def func_globals_inject ( func , ** overrides ) : if hasattr ( func , 'im_func' ) : func = func . __func__ func_globals = func . __globals__ injected_func_globals = [ ] overridden_func_globals = { } for override in overrides : if ( override in func_globals ) : overridden_func_globals [ override ] = func_globals [ override ] else : injected_func_globals . append ( override ) func_globals . update ( overrides ) ( yield ) func_globals . update ( overridden_func_globals ) for injected in injected_func_globals : del func_globals [ injected ]
override specific variables within a functions global context .