def caller_module(level=2, sys=sys):
    """Return the module object from which the call was made. For instance, if
    A defined in module M makes a call to B and B, by calling this function,
    can know the module of the caller."""
    module_globals = sys._getframe(level).f_globals
    module_name = module_globals.get('__name__') or '__main__'
    module = sys.modules[module_name]
    return module
