4 # https://gist.github.com/techtonik/2151727
5 # Public Domain, i.e. feel free to copy/paste
6 # Considered a hack in Python 2
13 def caller_name(skip=1):
14 """Get a name of a caller in the format module.class.method
16 `skip` specifies how many levels of stack to skip while getting caller
17 name. skip=1 means "who calls me", skip=2 "who calls my caller" etc.
19 An empty string is returned if skipped levels exceed stack height
24 framelist.append(frame)
28 stack = stack_(sys._getframe(1))
30 if len(stack) < start + 1:
32 parentframe = stack[start]
35 module = inspect.getmodule(parentframe)
36 # `modname` can be None when frame is executed directly in console
37 # TODO(techtonik): consider using __main__
39 name.append(module.__name__)
41 if 'self' in parentframe.f_locals:
42 # I don't know any way to detect call from the object method
43 # XXX: there seems to be no way to detect static method call - it will
44 # be just a function call
45 name.append(parentframe.f_locals['self'].__class__.__name__)
46 codename = parentframe.f_code.co_name
47 lineno = inspect.currentframe().f_back.f_back.f_lineno
48 if codename != '<module>': # top level usually
49 name.append(codename + "[%s]" % str(lineno)) # function or a method