"compile()" portability

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vio

    "compile()" portability

    I would like to know if executing:

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


    on a Linux box, then pickling+beamin g the result on a Windows box, will give me the expecting result:
    [color=blue][color=green][color=darkred]
    >>> exec(c)[/color][/color][/color]
    5

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

    Regards,
    Vio




  • Just

    #2
    Re: &quot;compile() &quot; portability

    In article <mailman.987.10 69497456.702.py thon-list@python.org >,
    Vio <vmilitaru@symp atico.ca> wrote:
    [color=blue]
    > I would like to know if executing:
    >
    > c = compile('a=5\np rint a','<string>',' exec')
    >
    >
    > on a Linux box, then pickling+beamin g the result on a Windows box, will give
    > me the expecting result:
    >[color=green][color=darkred]
    > >>> exec(c)[/color][/color]
    > 5
    >
    > In other words, does 'compile()' generate platform-dependent code?[/color]

    If you mean "marshal" instead of "pickle", and use the same (major)
    Python version on both ends, then yes.

    Just

    Comment

    • Fredrik Lundh

      #3
      Re: &quot;compile() &quot; portability

      Just wrote:
      [color=blue][color=green]
      > > I would like to know if executing:
      > >
      > > c = compile('a=5\np rint a','<string>',' exec')
      > >
      > > on a Linux box, then pickling+beamin g the result on a Windows box, will give
      > > me the expecting result:
      > >[color=darkred]
      > > >>> exec(c)[/color]
      > > 5
      > >
      > > In other words, does 'compile()' generate platform-dependent code?[/color]
      >
      > If you mean "marshal" instead of "pickle", and use the same (major)
      > Python version on both ends, then yes.[/color]

      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(c ode)

      ...

      magic = imp.get_magic()
      if not data.startswith (magic):
      raise ValueError("can not run this bytecode")
      code = marshal.loads(d ata[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>




      Comment

      Working...