marshal and unmarshal

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

    #1

    marshal and unmarshal

    Hi,
    following is marshal and unmarshal script

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

    ###Result:
    'c\x00\x00\x00\ x00\x01\x00\x00 \x00s\x0f\x00\x 00\x00\x7f\x00\ x00\x7f\x02\x00 d\x00\x00GHd\x0 1\x00S(\x02\x00 \x00\x00s\x0b\x 00\x00\x00hello
    worldN(\x00\x00 \x00\x00(\x00\x 00\x00\x00(\x00 \x00\x00\x00(\x 00\x00\x00\x00s \x07\x00\x00\x0 0<strng>s\x01\x 00\x00\x00?\x02 \x00s\x00\x00\x 00\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\x 00\x00\x7f\x00\ x00\x7f\x02\x00 d\x00\x00GHd\x0 1\x00S(\x02\x00 \x00\x00s\x0b\x 00\x00\x00hello
    worldN(\x00\x00 \x00\x00(\x00\x 00\x00\x00(\x00 \x00\x00\x00(\x 00\x00\x00\x00s \x07\x00\x00\x0 0<strng>s\x01\x 00\x00\x00?\x02 \x00s\x00\x00\x 00\x00'

    2. how safe is the compiled and serialize data.
    'c\x00\x00\x00\ x00\x01\x00\x00 \x00s\x0f\x00\x 00\x00\x7f\x00\ x00\x7f\x02\x00 d\x00\x00GHd\x0 1\x00S(\x02\x00 \x00\x00s\x0b\x 00\x00\x00hello
    worldN(\x00\x00 \x00\x00(\x00\x 00\x00\x00(\x00 \x00\x00\x00(\x 00\x00\x00\x00s \x07\x00\x00\x0 0<strng>s\x01\x 00\x00\x00?\x02 \x00s\x00\x00\x 00\x00'


  • Fredrik Lundh

    #2
    Re: marshal and unmarshal

    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>

    Comment

    Working...