ByteCode Questions

Stefan Schukat SSchukat at dspace.de
Tue Aug 21 13:21:52 EDT 2001


Hi,

I wanted to implement a "EvalInCaller" like in MATLAB. But
my code is only working if an exec statement is called after 
my code, e.g. 

import traceback

def EvalInCaller(Command):
    try:
        1/0
    except:
        import traceback
        import sys
        tb = sys.exc_info()[2]
        ExecFrame = tb.tb_frame.f_back
        exec(Command, ExecFrame.f_globals, ExecFrame.f_locals)

def Working():
    EvalInCaller("a = 2\n")
    exec("")
    print a

def NotWorking():
    EvalInCaller("a = 2\n")
    print a

try:
    Working() 
except:
    traceback.print_exc()

try:
    NotWorking() 
except:
    traceback.print_exc()


So the question is why does Python take the LOAD_GLOBAL byte code
in the not working function and the LOAD_NAME bytecode in the 
working version. How can I force the LOAD_NAME bytecode dynamically 
or must I exchange all LOAD_GLOBAL to LOAD_NAME in the codeblock
used.

Comments ??

    Stefan





More information about the Python-list mailing list