identity of the caller?
Robin Becker
robin at jessikat.demon.co.uk
Thu Feb 24 05:18:09 EST 2000
In article <38B4EDE0.1DB1B55F at horvath.com>, Bob Horvath
<bob at horvath.com> writes
>
>Is there any way to find out the identity of who called you? I am
>mainly thinking of method calls, but in general if there is a way
>for regular function calls, I would be interested in that too.
>
>I suppose I should give some context. We have a tool that draws
>message flow diagrams for communications systems. These flows are
>similar to use case flows, or ITU Z.120 MSC charts if you know
>what those are. The tools takes simple ascii text which has the
>sender, the receiver, and the message name, as in...
>
>SENDER RECEIVER MessageName
>
>We tend to have to enumerate a lot of these, and I had the thought
>that if I could prototype the functionality of the nodes, they
>could generate the text used to generate the drawings.
>
>What I was thinking is that the receivers of the message would
>define methods that handle them. To get the picture right,
>I would need to print out method name, the receiver, and who sent
>it. It is the "who sent it" bit that I can't visualize a solution
>for.
>
#Try this
#don't know how to get class names though?
from sys import exc_info
def _tb():
return exc_info()[2]
def who_called_me(n=0):
try:
raise None
except:
f = _tb().tb_frame.f_back
while n>0:
t = f.f_back
if not hasattr(t,'f_code'): break
f = t
n = n - 1
c = f.f_code
return c.co_filename, c.co_name, f.f_lineno
if __name__=='__main__':
import sys
def test2():
print 'test2',who_called_me(1)
def test1():
print 'test1',who_called_me(1)
test2()
def test():
print 'test',who_called_me(1)
test1()
test2()
class dingo:
def __init__(self):
self.a = 1
print '__init__',who_called_me(1)
def doit(self):
print 'dingo.doit',who_called_me(1), self.a
def calldoit(self):
print 'dingo.calldoit',who_called_me(1), self.a
self.doit()
test()
def mongo():
print 'mongo', who_called_me(1)
d=dingo()
d.doit()
d.calldoit()
test()
test1()
test2()
mongo()
--
Robin Becker
More information about the Python-list
mailing list