From: W. Trevor King Date: Fri, 27 Aug 2010 15:29:30 +0000 (-0400) Subject: Added hooke.util.caller.caller_names() for minimal stack traces. X-Git-Url: http://git.tremily.us/?p=hooke.git;a=commitdiff_plain;h=bd3bc564733b0dfc3b17cd21f66c69a43c2fccda Added hooke.util.caller.caller_names() for minimal stack traces. --- diff --git a/hooke/util/caller.py b/hooke/util/caller.py index b41b024..bbf9ca5 100644 --- a/hooke/util/caller.py +++ b/hooke/util/caller.py @@ -78,3 +78,30 @@ def caller_name(depth=1): """ f = frame(depth=depth+1) return f.f_code.co_name + +def caller_names(depth=1): + """Iterate through the names of all functions up the call stack. + + Examples + -------- + + >>> def x(): + ... y() + >>> def y(): + ... z() + >>> def z(): + ... print list(caller_names()) + >>> x() # doctest: +ELLIPSIS + ['z', 'y', 'x', ...] + >>> y() # doctest: +ELLIPSIS + ['z', 'y', ...] + >>> z() # doctest: +ELLIPSIS + ['z', ...] + """ + depth = 2 # start at caller_names()'s caller. + while True: + try: + yield caller_name(depth=depth) + except AttributeError: + return + depth += 1