Determine calling module's name
Roland Mas
mas at echo.fr
Thu Aug 10 03:17:59 EDT 2000
jeffgray at my-deja.com (2000-08-10 06:11:02 +0000) :
> How do I determine the calling module's name? i.e. from within a
> function, how can I print the __name__ of the next level up in the stack
> frame?
[...]
> My next thought was that there must be a way to determine the calling
> module name. I looked at the standard traceback module and all the
> sys.exc_info() type functions, but they only work with an exception.
So, you raise an exception, and Bob's your uncle:
def whocalled ():
try:
raise Exception ()
except:
list = traceback.extract_stack ()
(file, line, func, None) = list [len (list) - 3]
m = re.match ('.*/(.*)\.py', file)
return m.group (1), func, line
def logmsg (level, txt):
(file, func, None) = whocalled ()
sys.stderr.write (file + "::" + func + ": " + txt + "\n")
Roland.
--
Roland Mas
A lesson for you all: never fall in love during a total eclipse.
-- Senex, in A Funny Thing Happened on the Way to the Forum
More information about the Python-list
mailing list