"compile()" portability
Fredrik Lundh
fredrik at pythonware.com
Sat Nov 22 09:59:59 EST 2003
Just wrote:
> > I would like to know if executing:
> >
> > c = compile('a=5\nprint a','<string>','exec')
> >
> > on a Linux box, then pickling+beaming the result on a Windows box, will give
> > me the expecting result:
> >
> > >>> exec(c)
> > 5
> >
> > In other words, does 'compile()' generate platform-dependent code?
>
> If you mean "marshal" instead of "pickle", and use the same (major)
> Python version on both ends, then yes.
to make sure both sites are using the same byte code revision,
check the imp.get_magic() string.
something like this should work:
code = compile(...)
data = imp.get_magic() + marshal.dumps(code)
...
magic = imp.get_magic()
if not data.startswith(magic):
raise ValueError("cannot run this bytecode")
code = marshal.loads(data[len(magic):])
(len(magic) is currently 4, and will probably remain so for the
foreseeable 2.X future, but one never knows what happens in
3.X...)
</F>
More information about the Python-list
mailing list