3 """Define :func:`caller_name`.
5 This is useful, for example, to declare the `@callback` decorator for
6 making GUI writing less tedious. See :mod:`hooke.util.callback` and
7 :mod:`hooke.ui.gui` for examples.
14 """Return the frame for the function `depth` up the call stack.
18 The `ZeroDivisionError` trick is from stdlib's traceback.py. See
19 the Python Refrence Manual on `traceback objects`_ and `frame
22 .. _traceback objects:
23 http://docs.python.org/reference/datamodel.html#index-873
25 http://docs.python.org/reference/datamodel.html#index-870
28 raise ZeroDivisionError
29 except ZeroDivisionError:
30 traceback = sys.exc_info()[2]
31 f = traceback.tb_frame
32 for i in range(depth):
36 def caller_name(depth=1):
37 """Return the name of the function `depth` up the call stack.
45 ... print caller_name(depth)
55 See the Python Refrence manual on `frame objects`_ and
59 http://docs.python.org/reference/datamodel.html#index-870
61 http://docs.python.org/reference/datamodel.html#index-866
63 f = frame(depth=depth+1)
64 return f.f_code.co_name