Run update-copyright.py.
[hooke.git] / hooke / util / caller.py
index e2e592dd92cf584b546f57e9a1f8625632bba063..df57b5760311442836a080a761bb1171577484d6 100644 (file)
@@ -1,4 +1,19 @@
-# Copyright
+# Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
+#
+# This file is part of Hooke.
+#
+# Hooke is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
 
 """Define :func:`caller_name`.
 
@@ -62,3 +77,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