Identifying Caller

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tonguç Yumruk

    Identifying Caller

    Is there a way to identify the caller of a function? For example:

    def foo():
    print <Caller Functions name>

    def bar():
    foo()

    I want foo to print "bar"... And it will be great if I can also detect
    call is came from which module.

    Thanks.

    --
    Love, Respect, Linux
    ############### ############### ############### ############### ############### #
    "And the next time you consider complaining that running Lucid Emacs
    19.05 via NFS from a remote Linux machine in Paraguay doesn't seem to
    get the background colors right, you'll know who to thank."
    (By Matt Welsh)
    ############### ############### ############### ############### ############### #
    Tonguç Yumruk

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQE/iR6d1xWu4MLSyoY RAoE4AJ4r1+kwxq nFOr+Mu9QOqDGF4 Pw6KQCg3Syq
    jabkOdMQup66Xym jtux1QXY=
    =XtrV
    -----END PGP SIGNATURE-----

  • Robin Becker

    #2
    Re: Identifying Caller

    In article <mailman.28.106 5950882.2192.py thon-list@python.org >, Tonguç
    Yumruk <trooper@ttnet. net.tr> writes[color=blue]
    >Is there a way to identify the caller of a function? For example:
    >
    >def foo():
    > print <Caller Functions name>
    >
    >def bar():
    > foo()
    >
    >I want foo to print "bar"... And it will be great if I can also detect
    >call is came from which module.
    >
    >Thanks.
    >[/color]
    try something like this

    ############### ##############
    def who_called_me(n =0):
    import sys
    f = sys._getframe(n )
    c = f.f_code
    return c.co_filename, c.co_name, f.f_lineno

    if __name__=='__ma in__':
    def test2():
    print 'test2',who_cal led_me(1)

    def test1():
    print 'test1',who_cal led_me(1)
    test2()

    def test():
    print 'test',who_call ed_me(1)
    test1()
    test2()

    class dingo:
    def __init__(self):
    self.a = 1
    print '__init__',who_ called_me(1)
    def doit(self):
    print 'dingo.doit',wh o_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

    Comment

    • Alex Martelli

      #3
      Re: Identifying Caller

      Tonguç Yumruk wrote:
      [color=blue]
      > Is there a way to identify the caller of a function? For example:
      >
      > def foo():
      > print <Caller Functions name>
      >
      > def bar():
      > foo()
      >
      > I want foo to print "bar"... And it will be great if I can also detect
      > call is came from which module.[/color]

      See function _getframe in module sys. The frame object that
      it returns has many attributes useful for your tasks. In particular,
      sys._getframe(1 ).f_code.co_nam e should be 'bar' as you require,
      and sys._getframe(1 ).f_globals['__name__'] should be the module
      name which you desire.


      Alex

      Comment

      Working...