25 __all__ = [
"get_caller_name"]
29 """Get the name of the caller method. 31 Any item that cannot be determined (or is not relevant, e.g. a free 32 function has no class) is silently omitted, along with an 38 How many levels of stack to skip while getting caller name; 39 1 means "who calls me", 2 means "who calls my caller", etc. 44 Name of the caller as a string in the form ``module.class.method``. 45 An empty string is returned if ``skip`` exceeds the stack height. 49 Adapted from from http://stackoverflow.com/a/9812105 50 by adding support to get the class from ``parentframe.f_locals['cls']`` 52 stack = inspect.stack()
54 if len(stack) < start + 1:
56 parentframe = stack[start][0]
59 module = inspect.getmodule(parentframe)
61 name.append(module.__name__)
63 if 'self' in parentframe.f_locals:
64 name.append(type(parentframe.f_locals[
'self']).__name__)
65 elif 'cls' in parentframe.f_locals:
66 name.append(parentframe.f_locals[
'cls'].__name__)
67 codename = parentframe.f_code.co_name
68 if codename !=
'<module>':
def get_caller_name(skip=2)