2 years ago
#70900
rpetti
How do I get python3's marshal.loads to decode strings like in python 2?
I've got some code that needs to load marshalled dictionaries. This works fine in python 2.
Sample data (base64 encoded):
e3MEAAAAY29kZXMEAAAAc3RhdHMEAAAAVXNlcnMGAAAAcnBldHRpcwUAAABFbWFpbHMLAAAAcnBl
dHRpQGRhc2hzBgAAAFVwZGF0ZXMTAAAAMjAyMS8wNy8yOCAxMzo0MDo1MXMGAAAAQWNjZXNzcxMA
AAAyMDIyLzAxLzE5IDA4OjU2OjUzcwgAAABGdWxsTmFtZXMGAAAAcnBldHRpcwgAAABQYXNzd29y
ZHMGAAAAKioqKioqcwQAAABUeXBlcwgAAABzdGFuZGFyZHMKAAAAQXV0aE1ldGhvZHMIAAAAcGVy
Zm9yY2VzCQAAAGV4dHJhVGFnMHMOAAAAcGFzc3dvcmRDaGFuZ2VzDQAAAGV4dHJhVGFnVHlwZTBz
BAAAAGRhdGVzDgAAAHBhc3N3b3JkQ2hhbmdlcxMAAAAyMDIxLzA3LzI4IDEzOjQxOjIxMA==
Script:
#!/usr/bin/python
import marshal
import sys
while(True):
try:
data = marshal.load(sys.stdin)
print(data)
except EOFError:
break
Test:
$ base64 -d < sample.base64 | python unmarshal-test.py
{'code': 'stat', 'AuthMethod': 'perforce', 'Update': '2021/07/28 13:40:51', 'passwordChange': '2021/07/28 13:41:21', 'Access': '2022/01/19 08:56:53', 'extraTagType0': 'date', 'User': 'rpetti', 'FullName': 'rpetti', 'Password': '******', 'Type': 'standard', 'Email': 'rpetti@dash', 'extraTag0': 'passwordChange'}
However in Python 3, I end up with a dictionary that has byte objects for all keys and values:
Script:
#!/usr/bin/python
import marshal
import sys
while(True):
try:
# need to read from sys.stdin.buffer since sys.stdin's type has changed in python3
data = marshal.load(sys.stdin.buffer)
print(data)
except EOFError:
break
Test:
$ base64 -d < sample.base64 | python3 unmarshal-test.py
{b'code': b'stat', b'User': b'rpetti', b'Email': b'rpetti@dash', b'Update': b'2021/07/28 13:40:51', b'Access': b'2022/01/19 08:56:53', b'FullName': b'rpetti', b'Password': b'******', b'Type': b'standard', b'AuthMethod': b'perforce', b'extraTag0': b'passwordChange', b'extraTagType0': b'date', b'passwordChange': b'2021/07/28 13:41:21'}
It seems like python 2's marshal.loads automatically decodes bytes into strings where appropriate. Is there any way to get that same behavior in python 3? Or do I just need to manually decode every key and value in the loaded object after the fact?
python-3.x
python-2.7
unmarshalling
0 Answers
Your Answer