2

Let's say, I obtain a stack frame using sys._getframe(1), which is obviously not the current frame.

Now I want to, in some way, move to the outer stack frame and execute a statement, like maybe x=10, so as to create a variable in that frame.

I understand its not a good practice to set a variable that way, but it just might be some other statement. So, the important part is how to move to that frame. with(frame) doesn't seem to work.

I think it should be possible, else why would there be two functions named getinnerframes and getouterframes in inspect module? Unless you can move to the outer frame, why would you even have inner frames?

Edit: In pdb module, they have two commands up and down with the docs saying

d(own) Move the current frame one level down in the stack trace (to a newer frame).

u(p) Move the current frame one level up in the stack trace (to an older frame).

Is this going to help with my case? If yes, how to use it?

Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
bendtherules
  • 372
  • 4
  • 12
  • Partially answered in [Is it possible to write to a python frame object as returned by sys._getframe() from python code running within the interpreter?](http://stackoverflow.com/questions/626835/is-it-possible-to-write-to-a-python-frame-object-as-returned-by-sys-getframe) – leewz Jun 12 '15 at 04:41

1 Answers1

2

This is not generally possible. If the frame corresponds to module-level code, then you can do

exec 'x=10' in frame.f_globals, frame.f_locals

in Python 2, or

exec('x=10', frame.f_globals, frame.f_locals)

in Python 3. However, if the frame corresponds to a function call or class body, then this will execute x=10 as if it's embedded in a class statement nested within the frame's scope:

If two separate objects are given as globals and locals, the code will be executed as if it were embedded in a class definition.

No code outside the text of a function can create variables local to that function.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Ok, if thats true, what might be the explanation behind having `getinnerframes` function in `inspect`? And what is it actually supposed to return? – bendtherules Jun 12 '15 at 04:40
  • @bendtherules: All the stuff that returns or accesses frames is primarily to inspect their data, not to modify it. – user2357112 Jun 12 '15 at 04:41
  • Yes, but what does that function even return? – bendtherules Jun 12 '15 at 04:52
  • @bendtherules: It returns what it says it returns - a list of frame records for a traceback's frame and all inner frames up to where the exception was raised. The frame of a traceback seems to be the one where the exception was caught. "Frame record" is defined earlier. If you have more questions, you should hit the Ask Question button instead of putting them in the comments of a different question. I'm not really seeing why you think that question is related, anyway; it's not like the usefulness of `getinnerframes` depends on somehow being able to execute statements in the returned frames. – user2357112 Jun 12 '15 at 05:16