F
Francesco Guerrieri
Hi,
Today I've been thinking a bit about the "python internals". Inspired
by this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062
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
Today I've been thinking a bit about the "python internals". Inspired
by this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062
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