def callStackCatcher(*allowed_callers): def _callStackCatcher(function): def catcher(*args, **kwargs): import inspect caller = inspect.getouterframes(inspect.currentframe())[1][3] if caller not in allowed_callers: raise RuntimeError("Caller %r is not allowed to call %r: %r" % (caller, function, allowed_callers)) function(*args, **kwargs) return catcher return _callStackCatcher