Lines Matching defs:pickle
1 '''"Executable documentation" for the pickle module.
3 Extensive comments about the pickle protocols and pickle-machine opcodes
6 genops(pickle)
7 Generate all the opcodes in a pickle, as (opcode, arg, position) triples.
9 dis(pickle, out=None, memo=None, indentlevel=4)
10 Print a symbolic disassembly of a pickle.
17 # - A pickle verifier: read a pickle and check it exhaustively for
20 # - A protocol identifier: examine a pickle and return its protocol number
21 # (== the highest .proto attr value among all the opcodes in the pickle).
24 # - A pickle optimizer: for example, tuple-building code is sometimes more
31 "A pickle" is a program for a virtual pickle machine (PM, but more accurately
43 literal immediately following the INT opcode in the pickle bytestream. Other
62 point, and some sequences of pickle opcodes are subtle in order to
65 + Things pickle doesn't know everything about. Examples of things pickle
69 classes, pickle's knowledge is limited. Historically, many enhancements
70 have been made to the pickle protocol in order to do a better (faster,
74 pickle opcodes never go away, not even when better ways to do a thing
79 long integers (before protocol 2, the only ways to pickle a Python long
85 Pickle protocols:
87 For compatibility, the meaning of a pickle opcode never changes. Instead new
88 pickle opcodes get added, and each version's unpickler can handle all the
89 pickle opcodes in all protocol versions to date. So old pickles continue to
93 versions. However, a pickle does not contain its version number embedded
94 within it. If an older unpickler tries to read a pickle using a later
98 The original pickle used what's now called "protocol 0", and what was called
99 "text mode" before Python 2.3. The entire pickle bytestream is made up of
117 - A better way to pickle instances of new-style classes (NEWOBJ).
119 - A way for a pickle to identify its protocol (PROTO).
150 # Some pickle opcodes have an argument, following the opcode in the
502 # the pickle may have been written on a 64-bit box. There's also a hack
545 in a short Python int on the box where the pickle
547 in a short Python int on the box where the pickle
620 from pickle import decode_long
692 # Object descriptors. The stack used by the pickle machine holds objects,
821 # Descriptors for pickle opcodes.
1456 In order to guarantee pickle interchangeability, the extension
1504 # Ways to build objects of classes pickle doesn't know about directly
1506 # and comprehensibly -- you really have to read the pickle code to
1599 its __init__() method (pickle has waffled on this over the years; not
1684 For protocol 2 and above, a pickle must start with this opcode.
1696 Every pickle ends with this opcode. The object at the top of the stack
1711 The pickle module doesn't define what a persistent ID means. PERSID's
1755 # Also ensure we've got the same stuff as pickle.py, although the
1764 import pickle, re
1767 for name in pickle.__all__:
1772 picklecode = getattr(pickle, name)
1775 print ("skipping %r: value %r doesn't look like a pickle "
1784 raise ValueError("for pickle code %r, pickle.py uses name %r "
1792 raise ValueError("pickle.py appears to have a pickle opcode with "
1796 msg = ["we appear to have pickle opcodes that pickle.py doesn't have:"]
1805 # A pickle opcode generator.
1807 def genops(pickle):
1808 """Generate all the opcodes in a pickle.
1810 'pickle' is a file-like object, or string, containing the pickle.
1812 Each opcode in the pickle is generated, from the current pickle position,
1820 If the opcode has an argument embedded in the pickle, arg is its decoded
1824 If the pickle has a tell() method, pos was the value of pickle.tell()
1825 before reading the current opcode. If the pickle is a string object,
1827 used. Else (the pickle doesn't have a tell(), and it's not obvious how
1833 if isinstance(pickle, str):
1834 pickle = StringIO.StringIO(pickle)
1836 if hasattr(pickle, "tell"):
1837 getpos = pickle.tell
1843 code = pickle.read(1)
1847 raise ValueError("pickle exhausted before seeing STOP")
1855 arg = opcode.arg.reader(pickle)
1862 # A pickle optimizer.
1865 'Optimize a pickle string by removing unused PUT opcodes'
1878 # Copy the pickle string except for PUTS without a corresponding GET
1889 # A symbolic pickle disassembler.
1891 def dis(pickle, out=None, memo=None, indentlevel=4):
1892 """Produce a symbolic disassembly of a pickle.
1894 'pickle' is a file-like object, or string, containing a (at least one)
1895 pickle. The pickle is disassembled from the current position, through
1901 Optional arg 'memo' is a Python dict, used as the pickle's memo. It
1902 may be mutated by dis(), if the pickle contains PUT or BINPUT opcodes.
1937 for opcode, arg, pos in genops(pickle):
2027 # For use in the doctest, simply as an example of a class to pickle.
2033 >>> import pickle
2035 >>> pkl = pickle.dumps(x, 0)
2062 Try again with a "binary" pickle.
2064 >>> pkl = pickle.dumps(x, 1)
2090 >>> dis(pickle.dumps(pickletools.dis, 0))
2098 >>> dis(pickle.dumps(x, 0))
2119 >>> dis(pickle.dumps(x, 1))
2153 >>> dis(pickle.dumps(L, 0))
2165 >>> dis(pickle.dumps(L, 1))
2176 Note that, in the protocol 0 pickle of the recursive tuple, the disassembler
2180 >>> dis(pickle.dumps(T, 0))
2196 >>> dis(pickle.dumps(T, 1))
2212 >>> dis(pickle.dumps(L, 2))
2223 >>> dis(pickle.dumps(T, 2))
2238 >>> import pickle
2241 >>> p = pickle.Pickler(f, 2)