[Tutor] Accessing the name of a Function

Carroll, Barry Barry.Carroll at psc.com
Thu Dec 14 01:22:22 CET 2006


> -----Original Message-----
> From: Kent Johnson [mailto:kent37 at tds.net]
> Sent: Wednesday, December 13, 2006 3:42 PM
> To: Carroll, Barry
> Cc: tutor at python.org
> Subject: Re: [Tutor] Accessing the name of a Function
> 
<<skip>>
> 
> You could also do this very simply and cleanly with a decorator,
> assuming Python >= 2.4. This example is pretty much what you want:
> http://wiki.python.org/moin/PythonDecoratorLibrary#head-
> d4ce77c6d6e75aad25baf982f6fec0ff4b3653f4
> 
> In your approach, you can call _getframe() in handlestub, see for
example
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062
> 
> Kent
> 
> >
<<skip>>


Right again, Kent. Due to the constraints of the target test system,
this application has to stay at Python 2.3, at least for now.  But using
_getframe(1) to get the caller's stack frame does simplify the stub
handler call considerably, from 

		    handlestub(ctrl, proc, sys._getframe())
to
		    handlestub()

The handler now looks like this:

##########
def handlestub():
    """Identifies actions that are defined but not yet implemented."""
    
    # Get the calling function's stack frame.  
    thestubframe = sys._getframe(1)
    themsg = "Execuiting %s (implementation pending) with arguments: " %
\
             thestubframe.f_code.co_name
    (theargnames, trash1, trash2, thelocalsdict) = \
         getargvalues(thestubframe)
    # The first two arguments are internal to the application: do not 
    #display.
    if len(theargnames) <= 2:
        themsg += "None"
    else:
        for theargname in theargnames[2:]:
            themsg += "%s=%s  " % (theargname,
thelocalsdict[theargname])
    ctrl = thelocalsdict["ctrl"]
    ctrl.log.logaction(ctrl.log.INFO, themsg)
##########

Thanks again.  

Regards,
 
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed





More information about the Tutor mailing list