Home » Coding » Python » finding out the call (and not only the caller)
finding out the call (and not only the caller) [message #17440] Sun, 07 October 2007 17:47
Francesco Guerrieri  is currently offline Francesco Guerrieri
Messages: 16
Registered: August 2007
Junior Member
Hi,

Today I've been thinking a bit about the "python internals". Inspired
by this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/6606 2
I found out a little problem which haven't been able to solve.
In short, is there a way to find out how a given name lookup was started?
It is not enough to know the name of the caller as given by the recipe.

a little example:
import inspect

class test(object):
def __init__(self, a_list):
self.my_list = a_list
def info(self):
for counter, item in
enumerate(inspect.getouterframes(inspect.currentframe())):
print counter, item
return self.my_list
data = property(info)

if __name__ == '__main__':
a = test([1111,2222])
def g(a_seq):
for item in a_seq:
print item, '\n'
g(a.data)

This prints
0 (<frame object at 0x00B58B08>, 'myfile.py', 10, 'info', ['
for counter, item in
enumerate(inspect.getouterframes(inspect.currentframe())):\n '], 0)
1 (<frame object at 0x00A5B000>, 'myfile.py', 38, '<module>', ['
g(a.data)\n'], 0)
1111
2222

What I would like is a reference to g itself, and not only to
'<module>' which is the caller according to f_code.co_name.
I thought of manually parsing the string ' g(a.data)\n' to extract
the name but I'm sure that it would be a rather fragile approach, and
so I decided that it was time to ask for help :-)
To 'get a feeling', I tried to disassemble the code:

code = compile('g(a.data)', 'test', 'single')
dis.dis(code)

and this is the result

0 LOAD_NAME 0 (g)
3 LOAD_NAME 1 (a)
6 LOAD_ATTR 2 (data)
9 CALL_FUNCTION 1
12 PRINT_EXPR
13 LOAD_CONST 0 (None)
16 RETURN_VALUE

So ... I'm looking for the first name loaded. Is there a reference to
it, somewhere?

Francesco
Previous Topic:Override 'and' and 'or'
Next Topic:ANN: Pyrex 0.9.6
Goto Forum:
  


Current Time: Fri May 16 02:41:12 EDT 2008

Total time taken to generate the page: 0.92243 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software