"compile()" portability

V

Vio

I would like to know if executing:

c = compile('a=5\nprint a','<string>','exec')


5

In other words, does 'compile()' generate platform-dependent code?

Regards,
Vio
 
J

Just

Vio said:
I would like to know if executing:

c = compile('a=5\nprint a','<string>','exec')



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.

Just
 
F

Fredrik Lundh

Just said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top