Simon King
unread,Jan 22, 2020, 4:35:16 PM1/22/20Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to sage-...@googlegroups.com
Hi!
I have (Sage-related) data pickled with Python-2. Part of the data is
binary data put into a Python-2 str.
Now, with Python-3, the binary data is put into bytes.
Consequently, when unpickling my old data with Python-3, I want the
Python-2 str to be interpreted as bytes. However, Python-3 insists on
misinterpreting it as str, and I have trouble to turn that str into an
appropriate bytes.
Here is an example:
Unpickling the data initially results in a string, s:
sage: s = '\x80\x1f'
I want it to be interpreted as the following bytes, b:
sage: b = b'\x80\x1f'
How can I efficiently transform s into b? The following works, but I
doubt that it is very efficient:
sage: import struct
sage: struct.pack('{}B'.format(len(s)),*(ord(_) for _ in s)) == b
True
Note that sage.cpython.string.str_to_bytes does't do what I need:
sage: sage.cpython.string.str_to_bytes(s)
b'\xc2\x80\x1f'
Best regards,
Simon