marshal and unmarshal

Discussion in 'Python' started by leo, Aug 26, 2006.

  1. leo

    leo Guest

    Hi,
    following is marshal and unmarshal script

    import marshal
    strng = """
    print 'hello world'
    """
    code = compile(strng, "<strng>", "exec")
    data = marshal.dumps(code)
    strng0 = marshal.loads(data)
    print repr(data)
    print "-"*100
    print strng0

    ###Result:
    'c\x00\x00\x00\x00\x01\x00\x00\x00s\x0f\x00\x00\x00\x7f\x00\x00\x7f\x02\x00d\x00\x00GHd\x01\x00S(\x02\x00\x00\x00s\x0b\x00\x00\x00hello
    worldN(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s\x07\x00\x00\x00<strng>s\x01\x00\x00\x00?\x02\x00s\x00\x00\x00\x00'
    ----------------------------------------------------------------------------------------------------
    <code object ? at 0x81690c0, file "<strng>", line 2>

    Question:
    1. why unmarshal data is
    <code object ? at 0x81690c0, file "<strng>", line 2>
    not
    'c\x00\x00\x00\x00\x01\x00\x00\x00s\x0f\x00\x00\x00\x7f\x00\x00\x7f\x02\x00d\x00\x00GHd\x01\x00S(\x02\x00\x00\x00s\x0b\x00\x00\x00hello
    worldN(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s\x07\x00\x00\x00<strng>s\x01\x00\x00\x00?\x02\x00s\x00\x00\x00\x00'

    2. how safe is the compiled and serialize data.
    'c\x00\x00\x00\x00\x01\x00\x00\x00s\x0f\x00\x00\x00\x7f\x00\x00\x7f\x02\x00d\x00\x00GHd\x01\x00S(\x02\x00\x00\x00s\x0b\x00\x00\x00hello
    worldN(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s\x07\x00\x00\x00<strng>s\x01\x00\x00\x00?\x02\x00s\x00\x00\x00\x00'
    leo, Aug 26, 2006
    #1
    1. Advertising

  2. leo wrote:

    > 'c\x00\x00\x00\x00\x01\x00...
    > ----------------------------------------------------
    > <code object ? at 0x81690c0, file "<strng>", line 2>
    >
    > Question:
    > 1. why unmarshal data is
    > <code object ? at 0x81690c0, file "<strng>", line 2>


    because that's what compile returns, of course. marshal.dumps takes
    a Python object and turns it into a string; marshal.loads takes such
    a string and turns it into a Python object.

    > 2. how safe is the compiled and serialize data.


    define "safe".

    </F>
    Fredrik Lundh, Aug 27, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Serge Corbucci
    Replies:
    0
    Views:
    482
    Serge Corbucci
    Jan 14, 2004
  2. crash.test.dummy

    unmarshal XML from String using CastorXML

    crash.test.dummy, Jan 29, 2006, in forum: Java
    Replies:
    2
    Views:
    5,062
    crash.test.dummy
    Jan 30, 2006
  3. Dmitri Fedoruk
    Replies:
    4
    Views:
    694
    Dmitri Fedoruk
    Sep 14, 2007
  4. Replies:
    10
    Views:
    531
    Aaron Watters
    Jun 18, 2008
  5. Michael Davis

    Ruby 1.8 and Marshal.load/Marshal.dump

    Michael Davis, Oct 10, 2003, in forum: Ruby
    Replies:
    0
    Views:
    167
    Michael Davis
    Oct 10, 2003
Loading...

Share This Page