Added hooke.util.caller.caller_names() for minimal stack traces.
authorW. Trevor King <wking@drexel.edu>
Fri, 27 Aug 2010 15:29:30 +0000 (11:29 -0400)
committerW. Trevor King <wking@drexel.edu>
Fri, 27 Aug 2010 15:29:30 +0000 (11:29 -0400)
hooke/util/caller.py

index b41b0243c54fa28912abbfb79032ed26d13b12ff..bbf9ca5c291ad5142f7f7c75fe082afd24560009 100644 (file)
@@ -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