views:

119

answers:

3

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.

+2  A: 

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 + 7   # capture current point of execution, advance to expected store
  print dis.disco(f.f_code, i)
+1 I think that this is the way that I am going to end up having to go. I'm going to need to mix some AST work in here to figure out which line the assignment occurs on to know which `store` instruction I'm interested in finding.
aaronasterling
A: 

I don't know of how much help this is, but have you considered using the call to locals()? It returns a dict that contains the name and value of all the local variables.

For example:

s = ''
locals()
>>> {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 's': '', '__name__': '__main__', '__doc__': None}
t = s  # I think this is what is of most importance to you
locals()
>>> {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 's': '', 't': '', '__name__': '__main__', '__doc__': None}

So you could traverse this dictionary and check which variables have (as their value) an object of the type that you are looking for.

Like I said, I don't know of how much help this answer is, but if you need clarification on anything, then do leave a comment and I will try to respond as best as I can.

inspectorG4dget
This doesn't work because `locals()` always refers to the frame it's called in, I'm looking for one frame up. I can get that with either `sys._getframe()` for `inspect.currentfrmae`. The problem is that `foo = bar()` does not create an entry in `locals()` (referring to the frame the assignment takes place in ) until _after_ `bar.__init__()` returns. But that's the appropriate place to get the name because I can just do it with a decorator on `bar` instead of putting code after _every_ assignment.
aaronasterling
@AaronMcSmooth: Would a call to globals() instead of locals fix this problem
inspectorG4dget
@ InspectoG4det No for the same reason. `globals` is just `frame.f_locals` for the outermost frame and so an entry isn't places in `frame.f_locals` until the inner frame (`bar.__init__()` in this case) returns.
aaronasterling
@AaronMcSmooth: Many thanks. I actually didn't know this before. Thanks for teaching me something new (+1).
inspectorG4dget
A: 

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?

Yes:

  1. Start a program

  2. Wait unit it imports a particular module foo.py

  3. Edit foo.py

Now code that's loaded in a Python process doesn't match code found on disk.

Yet another reason why disassembling the bytecode may be a better technique.

Marius Gedminas