Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the name that an object being created is assigned to

I'm writing some code to determine the name that an object is assigned to. This is for general debugging work and to further familiarize myself with python internals.

I have it structured as a class decorator so that all instances of that class will have their names recorded if it is possible to do. The code is fairly long so I won't post it unless asked. The general technique is as follows though

  1. decorate the class' __init__ method with the code to do what I want

  2. set caller = inspect.currentframe().f_back and open inspect.getframeinfo(caller).filename and send it to ast.parse. I don't do any error checking here because (1) this is just for debugging/profiling/hacking (2) this exact process was 'just' completed or the code wouldn't be running. Is there a problem with this?

  3. find the ast.Assignment instance that causes the currently executing __init__ method to run

  4. if len(assignment.targets) == 1 then there is only one item on the left hand side, and I can get the name out of targets[0].id. In a simple form like a = Foo(), then the assignment.value is an instance of ast.Call. if it's a literal (e.g. list), then value will be that list and bail because the object I'm interested in isn't being assigned to a name.

What is the best way to confirm that assignment.value.func is in fact type(obj).__call__ of the object that I'm interested in. I'm pretty sure that I'm guaranteed that It's "in there somewhere" or the code wouldn't even be running. I just need for it to be at the top level. The obvious thing to do is walk it and make sure that it doesn't contain any interior calls. Then I'm guaranteed that I have the name. (My reasoning is correct, I'm not sure if its assumptions are). This is not ideal though because if I'm interested in Foo, this could lead me to toss away a = Foo(Bar()) because I don't know if it's a = Bar(Foo()).

Of course I can just check assignment.value.func.id but then somebody could have done Foobar = Foo or something so I don't want to rely on this too heavily

Any help would be greatly appreciated. As always, I'm interested in any other suggestions or problems that I might be overlooking.

Also, I'm really surprised that I just had to invent the 'python-internals' tag.

like image 560
aaronasterling Avatar asked Sep 19 '10 06:09

aaronasterling


1 Answers

The AST can't give you that answer. Try using frame.f_lasti and then peeking into the bytecode. If the next line isn't a STORE_FAST, you've got interior calls or something else going on other than the simple assignment you're looking for.

def f():
  f = sys._getframe()
  i = f.f_lasti + 3   # capture current point of execution, advance to expected store
  print dis.disco(f.f_code, i)
like image 52
tln Avatar answered Oct 14 '22 00:10

tln