From bd3bc564733b0dfc3b17cd21f66c69a43c2fccda Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 27 Aug 2010 11:29:30 -0400 Subject: [PATCH] Added hooke.util.caller.caller_names() for minimal stack traces. --- hooke/util/caller.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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 -- 2.26.2