Sickness alert: since "dir()" in a function _does_ materialize a
snapshot of the locals into a dict as a side-effect, this variation
"works":
def uplevels1( ignore ):
global testlocals
import sys
try:
1 + '' # make an error happen
except: # get caller's frame
frame = sys.exc_traceback.tb_frame.f_back
testlocals = frame.f_locals # caller's locals
def test():
i, j, k = 1, 2, 3
uplevels1( dir() ) # invoking dir for its dict-building side-effect
print 'in test'
print 'locals =', testlocals
test()
print 'back from test'
print 'locals =', testlocals
I.e., that prints
in test
locals = {'j': 2, 'i': 1, 'k': 3}
back from test
locals = {'j': 2, 'i': 1, 'k': 3}
where before an empty dict was displayed from inside test.
don't-try-this-at-home-ly y'rs - tim
Tim Peters tim@ksr.com
not speaking for Kendall Square Research Corp