Modify the local scope inside a function

Fabio Zadrozny fabioz at esss.com.br
Wed Mar 1 06:10:18 EST 2006


Hi Sandra,

Well, first, I'm not sure if you'd be interested, but Pydev Extensions 
(http://www.fabioz.com/pydev) should be able to make remote debugging in 
the way you want...Now, in order to do what you are trying to do, 
debuggers (or at least the pydev debugger) go for the frame you want to 
execute things (that contains the locals and globals in some scope).

In pydev extensions, in interactive debugging, the code to evaluate 
expressions is something like:

frame = findFrame(thread_id, frame_id)
exec expression in frame.f_globals, frame.f_locals

So, you'd just need to get the frame... pydev does multithreaded 
debugging, so, it needs to know the thread too, but if you just want to 
debug the current thread, you could just go to curFrame = 
sys._getframe() and then go iterating back in the frames to reach the 
one you want at frame.f_back (that's basically what the findFrame 
function does).

Cheers,

Fabio

Sandra-24 wrote:

>Hey Crutcher, thanks for the code, that would work. I'm now debating
>using that, or using function arguments to get the variables into the
>namespace. This would require knowing the variables in the dict ahead
>of time, but I suppose I can do that because it's part of the same
>system that creates the dict. I'm just not very fond of having code
>relating to one thing in more than one place, because it puts the onus
>on the programmer to remember to change it in both places. Here I might
>forgive it because it would make the generated code more readable.
>
>It seems I created a fair amount of confusion over what I'm trying to
>do. I use special psp like templates in my website. The template engine
>was previously execing the generated template code. It uses special
>environment variables that give it access to the functionality of the
>web engine. These are what are in that scope dictionary of mine, and
>why I exec the code in that scope.
>
>However, I want to integrate a debugger with the web engine now, and
>debugging execed generated code is a nightmare. So I save the generated
>code as a function in a module that is generated by the template
>engine. Unless I'm missing something about what you're saying, this
>should now be faster as well, because afaik execed code has to be
>compiled on the spot, wheras a module when you load it, is compiled (or
>loaded from a .pyc file) at import time. So one import and repeated
>function calls would be cheaper than repeated exec.
>
>Thanks,
>-Sandra
>
>  
>


-- 
Fabio Zadrozny
------------------------------------------------------
Software Developer

ESSS - Engineering Simulation and Scientific Software
www.esss.com.br

Pydev Extensions
www.fabioz.com/pydev

PyDev - Python Development Enviroment for Eclipse
pydev.sf.net
pydev.blogspot.com





More information about the Python-list mailing list