annotate Lib/pickletools.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 9feada79a411
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27518
b2b574118ffd minor grammar tweaks
Skip Montanaro <skip@pobox.com>
parents: 27513
diff changeset
1 '''"Executable documentation" for the pickle module.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
3 Extensive comments about the pickle protocols and pickle-machine opcodes
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
4 can be found here. Some functions meant for external use:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
5
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6 genops(pickle)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
7 Generate all the opcodes in a pickle, as (opcode, arg, position) triples.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
8
33004
867fef2b5060 Add argument to docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 27703
diff changeset
9 dis(pickle, out=None, memo=None, indentlevel=4)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
10 Print a symbolic disassembly of a pickle.
27518
b2b574118ffd minor grammar tweaks
Skip Montanaro <skip@pobox.com>
parents: 27513
diff changeset
11 '''
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
12
42064
bc90fc9b70b7 Rip out all codecs that can't work in a unicode/bytes world:
Walter Dörwald <walter@livinglogic.de>
parents: 41986
diff changeset
13 import codecs
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
14 import io
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
15 import pickle
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
16 import re
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
17 import sys
42064
bc90fc9b70b7 Rip out all codecs that can't work in a unicode/bytes world:
Walter Dörwald <walter@livinglogic.de>
parents: 41986
diff changeset
18
45278
e67050d62adf Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60706,60708-60712,60714-60724 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
19 __all__ = ['dis', 'genops', 'optimize']
33917
25714e0fc89d Essentially SF patch 1061679: add missing __all__ to pickletools.py.
Tim Peters <tim.peters@gmail.com>
parents: 33004
diff changeset
20
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
21 bytes_types = pickle.bytes_types
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
22
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
23 # Other ideas:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
24 #
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
25 # - A pickle verifier: read a pickle and check it exhaustively for
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
26 # well-formedness. dis() does a lot of this already.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
27 #
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
28 # - A protocol identifier: examine a pickle and return its protocol number
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
29 # (== the highest .proto attr value among all the opcodes in the pickle).
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
30 # dis() already prints this info at the end.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
31 #
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
32 # - A pickle optimizer: for example, tuple-building code is sometimes more
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
33 # elaborate than necessary, catering for the possibility that the tuple
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
34 # is recursive. Or lots of times a PUT is generated that's never accessed
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
35 # by a later GET.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
36
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
37
82957
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
38 # "A pickle" is a program for a virtual pickle machine (PM, but more accurately
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
39 # called an unpickling machine). It's a sequence of opcodes, interpreted by the
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
40 # PM, building an arbitrarily complex Python object.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
41 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
42 # For the most part, the PM is very simple: there are no looping, testing, or
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
43 # conditional instructions, no arithmetic and no function calls. Opcodes are
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
44 # executed once each, from first to last, until a STOP opcode is reached.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
45 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
46 # The PM has two data areas, "the stack" and "the memo".
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
47 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
48 # Many opcodes push Python objects onto the stack; e.g., INT pushes a Python
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
49 # integer object on the stack, whose value is gotten from a decimal string
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
50 # literal immediately following the INT opcode in the pickle bytestream. Other
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
51 # opcodes take Python objects off the stack. The result of unpickling is
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
52 # whatever object is left on the stack when the final STOP opcode is executed.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
53 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
54 # The memo is simply an array of objects, or it can be implemented as a dict
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
55 # mapping little integers to objects. The memo serves as the PM's "long term
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
56 # memory", and the little integers indexing the memo are akin to variable
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
57 # names. Some opcodes pop a stack object into the memo at a given index,
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
58 # and others push a memo object at a given index onto the stack again.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
59 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
60 # At heart, that's all the PM has. Subtleties arise for these reasons:
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
61 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
62 # + Object identity. Objects can be arbitrarily complex, and subobjects
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
63 # may be shared (for example, the list [a, a] refers to the same object a
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
64 # twice). It can be vital that unpickling recreate an isomorphic object
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
65 # graph, faithfully reproducing sharing.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
66 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
67 # + Recursive objects. For example, after "L = []; L.append(L)", L is a
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
68 # list, and L[0] is the same list. This is related to the object identity
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
69 # point, and some sequences of pickle opcodes are subtle in order to
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
70 # get the right result in all cases.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
71 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
72 # + Things pickle doesn't know everything about. Examples of things pickle
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
73 # does know everything about are Python's builtin scalar and container
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
74 # types, like ints and tuples. They generally have opcodes dedicated to
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
75 # them. For things like module references and instances of user-defined
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
76 # classes, pickle's knowledge is limited. Historically, many enhancements
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
77 # have been made to the pickle protocol in order to do a better (faster,
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
78 # and/or more compact) job on those.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
79 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
80 # + Backward compatibility and micro-optimization. As explained below,
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
81 # pickle opcodes never go away, not even when better ways to do a thing
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
82 # get invented. The repertoire of the PM just keeps growing over time.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
83 # For example, protocol 0 had two opcodes for building Python integers (INT
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
84 # and LONG), protocol 1 added three more for more-efficient pickling of short
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
85 # integers, and protocol 2 added two more for more-efficient pickling of
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
86 # long integers (before protocol 2, the only ways to pickle a Python long
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
87 # took time quadratic in the number of digits, for both pickling and
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
88 # unpickling). "Opcode bloat" isn't so much a subtlety as a source of
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
89 # wearying complication.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
90 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
91 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
92 # Pickle protocols:
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
93 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
94 # For compatibility, the meaning of a pickle opcode never changes. Instead new
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
95 # pickle opcodes get added, and each version's unpickler can handle all the
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
96 # pickle opcodes in all protocol versions to date. So old pickles continue to
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
97 # be readable forever. The pickler can generally be told to restrict itself to
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
98 # the subset of opcodes available under previous protocol versions too, so that
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
99 # users can create pickles under the current version readable by older
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
100 # versions. However, a pickle does not contain its version number embedded
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
101 # within it. If an older unpickler tries to read a pickle using a later
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
102 # protocol, the result is most likely an exception due to seeing an unknown (in
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
103 # the older unpickler) opcode.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
104 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
105 # The original pickle used what's now called "protocol 0", and what was called
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
106 # "text mode" before Python 2.3. The entire pickle bytestream is made up of
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
107 # printable 7-bit ASCII characters, plus the newline character, in protocol 0.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
108 # That's why it was called text mode. Protocol 0 is small and elegant, but
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
109 # sometimes painfully inefficient.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
110 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
111 # The second major set of additions is now called "protocol 1", and was called
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
112 # "binary mode" before Python 2.3. This added many opcodes with arguments
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
113 # consisting of arbitrary bytes, including NUL bytes and unprintable "high bit"
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
114 # bytes. Binary mode pickles can be substantially smaller than equivalent
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
115 # text mode pickles, and sometimes faster too; e.g., BININT represents a 4-byte
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
116 # int as 4 bytes following the opcode, which is cheaper to unpickle than the
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
117 # (perhaps) 11-character decimal string attached to INT. Protocol 1 also added
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
118 # a number of opcodes that operate on many stack elements at once (like APPENDS
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
119 # and SETITEMS), and "shortcut" opcodes (like EMPTY_DICT and EMPTY_TUPLE).
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
120 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
121 # The third major set of additions came in Python 2.3, and is called "protocol
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
122 # 2". This added:
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
123 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
124 # - A better way to pickle instances of new-style classes (NEWOBJ).
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
125 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
126 # - A way for a pickle to identify its protocol (PROTO).
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
127 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
128 # - Time- and space- efficient pickling of long ints (LONG{1,4}).
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
129 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
130 # - Shortcuts for small tuples (TUPLE{1,2,3}}.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
131 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
132 # - Dedicated opcodes for bools (NEWTRUE, NEWFALSE).
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
133 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
134 # - The "extension registry", a vector of popular objects that can be pushed
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
135 # efficiently by index (EXT{1,2,4}). This is akin to the memo and GET, but
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
136 # the registry contents are predefined (there's nothing akin to the memo's
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
137 # PUT).
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
138 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
139 # Another independent change with Python 2.3 is the abandonment of any
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
140 # pretense that it might be safe to load pickles received from untrusted
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
141 # parties -- no sufficient security analysis has been done to guarantee
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
142 # this and there isn't a use case that warrants the expense of such an
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
143 # analysis.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
144 #
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
145 # To this end, all tests for __safe_for_unpickling__ or for
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
146 # copyreg.safe_constructors are removed from the unpickling code.
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
147 # References to these variables in the descriptions below are to be seen
313bcff51900 Issue #17516: use comment syntax for comments, instead of multiline string
Victor Stinner <victor.stinner@gmail.com>
parents: 73958
diff changeset
148 # as describing unpickling in Python 2.2 and before.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
149
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
150
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
151 # Meta-rule: Descriptions are stored in instances of descriptor objects,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
152 # with plain constructors. No meta-language is defined from which
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
153 # descriptors could be constructed. If you want, e.g., XML, write a little
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
154 # program to generate XML from the objects.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
155
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
156 ##############################################################################
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
157 # Some pickle opcodes have an argument, following the opcode in the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
158 # bytestream. An argument is of a specific type, described by an instance
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
159 # of ArgumentDescriptor. These are not to be confused with arguments taken
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
160 # off the stack -- ArgumentDescriptor applies only to arguments embedded in
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
161 # the opcode stream, immediately following an opcode.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
162
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
163 # Represents the number of bytes consumed by an argument delimited by the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
164 # next newline character.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
165 UP_TO_NEWLINE = -1
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
166
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
167 # Represents the number of bytes consumed by a two-argument opcode where
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
168 # the first argument gives the number of bytes in the second argument.
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
169 TAKEN_FROM_ARGUMENT1 = -2 # num bytes is 1-byte unsigned int
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
170 TAKEN_FROM_ARGUMENT4 = -3 # num bytes is 4-byte signed little-endian int
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
171 TAKEN_FROM_ARGUMENT4U = -4 # num bytes is 4-byte unsigned little-endian int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
172 TAKEN_FROM_ARGUMENT8U = -5 # num bytes is 8-byte unsigned little-endian int
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
173
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
174 class ArgumentDescriptor(object):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
175 __slots__ = (
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
176 # name of descriptor record, also a module global name; a string
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
177 'name',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
178
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
179 # length of argument, in bytes; an int; UP_TO_NEWLINE and
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
180 # TAKEN_FROM_ARGUMENT{1,4,8} are negative values for variable-length
27435
430ed1c8e071 Removed the new LONG2 opcode: it's extravagant. If LONG1 isn't enough,
Tim Peters <tim.peters@gmail.com>
parents: 27434
diff changeset
181 # cases
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
182 'n',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
183
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
184 # a function taking a file-like object, reading this kind of argument
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
185 # from the object at the current position, advancing the current
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
186 # position by n bytes, and returning the value of the argument
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
187 'reader',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
188
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
189 # human-readable docs for this arg descriptor; a string
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
190 'doc',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
191 )
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
192
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
193 def __init__(self, name, n, reader, doc):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
194 assert isinstance(name, str)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
195 self.name = name
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
196
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
197 assert isinstance(n, int) and (n >= 0 or
27435
430ed1c8e071 Removed the new LONG2 opcode: it's extravagant. If LONG1 isn't enough,
Tim Peters <tim.peters@gmail.com>
parents: 27434
diff changeset
198 n in (UP_TO_NEWLINE,
430ed1c8e071 Removed the new LONG2 opcode: it's extravagant. If LONG1 isn't enough,
Tim Peters <tim.peters@gmail.com>
parents: 27434
diff changeset
199 TAKEN_FROM_ARGUMENT1,
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
200 TAKEN_FROM_ARGUMENT4,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
201 TAKEN_FROM_ARGUMENT4U,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
202 TAKEN_FROM_ARGUMENT8U))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
203 self.n = n
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
204
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
205 self.reader = reader
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
206
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
207 assert isinstance(doc, str)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
208 self.doc = doc
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
209
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
210 from struct import unpack as _unpack
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
211
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
212 def read_uint1(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
213 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
214 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
215 >>> read_uint1(io.BytesIO(b'\xff'))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
216 255
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
217 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
218
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
219 data = f.read(1)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
220 if data:
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
221 return data[0]
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
222 raise ValueError("not enough data in stream to read uint1")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
223
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
224 uint1 = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
225 name='uint1',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
226 n=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
227 reader=read_uint1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
228 doc="One-byte unsigned integer.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
229
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
230
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
231 def read_uint2(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
232 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
233 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
234 >>> read_uint2(io.BytesIO(b'\xff\x00'))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
235 255
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
236 >>> read_uint2(io.BytesIO(b'\xff\xff'))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
237 65535
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
238 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
239
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
240 data = f.read(2)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
241 if len(data) == 2:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
242 return _unpack("<H", data)[0]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
243 raise ValueError("not enough data in stream to read uint2")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
244
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
245 uint2 = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
246 name='uint2',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
247 n=2,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
248 reader=read_uint2,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
249 doc="Two-byte unsigned integer, little-endian.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
250
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
251
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
252 def read_int4(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
253 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
254 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
255 >>> read_int4(io.BytesIO(b'\xff\x00\x00\x00'))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
256 255
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
257 >>> read_int4(io.BytesIO(b'\x00\x00\x00\x80')) == -(2**31)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
258 True
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
259 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
260
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
261 data = f.read(4)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
262 if len(data) == 4:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
263 return _unpack("<i", data)[0]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
264 raise ValueError("not enough data in stream to read int4")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
265
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
266 int4 = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
267 name='int4',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
268 n=4,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
269 reader=read_int4,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
270 doc="Four-byte signed integer, little-endian, 2's complement.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
271
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
272
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
273 def read_uint4(f):
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
274 r"""
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
275 >>> import io
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
276 >>> read_uint4(io.BytesIO(b'\xff\x00\x00\x00'))
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
277 255
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
278 >>> read_uint4(io.BytesIO(b'\x00\x00\x00\x80')) == 2**31
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
279 True
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
280 """
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
281
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
282 data = f.read(4)
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
283 if len(data) == 4:
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
284 return _unpack("<I", data)[0]
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
285 raise ValueError("not enough data in stream to read uint4")
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
286
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
287 uint4 = ArgumentDescriptor(
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
288 name='uint4',
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
289 n=4,
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
290 reader=read_uint4,
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
291 doc="Four-byte unsigned integer, little-endian.")
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
292
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
293
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
294 def read_uint8(f):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
295 r"""
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
296 >>> import io
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
297 >>> read_uint8(io.BytesIO(b'\xff\x00\x00\x00\x00\x00\x00\x00'))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
298 255
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
299 >>> read_uint8(io.BytesIO(b'\xff' * 8)) == 2**64-1
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
300 True
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
301 """
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
302
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
303 data = f.read(8)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
304 if len(data) == 8:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
305 return _unpack("<Q", data)[0]
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
306 raise ValueError("not enough data in stream to read uint8")
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
307
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
308 uint8 = ArgumentDescriptor(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
309 name='uint8',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
310 n=8,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
311 reader=read_uint8,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
312 doc="Eight-byte unsigned integer, little-endian.")
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
313
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
314
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
315 def read_stringnl(f, decode=True, stripquotes=True):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
316 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
317 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
318 >>> read_stringnl(io.BytesIO(b"'abcd'\nefg\n"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
319 'abcd'
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
320
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
321 >>> read_stringnl(io.BytesIO(b"\n"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
322 Traceback (most recent call last):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
323 ...
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
324 ValueError: no string quotes around b''
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
325
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
326 >>> read_stringnl(io.BytesIO(b"\n"), stripquotes=False)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
327 ''
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
328
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
329 >>> read_stringnl(io.BytesIO(b"''\n"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
330 ''
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
331
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
332 >>> read_stringnl(io.BytesIO(b'"abcd"'))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
333 Traceback (most recent call last):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
334 ...
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
335 ValueError: no newline found when trying to read stringnl
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
336
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
337 Embedded escapes are undone in the result.
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
338 >>> read_stringnl(io.BytesIO(br"'a\n\\b\x00c\td'" + b"\n'e'"))
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
339 'a\n\\b\x00c\td'
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
340 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
341
42295
5337c3a4a273 Make test_pickletools.py pass.
Guido van Rossum <guido@python.org>
parents: 42064
diff changeset
342 data = f.readline()
42919
c72a3cc21ef7 More str/bytes fixes.
Guido van Rossum <guido@python.org>
parents: 42501
diff changeset
343 if not data.endswith(b'\n'):
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
344 raise ValueError("no newline found when trying to read stringnl")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
345 data = data[:-1] # lose the newline
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
346
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
347 if stripquotes:
42919
c72a3cc21ef7 More str/bytes fixes.
Guido van Rossum <guido@python.org>
parents: 42501
diff changeset
348 for q in (b'"', b"'"):
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
349 if data.startswith(q):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
350 if not data.endswith(q):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
351 raise ValueError("strinq quote %r not found at both "
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
352 "ends of %r" % (q, data))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
353 data = data[1:-1]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
354 break
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
355 else:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
356 raise ValueError("no string quotes around %r" % data)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
357
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
358 if decode:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
359 data = codecs.escape_decode(data)[0].decode("ascii")
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
360 return data
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
361
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
362 stringnl = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
363 name='stringnl',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
364 n=UP_TO_NEWLINE,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
365 reader=read_stringnl,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
366 doc="""A newline-terminated string.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
367
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
368 This is a repr-style string, with embedded escapes, and
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
369 bracketing quotes.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
370 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
371
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
372 def read_stringnl_noescape(f):
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
373 return read_stringnl(f, stripquotes=False)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
374
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
375 stringnl_noescape = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
376 name='stringnl_noescape',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
377 n=UP_TO_NEWLINE,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
378 reader=read_stringnl_noescape,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
379 doc="""A newline-terminated string.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
380
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
381 This is a str-style string, without embedded escapes,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
382 or bracketing quotes. It should consist solely of
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
383 printable ASCII characters.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
384 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
385
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
386 def read_stringnl_noescape_pair(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
387 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
388 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
389 >>> read_stringnl_noescape_pair(io.BytesIO(b"Queue\nEmpty\njunk"))
27422
24c8fbec761f stringnl_noescape_pair(): changed the reader to separate the two
Tim Peters <tim.peters@gmail.com>
parents: 27421
diff changeset
390 'Queue Empty'
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
391 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
392
27422
24c8fbec761f stringnl_noescape_pair(): changed the reader to separate the two
Tim Peters <tim.peters@gmail.com>
parents: 27421
diff changeset
393 return "%s %s" % (read_stringnl_noescape(f), read_stringnl_noescape(f))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
394
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
395 stringnl_noescape_pair = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
396 name='stringnl_noescape_pair',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
397 n=UP_TO_NEWLINE,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
398 reader=read_stringnl_noescape_pair,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
399 doc="""A pair of newline-terminated strings.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
400
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
401 These are str-style strings, without embedded
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
402 escapes, or bracketing quotes. They should
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
403 consist solely of printable ASCII characters.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
404 The pair is returned as a single string, with
27422
24c8fbec761f stringnl_noescape_pair(): changed the reader to separate the two
Tim Peters <tim.peters@gmail.com>
parents: 27421
diff changeset
405 a single blank separating the two strings.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
406 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
407
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
408
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
409 def read_string1(f):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
410 r"""
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
411 >>> import io
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
412 >>> read_string1(io.BytesIO(b"\x00"))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
413 ''
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
414 >>> read_string1(io.BytesIO(b"\x03abcdef"))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
415 'abc'
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
416 """
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
417
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
418 n = read_uint1(f)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
419 assert n >= 0
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
420 data = f.read(n)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
421 if len(data) == n:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
422 return data.decode("latin-1")
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
423 raise ValueError("expected %d bytes in a string1, but only %d remain" %
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
424 (n, len(data)))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
425
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
426 string1 = ArgumentDescriptor(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
427 name="string1",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
428 n=TAKEN_FROM_ARGUMENT1,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
429 reader=read_string1,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
430 doc="""A counted string.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
431
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
432 The first argument is a 1-byte unsigned int giving the number
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
433 of bytes in the string, and the second argument is that many
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
434 bytes.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
435 """)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
436
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
437
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
438 def read_string4(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
439 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
440 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
441 >>> read_string4(io.BytesIO(b"\x00\x00\x00\x00abc"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
442 ''
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
443 >>> read_string4(io.BytesIO(b"\x03\x00\x00\x00abcdef"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
444 'abc'
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
445 >>> read_string4(io.BytesIO(b"\x00\x00\x00\x03abcdef"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
446 Traceback (most recent call last):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
447 ...
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
448 ValueError: expected 50331648 bytes in a string4, but only 6 remain
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
449 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
450
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
451 n = read_int4(f)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
452 if n < 0:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
453 raise ValueError("string4 byte count < 0: %d" % n)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
454 data = f.read(n)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
455 if len(data) == n:
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
456 return data.decode("latin-1")
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
457 raise ValueError("expected %d bytes in a string4, but only %d remain" %
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
458 (n, len(data)))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
459
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
460 string4 = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
461 name="string4",
27435
430ed1c8e071 Removed the new LONG2 opcode: it's extravagant. If LONG1 isn't enough,
Tim Peters <tim.peters@gmail.com>
parents: 27434
diff changeset
462 n=TAKEN_FROM_ARGUMENT4,
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
463 reader=read_string4,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
464 doc="""A counted string.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
465
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
466 The first argument is a 4-byte little-endian signed int giving
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
467 the number of bytes in the string, and the second argument is
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
468 that many bytes.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
469 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
470
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
471
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
472 def read_bytes1(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
473 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
474 >>> import io
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
475 >>> read_bytes1(io.BytesIO(b"\x00"))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
476 b''
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
477 >>> read_bytes1(io.BytesIO(b"\x03abcdef"))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
478 b'abc'
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
479 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
480
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
481 n = read_uint1(f)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
482 assert n >= 0
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
483 data = f.read(n)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
484 if len(data) == n:
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
485 return data
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
486 raise ValueError("expected %d bytes in a bytes1, but only %d remain" %
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
487 (n, len(data)))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
488
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
489 bytes1 = ArgumentDescriptor(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
490 name="bytes1",
27435
430ed1c8e071 Removed the new LONG2 opcode: it's extravagant. If LONG1 isn't enough,
Tim Peters <tim.peters@gmail.com>
parents: 27434
diff changeset
491 n=TAKEN_FROM_ARGUMENT1,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
492 reader=read_bytes1,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
493 doc="""A counted bytes string.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
494
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
495 The first argument is a 1-byte unsigned int giving the number
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
496 of bytes in the string, and the second argument is that many
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
497 bytes.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
498 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
499
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
500
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
501 def read_bytes1(f):
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
502 r"""
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
503 >>> import io
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
504 >>> read_bytes1(io.BytesIO(b"\x00"))
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
505 b''
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
506 >>> read_bytes1(io.BytesIO(b"\x03abcdef"))
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
507 b'abc'
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
508 """
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
509
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
510 n = read_uint1(f)
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
511 assert n >= 0
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
512 data = f.read(n)
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
513 if len(data) == n:
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
514 return data
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
515 raise ValueError("expected %d bytes in a bytes1, but only %d remain" %
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
516 (n, len(data)))
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
517
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
518 bytes1 = ArgumentDescriptor(
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
519 name="bytes1",
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
520 n=TAKEN_FROM_ARGUMENT1,
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
521 reader=read_bytes1,
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
522 doc="""A counted bytes string.
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
523
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
524 The first argument is a 1-byte unsigned int giving the number
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
525 of bytes, and the second argument is that many bytes.
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
526 """)
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
527
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
528
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
529 def read_bytes4(f):
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
530 r"""
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
531 >>> import io
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
532 >>> read_bytes4(io.BytesIO(b"\x00\x00\x00\x00abc"))
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
533 b''
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
534 >>> read_bytes4(io.BytesIO(b"\x03\x00\x00\x00abcdef"))
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
535 b'abc'
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
536 >>> read_bytes4(io.BytesIO(b"\x00\x00\x00\x03abcdef"))
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
537 Traceback (most recent call last):
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
538 ...
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
539 ValueError: expected 50331648 bytes in a bytes4, but only 6 remain
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
540 """
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
541
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
542 n = read_uint4(f)
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
543 assert n >= 0
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
544 if n > sys.maxsize:
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
545 raise ValueError("bytes4 byte count > sys.maxsize: %d" % n)
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
546 data = f.read(n)
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
547 if len(data) == n:
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
548 return data
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
549 raise ValueError("expected %d bytes in a bytes4, but only %d remain" %
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
550 (n, len(data)))
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
551
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
552 bytes4 = ArgumentDescriptor(
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
553 name="bytes4",
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
554 n=TAKEN_FROM_ARGUMENT4U,
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
555 reader=read_bytes4,
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
556 doc="""A counted bytes string.
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
557
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
558 The first argument is a 4-byte little-endian unsigned int giving
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
559 the number of bytes, and the second argument is that many bytes.
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
560 """)
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
561
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
562
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
563 def read_bytes8(f):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
564 r"""
87457
eb6fd9e96015 Fix test_pickletools.py doctest's on 32-bit platforms. I hate doctests.
Gregory P. Smith <greg@krypto.org>
parents: 87438
diff changeset
565 >>> import io, struct, sys
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
566 >>> read_bytes8(io.BytesIO(b"\x00\x00\x00\x00\x00\x00\x00\x00abc"))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
567 b''
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
568 >>> read_bytes8(io.BytesIO(b"\x03\x00\x00\x00\x00\x00\x00\x00abcdef"))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
569 b'abc'
87457
eb6fd9e96015 Fix test_pickletools.py doctest's on 32-bit platforms. I hate doctests.
Gregory P. Smith <greg@krypto.org>
parents: 87438
diff changeset
570 >>> bigsize8 = struct.pack("<Q", sys.maxsize//3)
eb6fd9e96015 Fix test_pickletools.py doctest's on 32-bit platforms. I hate doctests.
Gregory P. Smith <greg@krypto.org>
parents: 87438
diff changeset
571 >>> read_bytes8(io.BytesIO(bigsize8 + b"abcdef")) #doctest: +ELLIPSIS
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
572 Traceback (most recent call last):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
573 ...
87457
eb6fd9e96015 Fix test_pickletools.py doctest's on 32-bit platforms. I hate doctests.
Gregory P. Smith <greg@krypto.org>
parents: 87438
diff changeset
574 ValueError: expected ... bytes in a bytes8, but only 6 remain
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
575 """
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
576
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
577 n = read_uint8(f)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
578 assert n >= 0
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
579 if n > sys.maxsize:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
580 raise ValueError("bytes8 byte count > sys.maxsize: %d" % n)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
581 data = f.read(n)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
582 if len(data) == n:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
583 return data
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
584 raise ValueError("expected %d bytes in a bytes8, but only %d remain" %
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
585 (n, len(data)))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
586
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
587 bytes8 = ArgumentDescriptor(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
588 name="bytes8",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
589 n=TAKEN_FROM_ARGUMENT8U,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
590 reader=read_bytes8,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
591 doc="""A counted bytes string.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
592
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
593 The first argument is a 8-byte little-endian unsigned int giving
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
594 the number of bytes, and the second argument is that many bytes.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
595 """)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
596
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
597 def read_unicodestringnl(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
598 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
599 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
600 >>> read_unicodestringnl(io.BytesIO(b"abc\\uabcd\njunk")) == 'abc\uabcd'
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
601 True
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
602 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
603
42295
5337c3a4a273 Make test_pickletools.py pass.
Guido van Rossum <guido@python.org>
parents: 42064
diff changeset
604 data = f.readline()
42919
c72a3cc21ef7 More str/bytes fixes.
Guido van Rossum <guido@python.org>
parents: 42501
diff changeset
605 if not data.endswith(b'\n'):
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
606 raise ValueError("no newline found when trying to read "
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
607 "unicodestringnl")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
608 data = data[:-1] # lose the newline
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 40930
diff changeset
609 return str(data, 'raw-unicode-escape')
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
610
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
611 unicodestringnl = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
612 name='unicodestringnl',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
613 n=UP_TO_NEWLINE,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
614 reader=read_unicodestringnl,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
615 doc="""A newline-terminated Unicode string.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
616
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
617 This is raw-unicode-escape encoded, so consists of
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
618 printable ASCII characters, and may contain embedded
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
619 escape sequences.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
620 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
621
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
622
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
623 def read_unicodestring1(f):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
624 r"""
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
625 >>> import io
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
626 >>> s = 'abcd\uabcd'
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
627 >>> enc = s.encode('utf-8')
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
628 >>> enc
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
629 b'abcd\xea\xaf\x8d'
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
630 >>> n = bytes([len(enc)]) # little-endian 1-byte length
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
631 >>> t = read_unicodestring1(io.BytesIO(n + enc + b'junk'))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
632 >>> s == t
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
633 True
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
634
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
635 >>> read_unicodestring1(io.BytesIO(n + enc[:-1]))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
636 Traceback (most recent call last):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
637 ...
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
638 ValueError: expected 7 bytes in a unicodestring1, but only 6 remain
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
639 """
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
640
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
641 n = read_uint1(f)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
642 assert n >= 0
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
643 data = f.read(n)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
644 if len(data) == n:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
645 return str(data, 'utf-8', 'surrogatepass')
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
646 raise ValueError("expected %d bytes in a unicodestring1, but only %d "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
647 "remain" % (n, len(data)))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
648
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
649 unicodestring1 = ArgumentDescriptor(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
650 name="unicodestring1",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
651 n=TAKEN_FROM_ARGUMENT1,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
652 reader=read_unicodestring1,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
653 doc="""A counted Unicode string.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
654
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
655 The first argument is a 1-byte little-endian signed int
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
656 giving the number of bytes in the string, and the second
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
657 argument-- the UTF-8 encoding of the Unicode string --
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
658 contains that many bytes.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
659 """)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
660
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
661
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
662 def read_unicodestring4(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
663 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
664 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
665 >>> s = 'abcd\uabcd'
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
666 >>> enc = s.encode('utf-8')
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
667 >>> enc
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
668 b'abcd\xea\xaf\x8d'
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
669 >>> n = bytes([len(enc), 0, 0, 0]) # little-endian 4-byte length
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
670 >>> t = read_unicodestring4(io.BytesIO(n + enc + b'junk'))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
671 >>> s == t
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
672 True
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
673
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
674 >>> read_unicodestring4(io.BytesIO(n + enc[:-1]))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
675 Traceback (most recent call last):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
676 ...
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
677 ValueError: expected 7 bytes in a unicodestring4, but only 6 remain
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
678 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
679
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
680 n = read_uint4(f)
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
681 assert n >= 0
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
682 if n > sys.maxsize:
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
683 raise ValueError("unicodestring4 byte count > sys.maxsize: %d" % n)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
684 data = f.read(n)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
685 if len(data) == n:
60577
7994269ad3a1 Issue #8383: pickle and pickletools use surrogatepass error handler when
Victor Stinner <victor.stinner@haypocalc.com>
parents: 54584
diff changeset
686 return str(data, 'utf-8', 'surrogatepass')
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
687 raise ValueError("expected %d bytes in a unicodestring4, but only %d "
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
688 "remain" % (n, len(data)))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
689
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
690 unicodestring4 = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
691 name="unicodestring4",
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
692 n=TAKEN_FROM_ARGUMENT4U,
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
693 reader=read_unicodestring4,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
694 doc="""A counted Unicode string.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
695
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
696 The first argument is a 4-byte little-endian signed int
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
697 giving the number of bytes in the string, and the second
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
698 argument-- the UTF-8 encoding of the Unicode string --
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
699 contains that many bytes.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
700 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
701
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
702
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
703 def read_unicodestring8(f):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
704 r"""
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
705 >>> import io
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
706 >>> s = 'abcd\uabcd'
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
707 >>> enc = s.encode('utf-8')
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
708 >>> enc
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
709 b'abcd\xea\xaf\x8d'
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
710 >>> n = bytes([len(enc)]) + bytes(7) # little-endian 8-byte length
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
711 >>> t = read_unicodestring8(io.BytesIO(n + enc + b'junk'))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
712 >>> s == t
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
713 True
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
714
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
715 >>> read_unicodestring8(io.BytesIO(n + enc[:-1]))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
716 Traceback (most recent call last):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
717 ...
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
718 ValueError: expected 7 bytes in a unicodestring8, but only 6 remain
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
719 """
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
720
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
721 n = read_uint8(f)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
722 assert n >= 0
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
723 if n > sys.maxsize:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
724 raise ValueError("unicodestring8 byte count > sys.maxsize: %d" % n)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
725 data = f.read(n)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
726 if len(data) == n:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
727 return str(data, 'utf-8', 'surrogatepass')
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
728 raise ValueError("expected %d bytes in a unicodestring8, but only %d "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
729 "remain" % (n, len(data)))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
730
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
731 unicodestring8 = ArgumentDescriptor(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
732 name="unicodestring8",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
733 n=TAKEN_FROM_ARGUMENT8U,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
734 reader=read_unicodestring8,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
735 doc="""A counted Unicode string.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
736
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
737 The first argument is a 8-byte little-endian signed int
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
738 giving the number of bytes in the string, and the second
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
739 argument-- the UTF-8 encoding of the Unicode string --
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
740 contains that many bytes.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
741 """)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
742
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
743
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
744 def read_decimalnl_short(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
745 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
746 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
747 >>> read_decimalnl_short(io.BytesIO(b"1234\n56"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
748 1234
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
749
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
750 >>> read_decimalnl_short(io.BytesIO(b"1234L\n56"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
751 Traceback (most recent call last):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
752 ...
85418
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 85221
diff changeset
753 ValueError: invalid literal for int() with base 10: b'1234L'
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
754 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
755
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
756 s = read_stringnl(f, decode=False, stripquotes=False)
85418
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 85221
diff changeset
757
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 85221
diff changeset
758 # There's a hack for True and False here.
43003
cd80eb989728 Change string literals to bytes, since they are compared to bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42919
diff changeset
759 if s == b"00":
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
760 return False
43003
cd80eb989728 Change string literals to bytes, since they are compared to bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42919
diff changeset
761 elif s == b"01":
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
762 return True
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
763
73076
67053b135ed9 Cleanup code: remove int/long idioms and simplify a while statement.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 68586
diff changeset
764 return int(s)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
765
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
766 def read_decimalnl_long(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
767 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
768 >>> import io
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
769
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
770 >>> read_decimalnl_long(io.BytesIO(b"1234L\n56"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
771 1234
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
772
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
773 >>> read_decimalnl_long(io.BytesIO(b"123456789012345678901234L\n6"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
774 123456789012345678901234
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
775 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
776
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
777 s = read_stringnl(f, decode=False, stripquotes=False)
50946
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
778 if s[-1:] == b'L':
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
779 s = s[:-1]
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40640
diff changeset
780 return int(s)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
781
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
782
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
783 decimalnl_short = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
784 name='decimalnl_short',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
785 n=UP_TO_NEWLINE,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
786 reader=read_decimalnl_short,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
787 doc="""A newline-terminated decimal integer literal.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
788
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
789 This never has a trailing 'L', and the integer fit
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
790 in a short Python int on the box where the pickle
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
791 was written -- but there's no guarantee it will fit
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
792 in a short Python int on the box where the pickle
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
793 is read.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
794 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
795
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
796 decimalnl_long = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
797 name='decimalnl_long',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
798 n=UP_TO_NEWLINE,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
799 reader=read_decimalnl_long,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
800 doc="""A newline-terminated decimal integer literal.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
801
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
802 This has a trailing 'L', and can represent integers
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
803 of any size.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
804 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
805
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
806
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
807 def read_floatnl(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
808 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
809 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
810 >>> read_floatnl(io.BytesIO(b"-1.25\n6"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
811 -1.25
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
812 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
813 s = read_stringnl(f, decode=False, stripquotes=False)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
814 return float(s)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
815
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
816 floatnl = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
817 name='floatnl',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
818 n=UP_TO_NEWLINE,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
819 reader=read_floatnl,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
820 doc="""A newline-terminated decimal floating literal.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
821
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
822 In general this requires 17 significant digits for roundtrip
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
823 identity, and pickling then unpickling infinities, NaNs, and
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
824 minus zero doesn't work across boxes, or on some boxes even
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
825 on itself (e.g., Windows can't read the strings it produces
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
826 for infinities or NaNs).
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
827 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
828
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
829 def read_float8(f):
27473
154bac0b26a5 Use raw-mode docstring whenever there's an escape code in an example --
Tim Peters <tim.peters@gmail.com>
parents: 27472
diff changeset
830 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
831 >>> import io, struct
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
832 >>> raw = struct.pack(">d", -1.25)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
833 >>> raw
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
834 b'\xbf\xf4\x00\x00\x00\x00\x00\x00'
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
835 >>> read_float8(io.BytesIO(raw + b"\n"))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
836 -1.25
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
837 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
838
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
839 data = f.read(8)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
840 if len(data) == 8:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
841 return _unpack(">d", data)[0]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
842 raise ValueError("not enough data in stream to read float8")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
843
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
844
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
845 float8 = ArgumentDescriptor(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
846 name='float8',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
847 n=8,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
848 reader=read_float8,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
849 doc="""An 8-byte binary representation of a float, big-endian.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
850
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
851 The format is unique to Python, and shared with the struct
42322
3e33215c66ed Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents: 42295
diff changeset
852 module (format string '>d') "in theory" (the struct and pickle
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
853 implementations don't share the code -- they should). It's
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
854 strongly related to the IEEE-754 double format, and, in normal
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
855 cases, is in fact identical to the big-endian 754 double format.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
856 On other boxes the dynamic range is limited to that of a 754
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
857 double, and "add a half and chop" rounding is used to reduce
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
858 the precision to 53 bits. However, even on a 754 box,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
859 infinities, NaNs, and minus zero may not be handled correctly
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
860 (may not survive roundtrip pickling intact).
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
861 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
862
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
863 # Protocol 2 formats
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
864
27506
b480a4f707f6 pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents: 27473
diff changeset
865 from pickle import decode_long
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
866
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
867 def read_long1(f):
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
868 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
869 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
870 >>> read_long1(io.BytesIO(b"\x00"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
871 0
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
872 >>> read_long1(io.BytesIO(b"\x02\xff\x00"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
873 255
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
874 >>> read_long1(io.BytesIO(b"\x02\xff\x7f"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
875 32767
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
876 >>> read_long1(io.BytesIO(b"\x02\x00\xff"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
877 -256
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
878 >>> read_long1(io.BytesIO(b"\x02\x00\x80"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
879 -32768
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
880 """
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
881
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
882 n = read_uint1(f)
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
883 data = f.read(n)
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
884 if len(data) != n:
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
885 raise ValueError("not enough data in stream to read long1")
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
886 return decode_long(data)
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
887
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
888 long1 = ArgumentDescriptor(
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
889 name="long1",
27435
430ed1c8e071 Removed the new LONG2 opcode: it's extravagant. If LONG1 isn't enough,
Tim Peters <tim.peters@gmail.com>
parents: 27434
diff changeset
890 n=TAKEN_FROM_ARGUMENT1,
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
891 reader=read_long1,
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
892 doc="""A binary long, little-endian, using 1-byte size.
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
893
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
894 This first reads one byte as an unsigned size, then reads that
27434
7116cb5bac50 Documented the 2's-comp business for the new long opcodes.
Tim Peters <tim.peters@gmail.com>
parents: 27433
diff changeset
895 many bytes and interprets them as a little-endian 2's-complement long.
27552
50c734250e05 It's Official: for LONG1/LONG4, a "byte count" of 0 is taken as a
Tim Peters <tim.peters@gmail.com>
parents: 27535
diff changeset
896 If the size is 0, that's taken as a shortcut for the long 0L.
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
897 """)
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
898
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
899 def read_long4(f):
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
900 r"""
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
901 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
902 >>> read_long4(io.BytesIO(b"\x02\x00\x00\x00\xff\x00"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
903 255
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
904 >>> read_long4(io.BytesIO(b"\x02\x00\x00\x00\xff\x7f"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
905 32767
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
906 >>> read_long4(io.BytesIO(b"\x02\x00\x00\x00\x00\xff"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
907 -256
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
908 >>> read_long4(io.BytesIO(b"\x02\x00\x00\x00\x00\x80"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
909 -32768
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
910 >>> read_long1(io.BytesIO(b"\x00\x00\x00\x00"))
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38397
diff changeset
911 0
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
912 """
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
913
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
914 n = read_int4(f)
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
915 if n < 0:
27436
60ff1ba428c3 Use proper function name in exception messages
Neal Norwitz <nnorwitz@gmail.com>
parents: 27435
diff changeset
916 raise ValueError("long4 byte count < 0: %d" % n)
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
917 data = f.read(n)
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
918 if len(data) != n:
27436
60ff1ba428c3 Use proper function name in exception messages
Neal Norwitz <nnorwitz@gmail.com>
parents: 27435
diff changeset
919 raise ValueError("not enough data in stream to read long4")
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
920 return decode_long(data)
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
921
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
922 long4 = ArgumentDescriptor(
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
923 name="long4",
27435
430ed1c8e071 Removed the new LONG2 opcode: it's extravagant. If LONG1 isn't enough,
Tim Peters <tim.peters@gmail.com>
parents: 27434
diff changeset
924 n=TAKEN_FROM_ARGUMENT4,
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
925 reader=read_long4,
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
926 doc="""A binary representation of a long, little-endian.
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
927
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
928 This first reads four bytes as a signed size (but requires the
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
929 size to be >= 0), then reads that many bytes and interprets them
27552
50c734250e05 It's Official: for LONG1/LONG4, a "byte count" of 0 is taken as a
Tim Peters <tim.peters@gmail.com>
parents: 27535
diff changeset
930 as a little-endian 2's-complement long. If the size is 0, that's taken
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40640
diff changeset
931 as a shortcut for the int 0, although LONG1 should really be used
27552
50c734250e05 It's Official: for LONG1/LONG4, a "byte count" of 0 is taken as a
Tim Peters <tim.peters@gmail.com>
parents: 27535
diff changeset
932 then instead (and in any case where # of bytes < 256).
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
933 """)
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
934
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
935
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
936 ##############################################################################
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
937 # Object descriptors. The stack used by the pickle machine holds objects,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
938 # and in the stack_before and stack_after attributes of OpcodeInfo
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
939 # descriptors we need names to describe the various types of objects that can
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
940 # appear on the stack.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
941
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
942 class StackObject(object):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
943 __slots__ = (
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
944 # name of descriptor record, for info only
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
945 'name',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
946
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
947 # type of object, or tuple of type objects (meaning the object can
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
948 # be of any type in the tuple)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
949 'obtype',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
950
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
951 # human-readable docs for this kind of stack object; a string
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
952 'doc',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
953 )
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
954
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
955 def __init__(self, name, obtype, doc):
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43003
diff changeset
956 assert isinstance(name, str)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
957 self.name = name
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
958
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
959 assert isinstance(obtype, type) or isinstance(obtype, tuple)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
960 if isinstance(obtype, tuple):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
961 for contained in obtype:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
962 assert isinstance(contained, type)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
963 self.obtype = obtype
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
964
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43003
diff changeset
965 assert isinstance(doc, str)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
966 self.doc = doc
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
967
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
968 def __repr__(self):
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
969 return self.name
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
970
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
971
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
972 pyint = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
973 name='int',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
974 obtype=int,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
975 doc="A short (as opposed to long) Python integer object.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
976
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
977 pylong = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
978 name='long',
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40640
diff changeset
979 obtype=int,
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
980 doc="A long (as opposed to short) Python integer object.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
981
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
982 pyinteger_or_bool = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
983 name='int_or_bool',
64010
656fe6292f9f Merged revisions 83212,83829,83833,83838-83839,83878,84019,84025,84028,84032,84036 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63041
diff changeset
984 obtype=(int, bool),
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
985 doc="A Python integer object (short or long), or "
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
986 "a Python bool.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
987
27429
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
988 pybool = StackObject(
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
989 name='bool',
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
990 obtype=(bool,),
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
991 doc="A Python bool object.")
b465c9d2196a Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents: 27424
diff changeset
992
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
993 pyfloat = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
994 name='float',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
995 obtype=float,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
996 doc="A Python float object.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
997
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
998 pystring = StackObject(
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
999 name='string',
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1000 obtype=bytes,
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1001 doc="A Python (8-bit) string object.")
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1002
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1003 pybytes = StackObject(
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1004 name='bytes',
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1005 obtype=bytes,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1006 doc="A Python bytes object.")
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1007
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1008 pyunicode = StackObject(
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43664
diff changeset
1009 name='str',
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 40930
diff changeset
1010 obtype=str,
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1011 doc="A Python (Unicode) string object.")
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1012
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1013 pynone = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1014 name="None",
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1015 obtype=type(None),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1016 doc="The Python None object.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1017
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1018 pytuple = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1019 name="tuple",
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1020 obtype=tuple,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1021 doc="A Python tuple object.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1022
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1023 pylist = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1024 name="list",
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1025 obtype=list,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1026 doc="A Python list object.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1027
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1028 pydict = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1029 name="dict",
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1030 obtype=dict,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1031 doc="A Python dict object.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1032
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1033 pyset = StackObject(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1034 name="set",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1035 obtype=set,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1036 doc="A Python set object.")
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1037
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1038 pyfrozenset = StackObject(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1039 name="frozenset",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1040 obtype=set,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1041 doc="A Python frozenset object.")
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1042
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1043 anyobject = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1044 name='any',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1045 obtype=object,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1046 doc="Any kind of object whatsoever.")
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1047
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1048 markobject = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1049 name="mark",
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1050 obtype=StackObject,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1051 doc="""'The mark' is a unique object.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1052
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1053 Opcodes that operate on a variable number of objects
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1054 generally don't embed the count of objects in the opcode,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1055 or pull it off the stack. Instead the MARK opcode is used
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1056 to push a special marker object on the stack, and then
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1057 some other opcodes grab all the objects from the top of
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1058 the stack down to (but not including) the topmost marker
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1059 object.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1060 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1061
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1062 stackslice = StackObject(
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1063 name="stackslice",
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1064 obtype=StackObject,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1065 doc="""An object representing a contiguous slice of the stack.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1066
85221
e07f104133d5 #18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83360
diff changeset
1067 This is used in conjunction with markobject, to represent all
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1068 of the stack following the topmost markobject. For example,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1069 the POP_MARK opcode changes the stack from
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1070
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1071 [..., markobject, stackslice]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1072 to
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1073 [...]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1074
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1075 No matter how many object are on the stack after the topmost
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1076 markobject, POP_MARK gets rid of all of them (including the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1077 topmost markobject too).
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1078 """)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1079
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1080 ##############################################################################
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1081 # Descriptors for pickle opcodes.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1082
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1083 class OpcodeInfo(object):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1084
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1085 __slots__ = (
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1086 # symbolic name of opcode; a string
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1087 'name',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1088
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1089 # the code used in a bytestream to represent the opcode; a
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1090 # one-character string
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1091 'code',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1092
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1093 # If the opcode has an argument embedded in the byte string, an
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1094 # instance of ArgumentDescriptor specifying its type. Note that
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1095 # arg.reader(s) can be used to read and decode the argument from
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1096 # the bytestream s, and arg.doc documents the format of the raw
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1097 # argument bytes. If the opcode doesn't have an argument embedded
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1098 # in the bytestream, arg should be None.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1099 'arg',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1100
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1101 # what the stack looks like before this opcode runs; a list
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1102 'stack_before',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1103
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1104 # what the stack looks like after this opcode runs; a list
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1105 'stack_after',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1106
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1107 # the protocol number in which this opcode was introduced; an int
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1108 'proto',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1109
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1110 # human-readable docs for this opcode; a string
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1111 'doc',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1112 )
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1113
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1114 def __init__(self, name, code, arg,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1115 stack_before, stack_after, proto, doc):
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43003
diff changeset
1116 assert isinstance(name, str)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1117 self.name = name
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1118
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43003
diff changeset
1119 assert isinstance(code, str)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1120 assert len(code) == 1
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1121 self.code = code
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1122
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1123 assert arg is None or isinstance(arg, ArgumentDescriptor)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1124 self.arg = arg
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1125
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1126 assert isinstance(stack_before, list)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1127 for x in stack_before:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1128 assert isinstance(x, StackObject)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1129 self.stack_before = stack_before
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1130
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1131 assert isinstance(stack_after, list)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1132 for x in stack_after:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1133 assert isinstance(x, StackObject)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1134 self.stack_after = stack_after
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1135
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1136 assert isinstance(proto, int) and 0 <= proto <= pickle.HIGHEST_PROTOCOL
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1137 self.proto = proto
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1138
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43003
diff changeset
1139 assert isinstance(doc, str)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1140 self.doc = doc
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1141
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1142 I = OpcodeInfo
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1143 opcodes = [
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1144
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1145 # Ways to spell integers.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1146
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1147 I(name='INT',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1148 code='I',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1149 arg=decimalnl_short,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1150 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1151 stack_after=[pyinteger_or_bool],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1152 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1153 doc="""Push an integer or bool.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1154
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1155 The argument is a newline-terminated decimal literal string.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1156
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1157 The intent may have been that this always fit in a short Python int,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1158 but INT can be generated in pickles written on a 64-bit box that
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1159 require a Python long on a 32-bit box. The difference between this
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1160 and LONG then is that INT skips a trailing 'L', and produces a short
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1161 int whenever possible.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1162
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1163 Another difference is due to that, when bool was introduced as a
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1164 distinct type in 2.3, builtin names True and False were also added to
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1165 2.2.2, mapping to ints 1 and 0. For compatibility in both directions,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1166 True gets pickled as INT + "I01\\n", and False as INT + "I00\\n".
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1167 Leading zeroes are never produced for a genuine integer. The 2.3
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1168 (and later) unpicklers special-case these and return bool instead;
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1169 earlier unpicklers ignore the leading "0" and return the int.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1170 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1171
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1172 I(name='BININT',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1173 code='J',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1174 arg=int4,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1175 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1176 stack_after=[pyint],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1177 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1178 doc="""Push a four-byte signed integer.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1179
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1180 This handles the full range of Python (short) integers on a 32-bit
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1181 box, directly as binary bytes (1 for the opcode and 4 for the integer).
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1182 If the integer is non-negative and fits in 1 or 2 bytes, pickling via
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1183 BININT1 or BININT2 saves space.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1184 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1185
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1186 I(name='BININT1',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1187 code='K',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1188 arg=uint1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1189 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1190 stack_after=[pyint],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1191 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1192 doc="""Push a one-byte unsigned integer.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1193
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1194 This is a space optimization for pickling very small non-negative ints,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1195 in range(256).
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1196 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1197
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1198 I(name='BININT2',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1199 code='M',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1200 arg=uint2,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1201 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1202 stack_after=[pyint],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1203 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1204 doc="""Push a two-byte unsigned integer.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1205
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1206 This is a space optimization for pickling small positive ints, in
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1207 range(256, 2**16). Integers in range(256) can also be pickled via
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1208 BININT2, but BININT1 instead saves a byte.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1209 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1210
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1211 I(name='LONG',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1212 code='L',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1213 arg=decimalnl_long,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1214 stack_before=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1215 stack_after=[pylong],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1216 proto=0,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1217 doc="""Push a long integer.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1218
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1219 The same as INT, except that the literal ends with 'L', and always
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1220 unpickles to a Python long. There doesn't seem a real purpose to the
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1221 trailing 'L'.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1222
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1223 Note that LONG takes time quadratic in the number of digits when
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1224 unpickling (this is simply due to the nature of decimal->binary
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1225 conversion). Proto 2 added linear-time (in C; still quadratic-time
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1226 in Python) LONG1 and LONG4 opcodes.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1227 """),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1228
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1229 I(name="LONG1",
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1230 code='\x8a',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1231 arg=long1,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1232 stack_before=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1233 stack_after=[pylong],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1234 proto=2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1235 doc="""Long integer using one-byte length.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1236
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1237 A more efficient encoding of a Python long; the long1 encoding
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1238 says it all."""),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1239
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1240 I(name="LONG4",
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1241 code='\x8b',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1242 arg=long4,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1243 stack_before=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1244 stack_after=[pylong],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1245 proto=2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1246 doc="""Long integer using found-byte length.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1247
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1248 A more efficient encoding of a Python long; the long4 encoding
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1249 says it all."""),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1250
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1251 # Ways to spell strings (8-bit, not Unicode).
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1252
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1253 I(name='STRING',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1254 code='S',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1255 arg=stringnl,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1256 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1257 stack_after=[pystring],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1258 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1259 doc="""Push a Python string object.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1260
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1261 The argument is a repr-style string, with bracketing quote characters,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1262 and perhaps embedded escapes. The argument extends until the next
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1263 newline character. (Actually, they are decoded into a str instance
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1264 using the encoding given to the Unpickler constructor. or the default,
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1265 'ASCII'.)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1266 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1267
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1268 I(name='BINSTRING',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1269 code='T',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1270 arg=string4,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1271 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1272 stack_after=[pystring],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1273 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1274 doc="""Push a Python string object.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1275
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1276 There are two arguments: the first is a 4-byte little-endian signed int
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1277 giving the number of bytes in the string, and the second is that many
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1278 bytes, which are taken literally as the string content. (Actually,
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1279 they are decoded into a str instance using the encoding given to the
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1280 Unpickler constructor. or the default, 'ASCII'.)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1281 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1282
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1283 I(name='SHORT_BINSTRING',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1284 code='U',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1285 arg=string1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1286 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1287 stack_after=[pystring],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1288 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1289 doc="""Push a Python string object.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1290
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1291 There are two arguments: the first is a 1-byte unsigned int giving
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1292 the number of bytes in the string, and the second is that many bytes,
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1293 which are taken literally as the string content. (Actually, they
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1294 are decoded into a str instance using the encoding given to the
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1295 Unpickler constructor. or the default, 'ASCII'.)
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1296 """),
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1297
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1298 # Bytes (protocol 3 only; older protocols don't support bytes at all)
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1299
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1300 I(name='BINBYTES',
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1301 code='B',
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1302 arg=bytes4,
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1303 stack_before=[],
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1304 stack_after=[pybytes],
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1305 proto=3,
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1306 doc="""Push a Python bytes object.
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1307
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1308 There are two arguments: the first is a 4-byte little-endian unsigned int
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1309 giving the number of bytes, and the second is that many bytes, which are
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1310 taken literally as the bytes content.
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1311 """),
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1312
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1313 I(name='SHORT_BINBYTES',
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1314 code='C',
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1315 arg=bytes1,
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1316 stack_before=[],
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1317 stack_after=[pybytes],
54215
6fab8bd15d71 Merge r72792: POP_MARK was not in pickle protocol 0, SHORT_BINBYTES was not in protocol 1.
Collin Winter <collinw@gmail.com>
parents: 51022
diff changeset
1318 proto=3,
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1319 doc="""Push a Python bytes object.
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1320
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
1321 There are two arguments: the first is a 1-byte unsigned int giving
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1322 the number of bytes, and the second is that many bytes, which are taken
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1323 literally as the string content.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1324 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1325
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1326 I(name='BINBYTES8',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1327 code='\x8e',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1328 arg=bytes8,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1329 stack_before=[],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1330 stack_after=[pybytes],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1331 proto=4,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1332 doc="""Push a Python bytes object.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1333
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1334 There are two arguments: the first is a 8-byte unsigned int giving
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1335 the number of bytes in the string, and the second is that many bytes,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1336 which are taken literally as the string content.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1337 """),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1338
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1339 # Ways to spell None.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1340
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1341 I(name='NONE',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1342 code='N',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1343 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1344 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1345 stack_after=[pynone],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1346 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1347 doc="Push None on the stack."),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1348
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1349 # Ways to spell bools, starting with proto 2. See INT for how this was
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1350 # done before proto 2.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1351
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1352 I(name='NEWTRUE',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1353 code='\x88',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1354 arg=None,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1355 stack_before=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1356 stack_after=[pybool],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1357 proto=2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1358 doc="""True.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1359
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1360 Push True onto the stack."""),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1361
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1362 I(name='NEWFALSE',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1363 code='\x89',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1364 arg=None,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1365 stack_before=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1366 stack_after=[pybool],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1367 proto=2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1368 doc="""True.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1369
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1370 Push False onto the stack."""),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1371
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1372 # Ways to spell Unicode strings.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1373
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1374 I(name='UNICODE',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1375 code='V',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1376 arg=unicodestringnl,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1377 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1378 stack_after=[pyunicode],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1379 proto=0, # this may be pure-text, but it's a later addition
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1380 doc="""Push a Python Unicode string object.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1381
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1382 The argument is a raw-unicode-escape encoding of a Unicode string,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1383 and so may contain embedded escape sequences. The argument extends
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1384 until the next newline character.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1385 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1386
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1387 I(name='SHORT_BINUNICODE',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1388 code='\x8c',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1389 arg=unicodestring1,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1390 stack_before=[],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1391 stack_after=[pyunicode],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1392 proto=4,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1393 doc="""Push a Python Unicode string object.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1394
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1395 There are two arguments: the first is a 1-byte little-endian signed int
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1396 giving the number of bytes in the string. The second is that many
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1397 bytes, and is the UTF-8 encoding of the Unicode string.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1398 """),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1399
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1400 I(name='BINUNICODE',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1401 code='X',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1402 arg=unicodestring4,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1403 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1404 stack_after=[pyunicode],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1405 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1406 doc="""Push a Python Unicode string object.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1407
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1408 There are two arguments: the first is a 4-byte little-endian unsigned int
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1409 giving the number of bytes in the string. The second is that many
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1410 bytes, and is the UTF-8 encoding of the Unicode string.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1411 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1412
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1413 I(name='BINUNICODE8',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1414 code='\x8d',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1415 arg=unicodestring8,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1416 stack_before=[],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1417 stack_after=[pyunicode],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1418 proto=4,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1419 doc="""Push a Python Unicode string object.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1420
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1421 There are two arguments: the first is a 8-byte little-endian signed int
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1422 giving the number of bytes in the string. The second is that many
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1423 bytes, and is the UTF-8 encoding of the Unicode string.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1424 """),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1425
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1426 # Ways to spell floats.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1427
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1428 I(name='FLOAT',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1429 code='F',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1430 arg=floatnl,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1431 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1432 stack_after=[pyfloat],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1433 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1434 doc="""Newline-terminated decimal float literal.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1435
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1436 The argument is repr(a_float), and in general requires 17 significant
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1437 digits for roundtrip conversion to be an identity (this is so for
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1438 IEEE-754 double precision values, which is what Python float maps to
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1439 on most boxes).
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1440
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1441 In general, FLOAT cannot be used to transport infinities, NaNs, or
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1442 minus zero across boxes (or even on a single box, if the platform C
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1443 library can't read the strings it produces for such things -- Windows
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1444 is like that), but may do less damage than BINFLOAT on boxes with
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1445 greater precision or dynamic range than IEEE-754 double.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1446 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1447
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1448 I(name='BINFLOAT',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1449 code='G',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1450 arg=float8,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1451 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1452 stack_after=[pyfloat],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1453 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1454 doc="""Float stored in binary form, with 8 bytes of data.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1455
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1456 This generally requires less than half the space of FLOAT encoding.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1457 In general, BINFLOAT cannot be used to transport infinities, NaNs, or
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1458 minus zero, raises an exception if the exponent exceeds the range of
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1459 an IEEE-754 double, and retains no more than 53 bits of precision (if
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1460 there are more than that, "add a half and chop" rounding is used to
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1461 cut it back to 53 significant bits).
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1462 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1463
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1464 # Ways to build lists.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1465
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1466 I(name='EMPTY_LIST',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1467 code=']',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1468 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1469 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1470 stack_after=[pylist],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1471 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1472 doc="Push an empty list."),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1473
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1474 I(name='APPEND',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1475 code='a',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1476 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1477 stack_before=[pylist, anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1478 stack_after=[pylist],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1479 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1480 doc="""Append an object to a list.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1481
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1482 Stack before: ... pylist anyobject
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1483 Stack after: ... pylist+[anyobject]
27463
2ee9742aadd8 Comments.
Tim Peters <tim.peters@gmail.com>
parents: 27462
diff changeset
1484
2ee9742aadd8 Comments.
Tim Peters <tim.peters@gmail.com>
parents: 27462
diff changeset
1485 although pylist is really extended in-place.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1486 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1487
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1488 I(name='APPENDS',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1489 code='e',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1490 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1491 stack_before=[pylist, markobject, stackslice],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1492 stack_after=[pylist],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1493 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1494 doc="""Extend a list by a slice of stack objects.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1495
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1496 Stack before: ... pylist markobject stackslice
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1497 Stack after: ... pylist+stackslice
27463
2ee9742aadd8 Comments.
Tim Peters <tim.peters@gmail.com>
parents: 27462
diff changeset
1498
2ee9742aadd8 Comments.
Tim Peters <tim.peters@gmail.com>
parents: 27462
diff changeset
1499 although pylist is really extended in-place.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1500 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1501
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1502 I(name='LIST',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1503 code='l',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1504 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1505 stack_before=[markobject, stackslice],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1506 stack_after=[pylist],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1507 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1508 doc="""Build a list out of the topmost stack slice, after markobject.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1509
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1510 All the stack entries following the topmost markobject are placed into
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1511 a single Python list, which single list object replaces all of the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1512 stack from the topmost markobject onward. For example,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1513
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1514 Stack before: ... markobject 1 2 3 'abc'
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1515 Stack after: ... [1, 2, 3, 'abc']
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1516 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1517
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1518 # Ways to build tuples.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1519
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1520 I(name='EMPTY_TUPLE',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1521 code=')',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1522 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1523 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1524 stack_after=[pytuple],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1525 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1526 doc="Push an empty tuple."),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1527
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1528 I(name='TUPLE',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1529 code='t',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1530 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1531 stack_before=[markobject, stackslice],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1532 stack_after=[pytuple],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1533 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1534 doc="""Build a tuple out of the topmost stack slice, after markobject.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1535
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1536 All the stack entries following the topmost markobject are placed into
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1537 a single Python tuple, which single tuple object replaces all of the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1538 stack from the topmost markobject onward. For example,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1539
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1540 Stack before: ... markobject 1 2 3 'abc'
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1541 Stack after: ... (1, 2, 3, 'abc')
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1542 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1543
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1544 I(name='TUPLE1',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1545 code='\x85',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1546 arg=None,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1547 stack_before=[anyobject],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1548 stack_after=[pytuple],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1549 proto=2,
63041
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1550 doc="""Build a one-tuple out of the topmost item on the stack.
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1551
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1552 This code pops one value off the stack and pushes a tuple of
63041
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1553 length 1 whose one item is that value back onto it. In other
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1554 words:
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1555
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1556 stack[-1] = tuple(stack[-1:])
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1557 """),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1558
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1559 I(name='TUPLE2',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1560 code='\x86',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1561 arg=None,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1562 stack_before=[anyobject, anyobject],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1563 stack_after=[pytuple],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1564 proto=2,
63041
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1565 doc="""Build a two-tuple out of the top two items on the stack.
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1566
63041
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1567 This code pops two values off the stack and pushes a tuple of
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1568 length 2 whose items are those values back onto it. In other
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1569 words:
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1570
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1571 stack[-2:] = [tuple(stack[-2:])]
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1572 """),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1573
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1574 I(name='TUPLE3',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1575 code='\x87',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1576 arg=None,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1577 stack_before=[anyobject, anyobject, anyobject],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1578 stack_after=[pytuple],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1579 proto=2,
63041
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1580 doc="""Build a three-tuple out of the top three items on the stack.
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1581
63041
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1582 This code pops three values off the stack and pushes a tuple of
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1583 length 3 whose items are those values back onto it. In other
24f7fa2581e6 Merged revisions 82915 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
1584 words:
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1585
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1586 stack[-3:] = [tuple(stack[-3:])]
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1587 """),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1588
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1589 # Ways to build dicts.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1590
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1591 I(name='EMPTY_DICT',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1592 code='}',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1593 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1594 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1595 stack_after=[pydict],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1596 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1597 doc="Push an empty dict."),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1598
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1599 I(name='DICT',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1600 code='d',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1601 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1602 stack_before=[markobject, stackslice],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1603 stack_after=[pydict],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1604 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1605 doc="""Build a dict out of the topmost stack slice, after markobject.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1606
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1607 All the stack entries following the topmost markobject are placed into
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1608 a single Python dict, which single dict object replaces all of the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1609 stack from the topmost markobject onward. The stack slice alternates
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1610 key, value, key, value, .... For example,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1611
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1612 Stack before: ... markobject 1 2 3 'abc'
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1613 Stack after: ... {1: 2, 3: 'abc'}
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1614 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1615
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1616 I(name='SETITEM',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1617 code='s',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1618 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1619 stack_before=[pydict, anyobject, anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1620 stack_after=[pydict],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1621 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1622 doc="""Add a key+value pair to an existing dict.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1623
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1624 Stack before: ... pydict key value
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1625 Stack after: ... pydict
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1626
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1627 where pydict has been modified via pydict[key] = value.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1628 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1629
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1630 I(name='SETITEMS',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1631 code='u',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1632 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1633 stack_before=[pydict, markobject, stackslice],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1634 stack_after=[pydict],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1635 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1636 doc="""Add an arbitrary number of key+value pairs to an existing dict.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1637
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1638 The slice of the stack following the topmost markobject is taken as
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1639 an alternating sequence of keys and values, added to the dict
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1640 immediately under the topmost markobject. Everything at and after the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1641 topmost markobject is popped, leaving the mutated dict at the top
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1642 of the stack.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1643
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1644 Stack before: ... pydict markobject key_1 value_1 ... key_n value_n
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1645 Stack after: ... pydict
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1646
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1647 where pydict has been modified via pydict[key_i] = value_i for i in
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1648 1, 2, ..., n, and in that order.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1649 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1650
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1651 # Ways to build sets
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1652
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1653 I(name='EMPTY_SET',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1654 code='\x8f',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1655 arg=None,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1656 stack_before=[],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1657 stack_after=[pyset],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1658 proto=4,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1659 doc="Push an empty set."),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1660
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1661 I(name='ADDITEMS',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1662 code='\x90',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1663 arg=None,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1664 stack_before=[pyset, markobject, stackslice],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1665 stack_after=[pyset],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1666 proto=4,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1667 doc="""Add an arbitrary number of items to an existing set.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1668
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1669 The slice of the stack following the topmost markobject is taken as
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1670 a sequence of items, added to the set immediately under the topmost
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1671 markobject. Everything at and after the topmost markobject is popped,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1672 leaving the mutated set at the top of the stack.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1673
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1674 Stack before: ... pyset markobject item_1 ... item_n
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1675 Stack after: ... pyset
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1676
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1677 where pyset has been modified via pyset.add(item_i) = item_i for i in
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1678 1, 2, ..., n, and in that order.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1679 """),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1680
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1681 # Way to build frozensets
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1682
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1683 I(name='FROZENSET',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1684 code='\x91',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1685 arg=None,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1686 stack_before=[markobject, stackslice],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1687 stack_after=[pyfrozenset],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1688 proto=4,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1689 doc="""Build a frozenset out of the topmost slice, after markobject.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1690
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1691 All the stack entries following the topmost markobject are placed into
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1692 a single Python frozenset, which single frozenset object replaces all
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1693 of the stack from the topmost markobject onward. For example,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1694
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1695 Stack before: ... markobject 1 2 3
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1696 Stack after: ... frozenset({1, 2, 3})
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1697 """),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1698
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1699 # Stack manipulation.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1700
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1701 I(name='POP',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1702 code='0',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1703 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1704 stack_before=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1705 stack_after=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1706 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1707 doc="Discard the top stack item, shrinking the stack by one item."),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1708
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1709 I(name='DUP',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1710 code='2',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1711 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1712 stack_before=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1713 stack_after=[anyobject, anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1714 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1715 doc="Push the top stack item onto the stack again, duplicating it."),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1716
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1717 I(name='MARK',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1718 code='(',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1719 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1720 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1721 stack_after=[markobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1722 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1723 doc="""Push markobject onto the stack.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1724
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1725 markobject is a unique object, used by other opcodes to identify a
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1726 region of the stack containing a variable number of objects for them
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1727 to work on. See markobject.doc for more detail.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1728 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1729
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1730 I(name='POP_MARK',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1731 code='1',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1732 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1733 stack_before=[markobject, stackslice],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1734 stack_after=[],
54215
6fab8bd15d71 Merge r72792: POP_MARK was not in pickle protocol 0, SHORT_BINBYTES was not in protocol 1.
Collin Winter <collinw@gmail.com>
parents: 51022
diff changeset
1735 proto=1,
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1736 doc="""Pop all the stack objects at and above the topmost markobject.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1737
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1738 When an opcode using a variable number of stack objects is done,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1739 POP_MARK is used to remove those objects, and to remove the markobject
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1740 that delimited their starting position on the stack.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1741 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1742
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1743 # Memo manipulation. There are really only two operations (get and put),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1744 # each in all-text, "short binary", and "long binary" flavors.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1745
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1746 I(name='GET',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1747 code='g',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1748 arg=decimalnl_short,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1749 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1750 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1751 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1752 doc="""Read an object from the memo and push it on the stack.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1753
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64010
diff changeset
1754 The index of the memo object to push is given by the newline-terminated
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1755 decimal string following. BINGET and LONG_BINGET are space-optimized
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1756 versions.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1757 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1758
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1759 I(name='BINGET',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1760 code='h',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1761 arg=uint1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1762 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1763 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1764 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1765 doc="""Read an object from the memo and push it on the stack.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1766
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1767 The index of the memo object to push is given by the 1-byte unsigned
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1768 integer following.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1769 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1770
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1771 I(name='LONG_BINGET',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1772 code='j',
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1773 arg=uint4,
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1774 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1775 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1776 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1777 doc="""Read an object from the memo and push it on the stack.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1778
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1779 The index of the memo object to push is given by the 4-byte unsigned
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1780 little-endian integer following.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1781 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1782
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1783 I(name='PUT',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1784 code='p',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1785 arg=decimalnl_short,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1786 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1787 stack_after=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1788 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1789 doc="""Store the stack top into the memo. The stack is not popped.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1790
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1791 The index of the memo location to write into is given by the newline-
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1792 terminated decimal string following. BINPUT and LONG_BINPUT are
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1793 space-optimized versions.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1794 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1795
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1796 I(name='BINPUT',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1797 code='q',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1798 arg=uint1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1799 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1800 stack_after=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1801 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1802 doc="""Store the stack top into the memo. The stack is not popped.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1803
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1804 The index of the memo location to write into is given by the 1-byte
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1805 unsigned integer following.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1806 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1807
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1808 I(name='LONG_BINPUT',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1809 code='r',
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1810 arg=uint4,
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1811 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1812 stack_after=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1813 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1814 doc="""Store the stack top into the memo. The stack is not popped.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1815
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1816 The index of the memo location to write into is given by the 4-byte
83360
4ced30417300 Issue #16550: Update the opcode descriptions of pickletools to use unsigned
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 73958
diff changeset
1817 unsigned little-endian integer following.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1818 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1819
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1820 I(name='MEMOIZE',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1821 code='\x94',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1822 arg=None,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1823 stack_before=[anyobject],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1824 stack_after=[anyobject],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1825 proto=4,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1826 doc="""Store the stack top into the memo. The stack is not popped.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1827
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1828 The index of the memo location to write is the number of
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1829 elements currently present in the memo.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1830 """),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1831
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1832 # Access the extension registry (predefined objects). Akin to the GET
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1833 # family.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1834
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1835 I(name='EXT1',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1836 code='\x82',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1837 arg=uint1,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1838 stack_before=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1839 stack_after=[anyobject],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1840 proto=2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1841 doc="""Extension code.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1842
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1843 This code and the similar EXT2 and EXT4 allow using a registry
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1844 of popular objects that are pickled by name, typically classes.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1845 It is envisioned that through a global negotiation and
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1846 registration process, third parties can set up a mapping between
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1847 ints and object names.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1848
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1849 In order to guarantee pickle interchangeability, the extension
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1850 code registry ought to be global, although a range of codes may
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1851 be reserved for private use.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1852
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1853 EXT1 has a 1-byte integer argument. This is used to index into the
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1854 extension registry, and the object at that index is pushed on the stack.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1855 """),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1856
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1857 I(name='EXT2',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1858 code='\x83',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1859 arg=uint2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1860 stack_before=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1861 stack_after=[anyobject],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1862 proto=2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1863 doc="""Extension code.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1864
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1865 See EXT1. EXT2 has a two-byte integer argument.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1866 """),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1867
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1868 I(name='EXT4',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1869 code='\x84',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1870 arg=int4,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1871 stack_before=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1872 stack_after=[anyobject],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1873 proto=2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1874 doc="""Extension code.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1875
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1876 See EXT1. EXT4 has a four-byte integer argument.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1877 """),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
1878
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1879 # Push a class object, or module function, on the stack, via its module
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1880 # and name.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1881
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1882 I(name='GLOBAL',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1883 code='c',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1884 arg=stringnl_noescape_pair,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1885 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1886 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1887 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1888 doc="""Push a global object (module.attr) on the stack.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1889
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1890 Two newline-terminated strings follow the GLOBAL opcode. The first is
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1891 taken as a module name, and the second as a class name. The class
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1892 object module.class is pushed on the stack. More accurately, the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1893 object returned by self.find_class(module, class) is pushed on the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1894 stack, so unpickling subclasses can override this form of lookup.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1895 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1896
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1897 I(name='STACK_GLOBAL',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1898 code='\x93',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1899 arg=None,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1900 stack_before=[pyunicode, pyunicode],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1901 stack_after=[anyobject],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1902 proto=0,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1903 doc="""Push a global object (module.attr) on the stack.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1904 """),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1905
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1906 # Ways to build objects of classes pickle doesn't know about directly
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1907 # (user-defined classes). I despair of documenting this accurately
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1908 # and comprehensibly -- you really have to read the pickle code to
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1909 # find all the special cases.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1910
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1911 I(name='REDUCE',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1912 code='R',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1913 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1914 stack_before=[anyobject, anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1915 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1916 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1917 doc="""Push an object built from a callable and an argument tuple.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1918
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1919 The opcode is named to remind of the __reduce__() method.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1920
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1921 Stack before: ... callable pytuple
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1922 Stack after: ... callable(*pytuple)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1923
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1924 The callable and the argument tuple are the first two items returned
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1925 by a __reduce__ method. Applying the callable to the argtuple is
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1926 supposed to reproduce the original object, or at least get it started.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1927 If the __reduce__ method returns a 3-tuple, the last component is an
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1928 argument to be passed to the object's __setstate__, and then the REDUCE
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1929 opcode is followed by code to create setstate's argument, and then a
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1930 BUILD opcode to apply __setstate__ to that argument.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1931
41986
6464cc638faa Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41715
diff changeset
1932 If not isinstance(callable, type), REDUCE complains unless the
46867
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 45812
diff changeset
1933 callable has been registered with the copyreg module's
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1934 safe_constructors dict, or the callable has a magic
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1935 '__safe_for_unpickling__' attribute with a true value. I'm not sure
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1936 why it does this, but I've sure seen this complaint often enough when
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1937 I didn't want to <wink>.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1938 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1939
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1940 I(name='BUILD',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1941 code='b',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1942 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1943 stack_before=[anyobject, anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1944 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1945 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1946 doc="""Finish building an object, via __setstate__ or dict update.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1947
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1948 Stack before: ... anyobject argument
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1949 Stack after: ... anyobject
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1950
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1951 where anyobject may have been mutated, as follows:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1952
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1953 If the object has a __setstate__ method,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1954
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1955 anyobject.__setstate__(argument)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1956
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1957 is called.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1958
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1959 Else the argument must be a dict, the object must have a __dict__, and
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1960 the object is updated via
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1961
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1962 anyobject.__dict__.update(argument)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1963 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1964
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1965 I(name='INST',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1966 code='i',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1967 arg=stringnl_noescape_pair,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1968 stack_before=[markobject, stackslice],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1969 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1970 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1971 doc="""Build a class instance.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1972
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1973 This is the protocol 0 version of protocol 1's OBJ opcode.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1974 INST is followed by two newline-terminated strings, giving a
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1975 module and class name, just as for the GLOBAL opcode (and see
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1976 GLOBAL for more details about that). self.find_class(module, name)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1977 is used to get a class object.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1978
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1979 In addition, all the objects on the stack following the topmost
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1980 markobject are gathered into a tuple and popped (along with the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1981 topmost markobject), just as for the TUPLE opcode.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1982
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1983 Now it gets complicated. If all of these are true:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1984
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1985 + The argtuple is empty (markobject was at the top of the stack
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1986 at the start).
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1987
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1988 + The class object does not have a __getinitargs__ attribute.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1989
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1990 then we want to create an old-style class instance without invoking
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1991 its __init__() method (pickle has waffled on this over the years; not
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1992 calling __init__() is current wisdom). In this case, an instance of
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1993 an old-style dummy class is created, and then we try to rebind its
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1994 __class__ attribute to the desired class object. If this succeeds,
41715
f60c877d52c8 Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41651
diff changeset
1995 the new instance object is pushed on the stack, and we're done.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1996
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1997 Else (the argtuple is not empty, it's not an old-style class object,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1998 or the class object does have a __getinitargs__ attribute), the code
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1999 first insists that the class object have a __safe_for_unpickling__
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2000 attribute. Unlike as for the __safe_for_unpickling__ check in REDUCE,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2001 it doesn't matter whether this attribute has a true or false value, it
42322
3e33215c66ed Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents: 42295
diff changeset
2002 only matters whether it exists (XXX this is a bug). If
3e33215c66ed Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents: 42295
diff changeset
2003 __safe_for_unpickling__ doesn't exist, UnpicklingError is raised.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2004
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2005 Else (the class object does have a __safe_for_unpickling__ attr),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2006 the class object obtained from INST's arguments is applied to the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2007 argtuple obtained from the stack, and the resulting instance object
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2008 is pushed on the stack.
27535
72b38d5fe61f Updated the INST/OBJ docs, to say they really are (almost) identical
Tim Peters <tim.peters@gmail.com>
parents: 27532
diff changeset
2009
72b38d5fe61f Updated the INST/OBJ docs, to say they really are (almost) identical
Tim Peters <tim.peters@gmail.com>
parents: 27532
diff changeset
2010 NOTE: checks for __safe_for_unpickling__ went away in Python 2.3.
73949
021e5bb297d1 Issue #13575: there is only one class type.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73076
diff changeset
2011 NOTE: the distinction between old-style and new-style classes does
021e5bb297d1 Issue #13575: there is only one class type.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73076
diff changeset
2012 not make sense in Python 3.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2013 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2014
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2015 I(name='OBJ',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2016 code='o',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2017 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2018 stack_before=[markobject, anyobject, stackslice],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2019 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2020 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2021 doc="""Build a class instance.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2022
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2023 This is the protocol 1 version of protocol 0's INST opcode, and is
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2024 very much like it. The major difference is that the class object
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2025 is taken off the stack, allowing it to be retrieved from the memo
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2026 repeatedly if several instances of the same class are created. This
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2027 can be much more efficient (in both time and space) than repeatedly
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2028 embedding the module and class names in INST opcodes.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2029
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2030 Unlike INST, OBJ takes no arguments from the opcode stream. Instead
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2031 the class object is taken off the stack, immediately above the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2032 topmost markobject:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2033
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2034 Stack before: ... markobject classobject stackslice
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2035 Stack after: ... new_instance_object
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2036
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2037 As for INST, the remainder of the stack above the markobject is
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2038 gathered into an argument tuple, and then the logic seems identical,
27513
b1d1fe7fb514 Document the demise of all pretenses of safety, and the difference
Guido van Rossum <guido@python.org>
parents: 27507
diff changeset
2039 except that no __safe_for_unpickling__ check is done (XXX this is
42322
3e33215c66ed Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents: 42295
diff changeset
2040 a bug). See INST for the gory details.
27535
72b38d5fe61f Updated the INST/OBJ docs, to say they really are (almost) identical
Tim Peters <tim.peters@gmail.com>
parents: 27532
diff changeset
2041
72b38d5fe61f Updated the INST/OBJ docs, to say they really are (almost) identical
Tim Peters <tim.peters@gmail.com>
parents: 27532
diff changeset
2042 NOTE: In Python 2.3, INST and OBJ are identical except for how they
72b38d5fe61f Updated the INST/OBJ docs, to say they really are (almost) identical
Tim Peters <tim.peters@gmail.com>
parents: 27532
diff changeset
2043 get the class object. That was always the intent; the implementations
72b38d5fe61f Updated the INST/OBJ docs, to say they really are (almost) identical
Tim Peters <tim.peters@gmail.com>
parents: 27532
diff changeset
2044 had diverged for accidental reasons.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2045 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2046
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2047 I(name='NEWOBJ',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2048 code='\x81',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2049 arg=None,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2050 stack_before=[anyobject, anyobject],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2051 stack_after=[anyobject],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2052 proto=2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2053 doc="""Build an object instance.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2054
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2055 The stack before should be thought of as containing a class
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2056 object followed by an argument tuple (the tuple being the stack
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2057 top). Call these cls and args. They are popped off the stack,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2058 and the value returned by cls.__new__(cls, *args) is pushed back
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2059 onto the stack.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2060 """),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2061
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2062 I(name='NEWOBJ_EX',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2063 code='\x92',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2064 arg=None,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2065 stack_before=[anyobject, anyobject, anyobject],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2066 stack_after=[anyobject],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2067 proto=4,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2068 doc="""Build an object instance.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2069
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2070 The stack before should be thought of as containing a class
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2071 object followed by an argument tuple and by a keyword argument dict
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2072 (the dict being the stack top). Call these cls and args. They are
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2073 popped off the stack, and the value returned by
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2074 cls.__new__(cls, *args, *kwargs) is pushed back onto the stack.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2075 """),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2076
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2077 # Machine control.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2078
27462
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2079 I(name='PROTO',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2080 code='\x80',
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2081 arg=uint1,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2082 stack_before=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2083 stack_after=[],
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2084 proto=2,
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2085 doc="""Protocol version indicator.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2086
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2087 For protocol 2 and above, a pickle must start with this opcode.
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2088 The argument is the protocol version, an int in range(2, 256).
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2089 """),
26a792007665 Now that proto2 is defined, replaced XXX blocks with text about it.
Tim Peters <tim.peters@gmail.com>
parents: 27436
diff changeset
2090
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2091 I(name='STOP',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2092 code='.',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2093 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2094 stack_before=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2095 stack_after=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2096 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2097 doc="""Stop the unpickling machine.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2098
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2099 Every pickle ends with this opcode. The object at the top of the stack
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2100 is popped, and that's the result of unpickling. The stack should be
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2101 empty then.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2102 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2103
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2104 # Framing support.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2105
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2106 I(name='FRAME',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2107 code='\x95',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2108 arg=uint8,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2109 stack_before=[],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2110 stack_after=[],
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2111 proto=4,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2112 doc="""Indicate the beginning of a new frame.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2113
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2114 The unpickler may use this opcode to safely prefetch data from its
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2115 underlying stream.
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2116 """),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2117
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2118 # Ways to deal with persistent IDs.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2119
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2120 I(name='PERSID',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2121 code='P',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2122 arg=stringnl_noescape,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2123 stack_before=[],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2124 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2125 proto=0,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2126 doc="""Push an object identified by a persistent ID.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2127
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2128 The pickle module doesn't define what a persistent ID means. PERSID's
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2129 argument is a newline-terminated str-style (no embedded escapes, no
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2130 bracketing quote characters) string, which *is* "the persistent ID".
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2131 The unpickler passes this string to self.persistent_load(). Whatever
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2132 object that returns is pushed on the stack. There is no implementation
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2133 of persistent_load() in Python's unpickler: it must be supplied by an
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2134 unpickler subclass.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2135 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2136
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2137 I(name='BINPERSID',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2138 code='Q',
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2139 arg=None,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2140 stack_before=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2141 stack_after=[anyobject],
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2142 proto=1,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2143 doc="""Push an object identified by a persistent ID.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2144
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2145 Like PERSID, except the persistent ID is popped off the stack (instead
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2146 of being a string embedded in the opcode bytestream). The persistent
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2147 ID is passed to self.persistent_load(), and whatever object that
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2148 returns is pushed on the stack. See PERSID for more detail.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2149 """),
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2150 ]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2151 del I
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2152
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2153 # Verify uniqueness of .name and .code members.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2154 name2i = {}
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2155 code2i = {}
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2156
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2157 for i, d in enumerate(opcodes):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2158 if d.name in name2i:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2159 raise ValueError("repeated name %r at indices %d and %d" %
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2160 (d.name, name2i[d.name], i))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2161 if d.code in code2i:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2162 raise ValueError("repeated code %r at indices %d and %d" %
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2163 (d.code, code2i[d.code], i))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2164
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2165 name2i[d.name] = i
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2166 code2i[d.code] = i
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2167
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2168 del name2i, code2i, i, d
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2169
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2170 ##############################################################################
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2171 # Build a code2op dict, mapping opcode characters to OpcodeInfo records.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2172 # Also ensure we've got the same stuff as pickle.py, although the
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2173 # introspection here is dicey.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2174
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2175 code2op = {}
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2176 for d in opcodes:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2177 code2op[d.code] = d
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2178 del d
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2179
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2180 def assure_pickle_consistency(verbose=False):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2181
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2182 copy = code2op.copy()
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2183 for name in pickle.__all__:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2184 if not re.match("[A-Z][A-Z0-9_]+$", name):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2185 if verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
2186 print("skipping %r: it doesn't look like an opcode name" % name)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2187 continue
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2188 picklecode = getattr(pickle, name)
41643
c6568550b1c6 Checkpoint. A b it closer to working pickles and pickletools.
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
2189 if not isinstance(picklecode, bytes) or len(picklecode) != 1:
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2190 if verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
2191 print(("skipping %r: value %r doesn't look like a pickle "
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
2192 "code" % (name, picklecode)))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2193 continue
41643
c6568550b1c6 Checkpoint. A b it closer to working pickles and pickletools.
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
2194 picklecode = picklecode.decode("latin-1")
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2195 if picklecode in copy:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2196 if verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
2197 print("checking name %r w/ code %r for consistency" % (
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
2198 name, picklecode))
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2199 d = copy[picklecode]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2200 if d.name != name:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2201 raise ValueError("for pickle code %r, pickle.py uses name %r "
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2202 "but we're using name %r" % (picklecode,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2203 name,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2204 d.name))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2205 # Forget this one. Any left over in copy at the end are a problem
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2206 # of a different kind.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2207 del copy[picklecode]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2208 else:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2209 raise ValueError("pickle.py appears to have a pickle opcode with "
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2210 "name %r and code %r, but we don't" %
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2211 (name, picklecode))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2212 if copy:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2213 msg = ["we appear to have pickle opcodes that pickle.py doesn't have:"]
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2214 for code, d in copy.items():
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2215 msg.append(" name %r with code %r" % (d.name, code))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2216 raise ValueError("\n".join(msg))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2217
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2218 assure_pickle_consistency()
27506
b480a4f707f6 pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents: 27473
diff changeset
2219 del assure_pickle_consistency
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2220
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2221 ##############################################################################
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2222 # A pickle opcode generator.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2223
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2224 def _genops(data, yield_end_pos=False):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2225 if isinstance(data, bytes_types):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2226 data = io.BytesIO(data)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2227
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2228 if hasattr(data, "tell"):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2229 getpos = data.tell
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2230 else:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2231 getpos = lambda: None
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2232
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2233 while True:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2234 pos = getpos()
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2235 code = data.read(1)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2236 opcode = code2op.get(code.decode("latin-1"))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2237 if opcode is None:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2238 if code == b"":
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2239 raise ValueError("pickle exhausted before seeing STOP")
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2240 else:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2241 raise ValueError("at position %s, opcode %r unknown" % (
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2242 "<unknown>" if pos is None else pos,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2243 code))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2244 if opcode.arg is None:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2245 arg = None
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2246 else:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2247 arg = opcode.arg.reader(data)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2248 if yield_end_pos:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2249 yield opcode, arg, pos, getpos()
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2250 else:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2251 yield opcode, arg, pos
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2252 if code == b'.':
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2253 assert opcode.name == 'STOP'
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2254 break
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2255
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2256 def genops(pickle):
27424
93ba42cf4e53 Remove a stray quote.
Guido van Rossum <guido@python.org>
parents: 27423
diff changeset
2257 """Generate all the opcodes in a pickle.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2258
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2259 'pickle' is a file-like object, or string, containing the pickle.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2260
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2261 Each opcode in the pickle is generated, from the current pickle position,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2262 stopping after a STOP opcode is delivered. A triple is generated for
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2263 each opcode:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2264
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2265 opcode, arg, pos
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2266
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2267 opcode is an OpcodeInfo record, describing the current opcode.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2268
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2269 If the opcode has an argument embedded in the pickle, arg is its decoded
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2270 value, as a Python object. If the opcode doesn't have an argument, arg
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2271 is None.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2272
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2273 If the pickle has a tell() method, pos was the value of pickle.tell()
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42322
diff changeset
2274 before reading the current opcode. If the pickle is a bytes object,
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42322
diff changeset
2275 it's wrapped in a BytesIO object, and the latter's tell() result is
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2276 used. Else (the pickle doesn't have a tell(), and it's not obvious how
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2277 to query its current position) pos is None.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2278 """
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2279 return _genops(pickle)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2280
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2281 ##############################################################################
45278
e67050d62adf Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60706,60708-60712,60714-60724 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
2282 # A pickle optimizer.
e67050d62adf Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60706,60708-60712,60714-60724 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
2283
e67050d62adf Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60706,60708-60712,60714-60724 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
2284 def optimize(p):
e67050d62adf Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60706,60708-60712,60714-60724 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
2285 'Optimize a pickle string by removing unused PUT opcodes'
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2286 not_a_put = object()
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2287 gets = { not_a_put } # set of args used by a GET opcode
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2288 opcodes = [] # (startpos, stoppos, putid)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2289 proto = 0
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2290 for opcode, arg, pos, end_pos in _genops(p, yield_end_pos=True):
45278
e67050d62adf Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60706,60708-60712,60714-60724 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
2291 if 'PUT' in opcode.name:
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2292 opcodes.append((pos, end_pos, arg))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2293 elif 'FRAME' in opcode.name:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2294 pass
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2295 else:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2296 if 'GET' in opcode.name:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2297 gets.add(arg)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2298 elif opcode.name == 'PROTO':
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2299 assert pos == 0, pos
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2300 proto = arg
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2301 opcodes.append((pos, end_pos, not_a_put))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2302 prevpos, prevarg = pos, None
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2303
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2304 # Copy the opcodes except for PUTS without a corresponding GET
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2305 out = io.BytesIO()
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2306 opcodes = iter(opcodes)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2307 if proto >= 2:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2308 # Write the PROTO header before any framing
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2309 start, stop, _ = next(opcodes)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2310 out.write(p[start:stop])
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2311 buf = pickle._Framer(out.write)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2312 if proto >= 4:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2313 buf.start_framing()
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2314 for start, stop, putid in opcodes:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2315 if putid in gets:
87685
9feada79a411 Issue #19754: Fix typo.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87684
diff changeset
2316 buf.commit_frame()
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2317 buf.write(p[start:stop])
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2318 if proto >= 4:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2319 buf.end_framing()
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2320 return out.getvalue()
45278
e67050d62adf Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60706,60708-60712,60714-60724 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
2321
e67050d62adf Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60706,60708-60712,60714-60724 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
2322 ##############################################################################
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2323 # A symbolic pickle disassembler.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2324
63055
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2325 def dis(pickle, out=None, memo=None, indentlevel=4, annotate=0):
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2326 """Produce a symbolic disassembly of a pickle.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2327
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2328 'pickle' is a file-like object, or string, containing a (at least one)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2329 pickle. The pickle is disassembled from the current position, through
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2330 the first STOP opcode encountered.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2331
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2332 Optional arg 'out' is a file-like object to which the disassembly is
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2333 printed. It defaults to sys.stdout.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2334
27703
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2335 Optional arg 'memo' is a Python dict, used as the pickle's memo. It
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2336 may be mutated by dis(), if the pickle contains PUT or BINPUT opcodes.
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2337 Passing the same memo object to another dis() call then allows disassembly
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2338 to proceed across multiple pickles that were all created by the same
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2339 pickler with the same memo. Ordinarily you don't need to worry about this.
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2340
63055
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2341 Optional arg 'indentlevel' is the number of blanks by which to indent
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2342 a new MARK level. It defaults to 4.
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2343
63055
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2344 Optional arg 'annotate' if nonzero instructs dis() to add short
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2345 description of the opcode on each line of disassembled output.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2346 The value given to 'annotate' must be an integer and is used as a
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2347 hint for the column where annotation should start. The default
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2348 value is 0, meaning no annotations.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2349
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2350 In addition to printing the disassembly, some sanity checks are made:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2351
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2352 + All embedded opcode arguments "make sense".
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2353
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2354 + Explicit and implicit pop operations have enough items on the stack.
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2355
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2356 + When an opcode implicitly refers to a markobject, a markobject is
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2357 actually on the stack.
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2358
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2359 + A memo entry isn't referenced before it's defined.
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2360
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2361 + The markobject isn't stored in the memo.
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2362
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2363 + A memo entry isn't redefined.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2364 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2365
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2366 # Most of the hair here is for sanity checks, but most of it is needed
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2367 # anyway to detect when a protocol 0 POP takes a MARK off the stack
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2368 # (which in turn is needed to indent MARK blocks correctly).
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2369
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2370 stack = [] # crude emulation of unpickler stack
27703
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2371 if memo is None:
85221
e07f104133d5 #18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83360
diff changeset
2372 memo = {} # crude emulation of unpickler memo
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2373 maxproto = -1 # max protocol number seen
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2374 markstack = [] # bytecode positions of MARK opcodes
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2375 indentchunk = ' ' * indentlevel
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2376 errormsg = None
85221
e07f104133d5 #18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83360
diff changeset
2377 annocol = annotate # column hint for annotations
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2378 for opcode, arg, pos in genops(pickle):
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2379 if pos is not None:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
2380 print("%5d:" % pos, end=' ', file=out)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2381
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2382 line = "%-4s %s%s" % (repr(opcode.code)[1:-1],
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2383 indentchunk * len(markstack),
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2384 opcode.name)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2385
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2386 maxproto = max(maxproto, opcode.proto)
27532
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2387 before = opcode.stack_before # don't mutate
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2388 after = opcode.stack_after # don't mutate
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2389 numtopop = len(before)
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2390
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2391 # See whether a MARK should be popped.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2392 markmsg = None
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2393 if markobject in before or (opcode.name == "POP" and
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2394 stack and
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2395 stack[-1] is markobject):
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2396 assert markobject not in after
27532
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2397 if __debug__:
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2398 if markobject in before:
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2399 assert before[-1] is stackslice
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2400 if markstack:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2401 markpos = markstack.pop()
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2402 if markpos is None:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2403 markmsg = "(MARK at unknown opcode offset)"
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2404 else:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2405 markmsg = "(MARK at %d)" % markpos
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2406 # Pop everything at and after the topmost markobject.
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2407 while stack[-1] is not markobject:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2408 stack.pop()
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2409 stack.pop()
27532
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2410 # Stop later code from popping too much.
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2411 try:
27532
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2412 numtopop = before.index(markobject)
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2413 except ValueError:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2414 assert opcode.name == "POP"
27532
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2415 numtopop = 0
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2416 else:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2417 errormsg = markmsg = "no MARK exists on stack"
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2418
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2419 # Check for correct memo usage.
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2420 if opcode.name in ("PUT", "BINPUT", "LONG_BINPUT", "MEMOIZE"):
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2421 if opcode.name == "MEMOIZE":
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2422 memo_idx = len(memo)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2423 else:
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2424 assert arg is not None
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2425 memo_idx = arg
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2426 if memo_idx in memo:
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2427 errormsg = "memo key %r already defined" % arg
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2428 elif not stack:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2429 errormsg = "stack is empty -- can't store into memo"
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2430 elif stack[-1] is markobject:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2431 errormsg = "can't store markobject in the memo"
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2432 else:
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
2433 memo[memo_idx] = stack[-1]
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2434 elif opcode.name in ("GET", "BINGET", "LONG_BINGET"):
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2435 if arg in memo:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2436 assert len(after) == 1
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2437 after = [memo[arg]] # for better stack emulation
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2438 else:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2439 errormsg = "memo key %r has never been stored into" % arg
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2440
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2441 if arg is not None or markmsg:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2442 # make a mild effort to align arguments
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2443 line += ' ' * (10 - len(opcode.name))
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2444 if arg is not None:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2445 line += ' ' + repr(arg)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2446 if markmsg:
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2447 line += ' ' + markmsg
63055
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2448 if annotate:
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2449 line += ' ' * (annocol - len(line))
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2450 # make a mild effort to align annotations
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2451 annocol = len(line)
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2452 if annocol > 50:
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2453 annocol = annotate
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2454 line += ' ' + opcode.doc.split('\n', 1)[0]
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
2455 print(line, file=out)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2456
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2457 if errormsg:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2458 # Note that we delayed complaining until the offending opcode
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2459 # was printed.
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2460 raise ValueError(errormsg)
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2461
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2462 # Emulate the stack effects.
27532
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2463 if len(stack) < numtopop:
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2464 raise ValueError("tries to pop %d items from stack with "
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2465 "only %d items" % (numtopop, len(stack)))
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2466 if numtopop:
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2467 del stack[-numtopop:]
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2468 if markobject in after:
27532
02f138c639a0 dis(): Simplified stack emulation a bit.
Tim Peters <tim.peters@gmail.com>
parents: 27522
diff changeset
2469 assert markobject not in before
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2470 markstack.append(pos)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2471
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2472 stack.extend(after)
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2473
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
2474 print("highest protocol among opcodes =", maxproto, file=out)
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2475 if stack:
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2476 raise ValueError("stack not empty after STOP: %r" % stack)
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2477
34590
ff3def77b129 An instance of class PicklingError was used here simply as an example of
Tim Peters <tim.peters@gmail.com>
parents: 34128
diff changeset
2478 # For use in the doctest, simply as an example of a class to pickle.
ff3def77b129 An instance of class PicklingError was used here simply as an example of
Tim Peters <tim.peters@gmail.com>
parents: 34128
diff changeset
2479 class _Example:
ff3def77b129 An instance of class PicklingError was used here simply as an example of
Tim Peters <tim.peters@gmail.com>
parents: 34128
diff changeset
2480 def __init__(self, value):
ff3def77b129 An instance of class PicklingError was used here simply as an example of
Tim Peters <tim.peters@gmail.com>
parents: 34128
diff changeset
2481 self.value = value
ff3def77b129 An instance of class PicklingError was used here simply as an example of
Tim Peters <tim.peters@gmail.com>
parents: 34128
diff changeset
2482
27472
de4061584c83 _dis_test should be a raw string now that it contains \x escapes.
Guido van Rossum <guido@python.org>
parents: 27471
diff changeset
2483 _dis_test = r"""
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2484 >>> import pickle
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
2485 >>> x = [1, 2, (3, 4), {b'abc': "def"}]
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
2486 >>> pkl0 = pickle.dumps(x, 0)
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
2487 >>> dis(pkl0)
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2488 0: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2489 1: l LIST (MARK at 0)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2490 2: p PUT 0
40640
6f5b33947274 Fix pickletools doctests -- all we get nowadays is longs.
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
2491 5: L LONG 1
50946
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2492 9: a APPEND
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2493 10: L LONG 2
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2494 14: a APPEND
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2495 15: ( MARK
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2496 16: L LONG 3
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2497 20: L LONG 4
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2498 24: t TUPLE (MARK at 15)
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2499 25: p PUT 1
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2500 28: a APPEND
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2501 29: ( MARK
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2502 30: d DICT (MARK at 29)
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2503 31: p PUT 2
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2504 34: c GLOBAL '_codecs encode'
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2505 50: p PUT 3
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2506 53: ( MARK
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2507 54: V UNICODE 'abc'
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2508 59: p PUT 4
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2509 62: V UNICODE 'latin1'
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2510 70: p PUT 5
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2511 73: t TUPLE (MARK at 53)
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2512 74: p PUT 6
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2513 77: R REDUCE
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2514 78: p PUT 7
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2515 81: V UNICODE 'def'
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2516 86: p PUT 8
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2517 89: s SETITEM
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2518 90: a APPEND
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2519 91: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2520 highest protocol among opcodes = 0
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2521
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2522 Try again with a "binary" pickle.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2523
45812
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
2524 >>> pkl1 = pickle.dumps(x, 1)
322693bf10ca - A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents: 45288
diff changeset
2525 >>> dis(pkl1)
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2526 0: ] EMPTY_LIST
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2527 1: q BINPUT 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2528 3: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2529 4: K BININT1 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2530 6: K BININT1 2
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2531 8: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2532 9: K BININT1 3
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2533 11: K BININT1 4
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2534 13: t TUPLE (MARK at 8)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2535 14: q BINPUT 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2536 16: } EMPTY_DICT
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2537 17: q BINPUT 2
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2538 19: c GLOBAL '_codecs encode'
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2539 35: q BINPUT 3
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2540 37: ( MARK
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2541 38: X BINUNICODE 'abc'
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2542 46: q BINPUT 4
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2543 48: X BINUNICODE 'latin1'
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2544 59: q BINPUT 5
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2545 61: t TUPLE (MARK at 37)
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2546 62: q BINPUT 6
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2547 64: R REDUCE
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2548 65: q BINPUT 7
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2549 67: X BINUNICODE 'def'
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2550 75: q BINPUT 8
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2551 77: s SETITEM
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2552 78: e APPENDS (MARK at 3)
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 68586
diff changeset
2553 79: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2554 highest protocol among opcodes = 1
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2555
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2556 Exercise the INST/OBJ/BUILD family.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2557
51022
f7f3d841da2c Merged revisions 68908 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50946
diff changeset
2558 >>> import pickletools
f7f3d841da2c Merged revisions 68908 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50946
diff changeset
2559 >>> dis(pickle.dumps(pickletools.dis, 0))
f7f3d841da2c Merged revisions 68908 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50946
diff changeset
2560 0: c GLOBAL 'pickletools dis'
f7f3d841da2c Merged revisions 68908 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50946
diff changeset
2561 17: p PUT 0
f7f3d841da2c Merged revisions 68908 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50946
diff changeset
2562 20: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2563 highest protocol among opcodes = 0
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2564
34590
ff3def77b129 An instance of class PicklingError was used here simply as an example of
Tim Peters <tim.peters@gmail.com>
parents: 34128
diff changeset
2565 >>> from pickletools import _Example
ff3def77b129 An instance of class PicklingError was used here simply as an example of
Tim Peters <tim.peters@gmail.com>
parents: 34128
diff changeset
2566 >>> x = [_Example(42)] * 2
27431
f8a90085acc4 Begin the change from 'binary vs. text mode' to 'protocol 0, 1, 2'.
Guido van Rossum <guido@python.org>
parents: 27429
diff changeset
2567 >>> dis(pickle.dumps(x, 0))
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2568 0: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2569 1: l LIST (MARK at 0)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2570 2: p PUT 0
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2571 5: c GLOBAL 'copy_reg _reconstructor'
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2572 30: p PUT 1
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2573 33: ( MARK
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2574 34: c GLOBAL 'pickletools _Example'
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2575 56: p PUT 2
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2576 59: c GLOBAL '__builtin__ object'
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2577 79: p PUT 3
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2578 82: N NONE
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2579 83: t TUPLE (MARK at 33)
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2580 84: p PUT 4
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2581 87: R REDUCE
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2582 88: p PUT 5
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2583 91: ( MARK
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2584 92: d DICT (MARK at 91)
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2585 93: p PUT 6
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2586 96: V UNICODE 'value'
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2587 103: p PUT 7
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2588 106: L LONG 42
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2589 111: s SETITEM
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2590 112: b BUILD
50946
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47645
diff changeset
2591 113: a APPEND
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2592 114: g GET 5
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2593 117: a APPEND
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2594 118: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2595 highest protocol among opcodes = 0
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2596
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2597 >>> dis(pickle.dumps(x, 1))
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2598 0: ] EMPTY_LIST
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2599 1: q BINPUT 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2600 3: ( MARK
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2601 4: c GLOBAL 'copy_reg _reconstructor'
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2602 29: q BINPUT 1
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2603 31: ( MARK
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2604 32: c GLOBAL 'pickletools _Example'
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2605 54: q BINPUT 2
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2606 56: c GLOBAL '__builtin__ object'
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2607 76: q BINPUT 3
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2608 78: N NONE
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2609 79: t TUPLE (MARK at 31)
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2610 80: q BINPUT 4
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2611 82: R REDUCE
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2612 83: q BINPUT 5
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2613 85: } EMPTY_DICT
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2614 86: q BINPUT 6
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2615 88: X BINUNICODE 'value'
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2616 98: q BINPUT 7
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2617 100: K BININT1 42
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2618 102: s SETITEM
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2619 103: b BUILD
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2620 104: h BINGET 5
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2621 106: e APPENDS (MARK at 3)
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54215
diff changeset
2622 107: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2623 highest protocol among opcodes = 1
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2624
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2625 Try "the canonical" recursive-object test.
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2626
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2627 >>> L = []
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2628 >>> T = L,
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2629 >>> L.append(T)
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2630 >>> L[0] is T
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2631 True
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2632 >>> T[0] is L
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2633 True
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2634 >>> L[0][0] is L
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2635 True
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2636 >>> T[0][0] is T
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2637 True
27431
f8a90085acc4 Begin the change from 'binary vs. text mode' to 'protocol 0, 1, 2'.
Guido van Rossum <guido@python.org>
parents: 27429
diff changeset
2638 >>> dis(pickle.dumps(L, 0))
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2639 0: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2640 1: l LIST (MARK at 0)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2641 2: p PUT 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2642 5: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2643 6: g GET 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2644 9: t TUPLE (MARK at 5)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2645 10: p PUT 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2646 13: a APPEND
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2647 14: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2648 highest protocol among opcodes = 0
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2649
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2650 >>> dis(pickle.dumps(L, 1))
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2651 0: ] EMPTY_LIST
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2652 1: q BINPUT 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2653 3: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2654 4: h BINGET 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2655 6: t TUPLE (MARK at 3)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2656 7: q BINPUT 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2657 9: a APPEND
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2658 10: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2659 highest protocol among opcodes = 1
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2660
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2661 Note that, in the protocol 0 pickle of the recursive tuple, the disassembler
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2662 has to emulate the stack in order to realize that the POP opcode at 16 gets
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2663 rid of the MARK at 0.
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2664
27431
f8a90085acc4 Begin the change from 'binary vs. text mode' to 'protocol 0, 1, 2'.
Guido van Rossum <guido@python.org>
parents: 27429
diff changeset
2665 >>> dis(pickle.dumps(T, 0))
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2666 0: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2667 1: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2668 2: l LIST (MARK at 1)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2669 3: p PUT 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2670 6: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2671 7: g GET 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2672 10: t TUPLE (MARK at 6)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2673 11: p PUT 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2674 14: a APPEND
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2675 15: 0 POP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2676 16: 0 POP (MARK at 0)
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2677 17: g GET 1
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2678 20: . STOP
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2679 highest protocol among opcodes = 0
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2680
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2681 >>> dis(pickle.dumps(T, 1))
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2682 0: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2683 1: ] EMPTY_LIST
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2684 2: q BINPUT 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2685 4: ( MARK
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2686 5: h BINGET 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2687 7: t TUPLE (MARK at 4)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2688 8: q BINPUT 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2689 10: a APPEND
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2690 11: 1 POP_MARK (MARK at 0)
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2691 12: h BINGET 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2692 14: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2693 highest protocol among opcodes = 1
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2694
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2695 Try protocol 2.
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2696
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2697 >>> dis(pickle.dumps(L, 2))
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2698 0: \x80 PROTO 2
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2699 2: ] EMPTY_LIST
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2700 3: q BINPUT 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2701 5: h BINGET 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2702 7: \x85 TUPLE1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2703 8: q BINPUT 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2704 10: a APPEND
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2705 11: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2706 highest protocol among opcodes = 2
27471
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2707
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2708 >>> dis(pickle.dumps(T, 2))
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2709 0: \x80 PROTO 2
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2710 2: ] EMPTY_LIST
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2711 3: q BINPUT 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2712 5: h BINGET 0
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2713 7: \x85 TUPLE1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2714 8: q BINPUT 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2715 10: a APPEND
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2716 11: 0 POP
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2717 12: h BINGET 1
05f7e1c65d7e dis(): Not all opcodes are printable anymore, so print the repr
Tim Peters <tim.peters@gmail.com>
parents: 27468
diff changeset
2718 14: . STOP
27522
0f9eb8413b61 dis(): This had a problem with proto 0 pickles, in that POP sometimes
Tim Peters <tim.peters@gmail.com>
parents: 27518
diff changeset
2719 highest protocol among opcodes = 2
63055
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2720
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2721 Try protocol 3 with annotations:
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2722
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2723 >>> dis(pickle.dumps(T, 3), annotate=1)
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2724 0: \x80 PROTO 3 Protocol version indicator.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2725 2: ] EMPTY_LIST Push an empty list.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2726 3: q BINPUT 0 Store the stack top into the memo. The stack is not popped.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2727 5: h BINGET 0 Read an object from the memo and push it on the stack.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2728 7: \x85 TUPLE1 Build a one-tuple out of the topmost item on the stack.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2729 8: q BINPUT 1 Store the stack top into the memo. The stack is not popped.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2730 10: a APPEND Append an object to a list.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2731 11: 0 POP Discard the top stack item, shrinking the stack by one item.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2732 12: h BINGET 1 Read an object from the memo and push it on the stack.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2733 14: . STOP Stop the unpickling machine.
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2734 highest protocol among opcodes = 2
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2735
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2736 """
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2737
27703
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2738 _memo_test = r"""
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2739 >>> import pickle
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
2740 >>> import io
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
2741 >>> f = io.BytesIO()
27703
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2742 >>> p = pickle.Pickler(f, 2)
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2743 >>> x = [1, 2, 3]
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2744 >>> p.dump(x)
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2745 >>> p.dump(x)
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2746 >>> f.seek(0)
41651
44ebae4f61c4 Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
2747 0
27703
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2748 >>> memo = {}
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2749 >>> dis(f, memo=memo)
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2750 0: \x80 PROTO 2
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2751 2: ] EMPTY_LIST
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2752 3: q BINPUT 0
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2753 5: ( MARK
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2754 6: K BININT1 1
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2755 8: K BININT1 2
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2756 10: K BININT1 3
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2757 12: e APPENDS (MARK at 5)
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2758 13: . STOP
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2759 highest protocol among opcodes = 2
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2760 >>> dis(f, memo=memo)
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2761 14: \x80 PROTO 2
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2762 16: h BINGET 0
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2763 18: . STOP
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2764 highest protocol among opcodes = 2
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2765 """
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2766
27468
6ffafcef98fb Fix one disassembly output now that empty tuples are no longer
Guido van Rossum <guido@python.org>
parents: 27463
diff changeset
2767 __test__ = {'disassembler_test': _dis_test,
27703
0428d59069a9 dis(): Added an optional memo argument, so that multiple pickles in a
Tim Peters <tim.peters@gmail.com>
parents: 27552
diff changeset
2768 'disassembler_memo_test': _memo_test,
27421
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2769 }
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2770
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2771 def _test():
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2772 import doctest
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2773 return doctest.testmod()
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2774
0aeaa3d1e0ff Moving pickletools.py from the sandbox into the std library. I started
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2775 if __name__ == "__main__":
62752
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2776 import sys, argparse
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2777 parser = argparse.ArgumentParser(
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2778 description='disassemble one or more pickle files')
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2779 parser.add_argument(
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2780 'pickle_file', type=argparse.FileType('br'),
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2781 nargs='*', help='the pickle file')
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2782 parser.add_argument(
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2783 '-o', '--output', default=sys.stdout, type=argparse.FileType('w'),
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2784 help='the file where the output should be written')
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2785 parser.add_argument(
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2786 '-m', '--memo', action='store_true',
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2787 help='preserve memo between disassemblies')
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2788 parser.add_argument(
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2789 '-l', '--indentlevel', default=4, type=int,
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2790 help='the number of blanks by which to indent a new MARK level')
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2791 parser.add_argument(
63055
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2792 '-a', '--annotate', action='store_true',
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2793 help='annotate each line with a short opcode description')
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2794 parser.add_argument(
62752
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2795 '-p', '--preamble', default="==> {name} <==",
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2796 help='if more than one pickle file is specified, print this before'
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2797 ' each disassembly')
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2798 parser.add_argument(
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2799 '-t', '--test', action='store_true',
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2800 help='run self-test suite')
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2801 parser.add_argument(
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2802 '-v', action='store_true',
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2803 help='run verbosely; only affects self-test run')
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2804 args = parser.parse_args()
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2805 if args.test:
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2806 _test()
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2807 else:
63055
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2808 annotate = 30 if args.annotate else 0
62752
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2809 if not args.pickle_file:
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2810 parser.print_help()
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2811 elif len(args.pickle_file) == 1:
63055
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2812 dis(args.pickle_file[0], args.output, None,
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2813 args.indentlevel, annotate)
62752
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2814 else:
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2815 memo = {} if args.memo else None
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2816 for f in args.pickle_file:
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2817 preamble = args.preamble.format(name=f.name)
e42baf678d14 Issue #9094: Make python -m pickletools disassemble pickles given in
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
2818 args.output.write(preamble + '\n')
63055
cf4b833a47de Issue #9268: Add annotation option to pickletools.dis
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63040
diff changeset
2819 dis(f, args.output, memo, args.indentlevel, annotate)