Entry
Function which knows who has called it
Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 293, Darrell
"""
Packages: miscellaneous.introspection
"""
"""
> Is if possible in Python to have a function that prints the name
> of the function that called it. (I'm thinking of using this in
> some debugging code I want to write).
"""
def myDirExcept(howFarBack):
"""
Get the directory of the module running at "howfarBack "
in the stack frames
This value is compiled into the .pyc file
This can be a problem.
Name will change depending on where python was run from
when the pyc file was created
Unix can store a relative path
Windows stores an absolute path
"""
try:
raise ZeroDivisionError
except ZeroDivisionError:
f = sys.exc_info()[2].tb_frame
for i in range(howFarBack):
f = f.f_back
t = f.f_code
fname = t.co_filename
p = string.split(fname, os.sep)
fname = string.join(p[:-1], os.sep)
return fname