23 from __future__
import absolute_import, division, print_function
26 __all__ = [
"get_caller_name"]
30 """Get the name of the caller as a string in the form module.class.method
32 Any item that cannot be determined (or is not relevant, e.g. a free function
33 function has no class) is silently omitted, along with an associated separator.
34 An empty string is returned if `skip` exceeds the stack height.
39 How many levels of stack to skip while getting caller name;
40 1 means "who calls me", 2 means "who calls my caller", etc.
42 Adapted from from http://stackoverflow.com/a/9812105
43 by adding support to get the class from parentframe.f_locals['cls']
45 stack = inspect.stack()
47 if len(stack) < start + 1:
49 parentframe = stack[start][0]
52 module = inspect.getmodule(parentframe)
54 name.append(module.__name__)
56 if 'self' in parentframe.f_locals:
57 name.append(type(parentframe.f_locals[
'self']).__name__)
58 elif 'cls' in parentframe.f_locals:
59 name.append(parentframe.f_locals[
'cls'].__name__)
60 codename = parentframe.f_code.co_name
61 if codename !=
'<module>':