marshal and unmarshal

L

leo

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'
 
F

Fredrik Lundh

leo said:
'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>
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top