How to find out name of calling function from called function 
Author Message
 How to find out name of calling function from called function

Hi everyone,
Is it possible with Python1.4 to find out the name of
the calling function from the called function itself? I'd
like to use this feature for debugging. Any help or hints
would be greatly appreciated.
Thuc



Sat, 15 Apr 2000 03:00:00 GMT  
 How to find out name of calling function from called function

Quote:
>Is it possible with Python1.4 to find out the name of
>the calling function from the called function itself? I'd
>like to use this feature for debugging. Any help or hints
>would be greatly appreciated.

Sure. Here's one way to do it:

def caller():
    # find name of calling function
    try:
        raise SyntaxError
    except:
        import sys
        return sys.exc_traceback.tb_frame.f_back.f_back.f_code.co_name

#
# example
def foo():
    print caller()

def main():
    foo()

main()

The "caller" function returns the name of the function two levels
up the stack, or "?" if called from the module level.  People using
this for other purposes than debugging will be slapped in the
head with a huge great fish (etc, etc).

Cheers /F



Sat, 15 Apr 2000 03:00:00 GMT  
 How to find out name of calling function from called function

#!/usr/local/bin/python

"""myname.py:  illustrate how to obtain the name of a function object
        from a stack backtrace

    Charles G. Waldman,  July 1997

"""

import sys

ForceTraceback = "ForceTraceback"

def myname():
    """This function will return the name of the function which calls
it"""
    try:
        raise ForceTraceback
    except ForceTraceback:
        tb = sys.exc_traceback
        f = tb.tb_frame.f_back   # need to go up one level, to get the
caller's stack frame
        return f.f_code.co_name

def caller_name():
    """This function will return the name of the function which calls
it"""
    try:
        raise ForceTraceback
    except ForceTraceback:
        tb = sys.exc_traceback
        f = tb.tb_frame.f_back.f_back   # need to go up two levels, to
get the caller's caller's stack frame
        return f.f_code.co_name

def f():
    print myname()

class X:
    def meth(self):
        print myname()
    def meth2(self):
        print self.__class__.__name__ + "." + myname()

def bar():
    print "I am ", myname()
    print "I am being called by ", caller_name()

def foo():
    return bar()

if __name__ == "__main__":
    f()
    x = X()
    x.meth()
    x.meth2()
    foo()



Sat, 15 Apr 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. VC++ calling fortran function and fortran function calling a c++ function

2. Call Library Function - Function not found in library

3. Calling functions from functions from functions ...

4. Find function name in function

5. Calling functions by name?

6. Api calls Where I can found C function prototypes

7. HOW DO WE GET THE NAMES OF FUNCTIONS FOR WIN32API CALLS

8. Runtime can't be found when calling function in DLL

9. Problem with Called Library Function name

10. C functions calling {C++/Ada/...} functions

11. What function called this function ??

12. Calling a function or method by name

 

 
Powered by phpBB® Forum Software