marshal and unmarshal

leo lgoh at optonline.net
Sat Aug 26 13:55:06 EDT 2006


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' 





More information about the Python-list mailing list