[Python-Dev] frame.f_locals is writable

Nick Coghlan ncoghlan at iinet.net.au
Fri Jan 14 15:24:15 CET 2005


Shane Holloway (IEEE) wrote:
> For a little background, I'm working on making an edit and continue 
> support in python a little more robust.  So, in replacing references to 
> unmodifiable types like tuples and bound-methods (instance or class), I 
> iterate over gc.get_referrers.
> 
> So, I'm working on frame types, and wrote this code::
> 
>     def replaceFrame(self, ref, oldValue, newValue):
>         for name, value in ref.f_locals.items():
>             if value is oldValue:
>                 ref.f_locals[name] = newValue
>                 assert ref.f_locals[name] is newValue
> 

FWIW, this should work:

     def replaceFrame(self, ref, oldValue, newValue):
         for name, value in ref.f_locals.items():
             if value is oldValue:
                 exec "ref.f_locals[name] = newValue"
                 assert ref.f_locals[name] is newValue

And, no, you don't have to tell me that this is an evil hack. I already know 
that, since I discovered it earlier this evening by poking around in the C 
source code for PyFrame_LocalsToFast and then looking to see what code calls 
that function :)

Cheers,
Nick

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


More information about the Python-Dev mailing list