faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 5 people (60%) answered Yes
Recently 0 of 1 people (0%) answered Yes

Entry

Executing binaries...?

Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 234, M.-A. Lemburg


"""
Packages: miscellaneous;core_python
"""
"""
> Is there any way to execute python binaries with something like
> execfile (giving it a special environment as a dict)? The only way I
> can thing of is using execfile that imports another file (which is
> compiled)... Is there any other way?
"""
def execpyc(filename,globals=None,locals=None):
    """ Execute a byte compiled file filename in globals, locals 
    """
    import marshal
    f = open(filename,'rb')
    f.read(8) # skip header (id check omitted)
    code = marshal.load(f)
    exec code in globals,locals