diff --git a/serializing.html b/serializing.html index 602a0cc..7d48901 100644 --- a/serializing.html +++ b/serializing.html @@ -6,6 +6,7 @@ @@ -45,7 +46,7 @@ body{counter-reset:h1 13}
What can the pickle module store?
byte objects, byte arrays, and None.
+bytes objects, byte arrays, and None.
The pickle protocol has been around for many years, and it has matured as Python itself has matured. -
FIXME - discussion of pickle protocol versions, backward incompatibility of protocol version 3 due to bytes/strings separation in Python 3, link to http://docs.python.org/3.1/library/pickle.html#data-stream-format +
The pickle protocol has been around for many years, and it has matured as Python itself has matured. There are now four different versions of the pickle protocol. + +
bytes objects and byte arrays. It is a binary format.
+Oh look, the difference between bytes and strings rears its ugly head again. (If you’re surprised, you haven’t been paying attention.) What this means in practice is that, while Python 3 can read data pickled with protocol version 2, Python 2 can not read data pickled with protocol version 3.
FIXME more here about fix_imports and such? +
The most interesting piece of information in that disassembly is on the last line, because it includes the version of the pickle protocol with which this file was saved. There is no explicit version marker in the pickle protocol. To determine which protocol version was used to store a pickle file, you need to look at the markers (“opcodes”) within the pickled data and use hard-coded knowledge of which opcodes were introduced with each version of the pickle protocol. The pickle.dis() function does exactly that, and it prints the result in the last line of the disassembly output. Here is a function that returns just the version number, without printing anything:
+
+
import pickletools
+
+def protocol_version(file_object):
+ maxproto = -1
+ for opcode, arg, pos in pickletools.genops(file_object):
+ maxproto = max(maxproto, opcode.proto)
+ return maxproto
+
+And here it is in action:
+ +
+>>> import pickleversion
+>>> with open('entry.pickle', 'rb') as f:
+... pickleversion.protocol_version(f)
+3
⁂