Executing bytecode from a string.

vjshield-widgets at yahoo.com vjshield-widgets at yahoo.com
Mon Aug 23 12:34:38 EDT 2004


>   I am just writing my Macro/Script in python
>>sourcecode.
>   In the program I'm doing:
>   
>   f = open('segment.py','r')
>   str = f.read()
>   f.close()
>   
>   segment_code = compile(str, '<string>', 'exec')
>   
>   exec(segment_code)

I have the same problem. I'm currently storing the
source in the db. From my application
I get the source code, save it to a local file and
then do the following:
 
        module          =
sys.modules[randomModuleFullPath]
        classObj  = getattr(module, customClassName)
        self.system     = classObj()

The current doucmentation specifiees compile usage as:

compile( file[, cfile[, dfile[, doraise]]]) 

    Compile a source file to byte-code and write out
the byte-code cache file. The source code is
    loaded from the file name file. The byte-code is
written to cfile, which defaults to file + 'c' 
    ('o' if optimization is enabled in the current
interpreter). If dfile is specified, it is used 
    as the name of the source file in error messages
instead of file. If doraise = True, a 
    PyCompileError is raised when an error is
encountered while compiling file. If doraise = False 
    (the default), an error string is written to
sys.stderr, but no exception is raised. 

Is your compile the same function? Does it reutrn the
byte code of the module. If the bytecode contains
a Class definition, how do i instantiate a class
object from it. 

Thanks,

VJ



More information about the Python-list mailing list