Mercurial > cpython-lru_cache-weakref
annotate Lib/pickle.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 5832d3622473 |
| children | c9d205e2dd02 |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11377
diff
changeset
|
1 """Create portable serialized representations of Python objects. |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
46867
dd5513b472dc
Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46636
diff
changeset
|
3 See module copyreg for a mechanism for registering custom picklers. |
|
27425
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
4 See module pickletools source for extensive comments. |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
6 Classes: |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
8 Pickler |
|
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
9 Unpickler |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
11 Functions: |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
13 dump(object, file) |
|
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
14 dumps(object) -> string |
|
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
15 load(file) -> object |
|
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
16 loads(string) -> object |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
18 Misc variables: |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
7274
f5ea1b30cb3f
Fixed typo in docstring: "__ version__" --> "__version__"
Fred Drake <fdrake@acm.org>
parents:
7166
diff
changeset
|
20 __version__ |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
21 format_version |
|
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
22 compatible_formats |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 """ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
26 from types import FunctionType, BuiltinFunctionType |
|
46867
dd5513b472dc
Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46636
diff
changeset
|
27 from copyreg import dispatch_table |
|
dd5513b472dc
Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46636
diff
changeset
|
28 from copyreg import _extension_registry, _inverted_registry, _extension_cache |
| 9439 | 29 import marshal |
| 30 import sys | |
| 31 import struct | |
|
16655
1d7aed076964
add module-level constants to __all__
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
32 import re |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
33 import io |
|
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
|
34 import codecs |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
35 import _compat_pickle |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
| 16474 | 37 __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler", |
| 38 "Unpickler", "dump", "dumps", "load", "loads"] | |
| 39 | |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
40 # Shortcut for use in isinstance testing |
|
46636
032a0a0d3fa9
Removed memoryview objects from bytes_types.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45812
diff
changeset
|
41 bytes_types = (bytes, bytearray) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
42 |
|
27506
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
43 # These are purely informational; no code uses these. |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
44 format_version = "3.0" # File format version we write |
|
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
|
45 compatible_formats = ["1.0", # Original protocol 0 |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
46 "1.1", # Protocol 0 with INST added |
|
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
|
47 "1.2", # Original protocol 1 |
|
f8a90085acc4
Begin the change from 'binary vs. text mode' to 'protocol 0, 1, 2'.
Guido van Rossum <guido@python.org>
parents:
27429
diff
changeset
|
48 "1.3", # Protocol 1 with BINFLOAT added |
|
f8a90085acc4
Begin the change from 'binary vs. text mode' to 'protocol 0, 1, 2'.
Guido van Rossum <guido@python.org>
parents:
27429
diff
changeset
|
49 "2.0", # Protocol 2 |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
50 "3.0", # Protocol 3 |
|
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
|
51 ] # Old format versions we can read |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
52 |
|
42322
3e33215c66ed
Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents:
42319
diff
changeset
|
53 # This is the highest protocol number we know how to read. |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
54 HIGHEST_PROTOCOL = 3 |
|
27864
b4eb34372bdb
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents:
27825
diff
changeset
|
55 |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
56 # The protocol we write by default. May be less than HIGHEST_PROTOCOL. |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
57 # We intentionally write a protocol that Python 2.x cannot read; |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
58 # there are too many issues with that. |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
59 DEFAULT_PROTOCOL = 3 |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
60 |
|
27453
3509ad07d156
Add a comment explaining that struct.pack() beats marshal.dumps(), but
Guido van Rossum <guido@python.org>
parents:
27452
diff
changeset
|
61 # Why use struct.pack() for pickling but marshal.loads() for |
|
27506
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
62 # unpickling? struct.pack() is 40% faster than marshal.dumps(), but |
|
27453
3509ad07d156
Add a comment explaining that struct.pack() beats marshal.dumps(), but
Guido van Rossum <guido@python.org>
parents:
27452
diff
changeset
|
63 # marshal.loads() is twice as fast as struct.unpack()! |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
64 mloads = marshal.loads |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
65 |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
66 class PickleError(Exception): |
|
23579
4e3ea55f7e11
Remove comment about inheritance, look one line up
Neal Norwitz <nnorwitz@gmail.com>
parents:
23565
diff
changeset
|
67 """A common base class for the other pickling exceptions.""" |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
68 pass |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
69 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
70 class PicklingError(PickleError): |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
71 """This exception is raised when an unpicklable object is passed to the |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
72 dump() method. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
73 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
74 """ |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
75 pass |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
76 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
77 class UnpicklingError(PickleError): |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
78 """This exception is raised when there is a problem unpickling an object, |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
79 such as a security violation. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
80 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
81 Note that other exceptions may also be raised during unpickling, including |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
82 (but not necessarily limited to) AttributeError, EOFError, ImportError, |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
83 and IndexError. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
84 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
85 """ |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
86 pass |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 |
|
27506
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
88 # An instance of _Stop is raised by Unpickler.load_stop() in response to |
|
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
89 # the STOP opcode, passing the object that is the result of unpickling. |
|
15552
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
90 class _Stop(Exception): |
|
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
91 def __init__(self, value): |
|
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
92 self.value = value |
|
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
93 |
|
27482
7c8dfd3f4ae9
Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents:
27478
diff
changeset
|
94 # Jython has PyStringMap; it's a dict subclass with string keys |
|
8431
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
95 try: |
|
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
96 from org.python.core import PyStringMap |
|
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
97 except ImportError: |
|
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
98 PyStringMap = None |
|
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
99 |
|
27425
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
100 # Pickle opcodes. See pickletools.py for extensive docs. The listing |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
101 # here is in kind-of alphabetical order of 1-character pickle code. |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
102 # pickletools groups them by purpose. |
|
20020
b70c08813ba9
Make these modules work when Python is compiled without Unicode support.
Guido van Rossum <guido@python.org>
parents:
19544
diff
changeset
|
103 |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
104 MARK = b'(' # push special markobject on stack |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
105 STOP = b'.' # every pickle ends with STOP |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
106 POP = b'0' # discard topmost stack item |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
107 POP_MARK = b'1' # discard stack top through topmost markobject |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
108 DUP = b'2' # duplicate top stack item |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
109 FLOAT = b'F' # push float object; decimal string argument |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
110 INT = b'I' # push integer or bool; decimal string argument |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
111 BININT = b'J' # push four-byte signed int |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
112 BININT1 = b'K' # push 1-byte unsigned int |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
113 LONG = b'L' # push long; decimal string argument |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
114 BININT2 = b'M' # push 2-byte unsigned int |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
115 NONE = b'N' # push None |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
116 PERSID = b'P' # push persistent object; id is taken from string arg |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
117 BINPERSID = b'Q' # " " " ; " " " " stack |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
118 REDUCE = b'R' # apply callable to argtuple, both on stack |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
119 STRING = b'S' # push string; NL-terminated string argument |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
120 BINSTRING = b'T' # push string; counted binary string argument |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
121 SHORT_BINSTRING= b'U' # " " ; " " " " < 256 bytes |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
122 UNICODE = b'V' # push Unicode string; raw-unicode-escaped'd argument |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
123 BINUNICODE = b'X' # " " " ; counted UTF-8 string argument |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
124 APPEND = b'a' # append stack top to list below it |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
125 BUILD = b'b' # call __setstate__ or __dict__.update() |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
126 GLOBAL = b'c' # push self.find_class(modname, name); 2 string args |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
127 DICT = b'd' # build a dict from stack items |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
128 EMPTY_DICT = b'}' # push empty dict |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
129 APPENDS = b'e' # extend list on stack by topmost stack slice |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
130 GET = b'g' # push item from memo on stack; index is string arg |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
131 BINGET = b'h' # " " " " " " ; " " 1-byte arg |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
132 INST = b'i' # build & push class instance |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
133 LONG_BINGET = b'j' # push item from memo on stack; index is 4-byte arg |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
134 LIST = b'l' # build list from topmost stack items |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
135 EMPTY_LIST = b']' # push empty list |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
136 OBJ = b'o' # build & push class instance |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
137 PUT = b'p' # store stack top in memo; index is string arg |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
138 BINPUT = b'q' # " " " " " ; " " 1-byte arg |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
139 LONG_BINPUT = b'r' # " " " " " ; " " 4-byte arg |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
140 SETITEM = b's' # add key+value pair to dict |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
141 TUPLE = b't' # build tuple from topmost stack items |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
142 EMPTY_TUPLE = b')' # push empty tuple |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
143 SETITEMS = b'u' # modify dict by adding topmost key+value pairs |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
144 BINFLOAT = b'G' # push float; arg is 8-byte float encoding |
|
27425
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
145 |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
146 TRUE = b'I01\n' # not an opcode; see INT docs in pickletools.py |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
147 FALSE = b'I00\n' # not an opcode; see INT docs in pickletools.py |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22569
diff
changeset
|
148 |
|
27512
a2d96e7d91a1
Declare Protocol 2 as implemented.
Guido van Rossum <guido@python.org>
parents:
27511
diff
changeset
|
149 # Protocol 2 |
|
27429
b465c9d2196a
Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents:
27428
diff
changeset
|
150 |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
151 PROTO = b'\x80' # identify pickle protocol |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
152 NEWOBJ = b'\x81' # build object by applying cls.__new__ to argtuple |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
153 EXT1 = b'\x82' # push object from extension registry; 1-byte index |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
154 EXT2 = b'\x83' # ditto, but 2-byte index |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
155 EXT4 = b'\x84' # ditto, but 4-byte index |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
156 TUPLE1 = b'\x85' # build 1-tuple from stack top |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
157 TUPLE2 = b'\x86' # build 2-tuple from two topmost stack items |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
158 TUPLE3 = b'\x87' # build 3-tuple from three topmost stack items |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
159 NEWTRUE = b'\x88' # push True |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
160 NEWFALSE = b'\x89' # push False |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
161 LONG1 = b'\x8a' # push long from < 256 bytes |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
162 LONG4 = b'\x8b' # push really big long |
|
27429
b465c9d2196a
Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents:
27428
diff
changeset
|
163 |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
164 _tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3] |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
165 |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
166 # Protocol 3 (Python 3.x) |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
167 |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
168 BINBYTES = b'B' # push bytes; counted binary string argument |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
169 SHORT_BINBYTES = b'C' # " " ; " " " " < 256 bytes |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 |
|
16655
1d7aed076964
add module-level constants to __all__
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
171 __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)]) |
|
1d7aed076964
add module-level constants to __all__
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
172 |
|
27470
17d87c5ba6e4
Add a few comments. Change the way the protocol is checked (it must
Guido van Rossum <guido@python.org>
parents:
27469
diff
changeset
|
173 # Pickling machinery |
|
17d87c5ba6e4
Add a few comments. Change the way the protocol is checked (it must
Guido van Rossum <guido@python.org>
parents:
27469
diff
changeset
|
174 |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
175 class _Pickler: |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
177 def __init__(self, file, protocol=None, *, fix_imports=True): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
178 """This takes a binary file for writing a pickle data stream. |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
179 |
|
27771
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
180 The optional protocol argument tells the pickler to use the |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
181 given protocol; supported protocols are 0, 1, 2, 3. The default |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
182 protocol is 3; a backward-incompatible protocol designed for |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
183 Python 3.0. |
|
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
|
184 |
|
27562
d42d83e5686c
Change the default protocol back to 0.
Guido van Rossum <guido@python.org>
parents:
27561
diff
changeset
|
185 Specifying a negative protocol version selects the highest |
|
27584
65635c72135e
The C pickle now knows how to deal with a proto= argument. Assorted
Tim Peters <tim.peters@gmail.com>
parents:
27562
diff
changeset
|
186 protocol version supported. The higher the protocol used, the |
|
65635c72135e
The C pickle now knows how to deal with a proto= argument. Assorted
Tim Peters <tim.peters@gmail.com>
parents:
27562
diff
changeset
|
187 more recent the version of Python needed to read the pickle |
|
65635c72135e
The C pickle now knows how to deal with a proto= argument. Assorted
Tim Peters <tim.peters@gmail.com>
parents:
27562
diff
changeset
|
188 produced. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
189 |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
190 The file argument must have a write() method that accepts a single |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
191 bytes argument. It can thus be a file object opened for binary |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
192 writing, a io.BytesIO instance, or any other custom object that |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
193 meets this interface. |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
194 |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
195 If fix_imports is True and protocol is less than 3, pickle will try to |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
196 map the new Python 3.x names to the old module names used in Python |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
197 2.x, so that the pickle data stream is readable with Python 2.x. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
198 """ |
|
27771
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
199 if protocol is None: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
200 protocol = DEFAULT_PROTOCOL |
|
27771
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
201 if protocol < 0: |
|
27864
b4eb34372bdb
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents:
27825
diff
changeset
|
202 protocol = HIGHEST_PROTOCOL |
|
b4eb34372bdb
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents:
27825
diff
changeset
|
203 elif not 0 <= protocol <= HIGHEST_PROTOCOL: |
|
b4eb34372bdb
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents:
27825
diff
changeset
|
204 raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL) |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
205 try: |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
206 self.write = file.write |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
207 except AttributeError: |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
208 raise TypeError("file must have a 'write' attribute") |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
209 self.memo = {} |
|
27771
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
210 self.proto = int(protocol) |
|
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
211 self.bin = protocol >= 1 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27512
diff
changeset
|
212 self.fast = 0 |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
213 self.fix_imports = fix_imports and protocol < 3 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
214 |
|
23331
aae6dc6b2a58
Add Pickler.clear_memo() so the pickle and cPickle modules are more similar.
Fred Drake <fdrake@acm.org>
parents:
22851
diff
changeset
|
215 def clear_memo(self): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
216 """Clears the pickler's "memo". |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
217 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
218 The memo is the data structure that remembers which objects the |
|
27438
18e8ed80e8d1
clear_memo(): Repaired grammar in docstring.
Tim Peters <tim.peters@gmail.com>
parents:
27437
diff
changeset
|
219 pickler has already seen, so that shared or recursive objects are |
|
18e8ed80e8d1
clear_memo(): Repaired grammar in docstring.
Tim Peters <tim.peters@gmail.com>
parents:
27437
diff
changeset
|
220 pickled by reference and not by value. This method is useful when |
|
18e8ed80e8d1
clear_memo(): Repaired grammar in docstring.
Tim Peters <tim.peters@gmail.com>
parents:
27437
diff
changeset
|
221 re-using picklers. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
222 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
223 """ |
|
23331
aae6dc6b2a58
Add Pickler.clear_memo() so the pickle and cPickle modules are more similar.
Fred Drake <fdrake@acm.org>
parents:
22851
diff
changeset
|
224 self.memo.clear() |
|
aae6dc6b2a58
Add Pickler.clear_memo() so the pickle and cPickle modules are more similar.
Fred Drake <fdrake@acm.org>
parents:
22851
diff
changeset
|
225 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
226 def dump(self, obj): |
|
27584
65635c72135e
The C pickle now knows how to deal with a proto= argument. Assorted
Tim Peters <tim.peters@gmail.com>
parents:
27562
diff
changeset
|
227 """Write a pickled representation of obj to the open file.""" |
|
50545
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
228 # Check whether Pickler was initialized correctly. This is |
|
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
229 # only needed to mimic the behavior of _pickle.Pickler.dump(). |
|
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
230 if not hasattr(self, "write"): |
|
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
231 raise PicklingError("Pickler.__init__() was not called by " |
|
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
232 "%s.__init__()" % (self.__class__.__name__,)) |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
233 if self.proto >= 2: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
234 self.write(PROTO + bytes([self.proto])) |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
235 self.save(obj) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
236 self.write(STOP) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
237 |
|
27362
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
238 def memoize(self, obj): |
|
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
239 """Store an object in the memo.""" |
|
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
240 |
|
27427
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
241 # The Pickler memo is a dictionary mapping object ids to 2-tuples |
|
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
242 # that contain the Unpickler memo key and the object being memoized. |
|
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
243 # The memo key is written to the pickle and will become |
|
27362
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
244 # the key in the Unpickler's memo. The object is stored in the |
|
27427
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
245 # Pickler memo so that transient objects are kept alive during |
|
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
246 # pickling. |
|
27362
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
247 |
|
27427
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
248 # The use of the Unpickler memo length as the memo key is just a |
|
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
249 # convention. The only requirement is that the memo values be unique. |
|
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
250 # But there appears no advantage to any other scheme, and this |
|
27439
d9d24ce1cbf6
Repaired grammar in new comment.
Tim Peters <tim.peters@gmail.com>
parents:
27438
diff
changeset
|
251 # scheme allows the Unpickler memo to be implemented as a plain (but |
|
27427
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
252 # growable) array, indexed by memo key. |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27512
diff
changeset
|
253 if self.fast: |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27512
diff
changeset
|
254 return |
|
27529
696c42da7875
There was a subtle big in save_newobj(): it used self.save_global(t)
Guido van Rossum <guido@python.org>
parents:
27528
diff
changeset
|
255 assert id(obj) not in self.memo |
|
27362
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
256 memo_len = len(self.memo) |
|
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
257 self.write(self.put(memo_len)) |
|
27443
8f13729a94a9
Several routines appeared to inline the guts of memoize(), possibly for
Tim Peters <tim.peters@gmail.com>
parents:
27442
diff
changeset
|
258 self.memo[id(obj)] = memo_len, obj |
|
27362
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
259 |
| 27428 | 260 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i. |
|
27452
7fb5e8a541ab
Got rid of mdumps; I timed it, and struct.pack("<i", x) is more than
Guido van Rossum <guido@python.org>
parents:
27451
diff
changeset
|
261 def put(self, i, pack=struct.pack): |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
262 if self.bin: |
|
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
263 if i < 256: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
264 return BINPUT + bytes([i]) |
|
27452
7fb5e8a541ab
Got rid of mdumps; I timed it, and struct.pack("<i", x) is more than
Guido van Rossum <guido@python.org>
parents:
27451
diff
changeset
|
265 else: |
|
7fb5e8a541ab
Got rid of mdumps; I timed it, and struct.pack("<i", x) is more than
Guido van Rossum <guido@python.org>
parents:
27451
diff
changeset
|
266 return LONG_BINPUT + pack("<i", i) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
267 |
|
42902
62874b34d95d
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42322
diff
changeset
|
268 return PUT + repr(i).encode("ascii") + b'\n' |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
269 |
| 27428 | 270 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i. |
|
27452
7fb5e8a541ab
Got rid of mdumps; I timed it, and struct.pack("<i", x) is more than
Guido van Rossum <guido@python.org>
parents:
27451
diff
changeset
|
271 def get(self, i, pack=struct.pack): |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
272 if self.bin: |
|
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
273 if i < 256: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
274 return BINGET + bytes([i]) |
|
27452
7fb5e8a541ab
Got rid of mdumps; I timed it, and struct.pack("<i", x) is more than
Guido van Rossum <guido@python.org>
parents:
27451
diff
changeset
|
275 else: |
|
7fb5e8a541ab
Got rid of mdumps; I timed it, and struct.pack("<i", x) is more than
Guido van Rossum <guido@python.org>
parents:
27451
diff
changeset
|
276 return LONG_BINGET + pack("<i", i) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
277 |
|
42902
62874b34d95d
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42322
diff
changeset
|
278 return GET + repr(i).encode("ascii") + b'\n' |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15605
diff
changeset
|
279 |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
280 def save(self, obj, save_persistent_id=True): |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
281 # Check for persistent id (defined by a subclass) |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
282 pid = self.persistent_id(obj) |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
283 if pid is not None and save_persistent_id: |
|
26201
7e58a1504872
Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25332
diff
changeset
|
284 self.save_pers(pid) |
|
7e58a1504872
Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25332
diff
changeset
|
285 return |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
286 |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
287 # Check the memo |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
288 x = self.memo.get(id(obj)) |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
289 if x: |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
290 self.write(self.get(x[0])) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
291 return |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
293 # Check the type dispatch table |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
294 t = type(obj) |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
295 f = self.dispatch.get(t) |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
296 if f: |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
297 f(self, obj) # Call unbound method with explicit self |
|
27442
6b0dbde0fed7
save(): Fiddled the control flow to put the normal case where it
Tim Peters <tim.peters@gmail.com>
parents:
27441
diff
changeset
|
298 return |
| 6824 | 299 |
|
75391
f7a9a10ae0c0
Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73958
diff
changeset
|
300 # Check private dispatch table if any, or else copyreg.dispatch_table |
|
f7a9a10ae0c0
Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73958
diff
changeset
|
301 reduce = getattr(self, 'dispatch_table', dispatch_table).get(t) |
|
27948
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
302 if reduce: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
303 rv = reduce(obj) |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
304 else: |
|
72639
760ac320fa3d
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
72121
diff
changeset
|
305 # Check for a class with a custom metaclass; treat as regular class |
|
760ac320fa3d
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
72121
diff
changeset
|
306 try: |
|
760ac320fa3d
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
72121
diff
changeset
|
307 issc = issubclass(t, type) |
|
760ac320fa3d
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
72121
diff
changeset
|
308 except TypeError: # t is not a class (old Boost; see SF #502085) |
|
760ac320fa3d
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
72121
diff
changeset
|
309 issc = False |
|
760ac320fa3d
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
72121
diff
changeset
|
310 if issc: |
|
760ac320fa3d
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
72121
diff
changeset
|
311 self.save_global(obj) |
|
760ac320fa3d
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
72121
diff
changeset
|
312 return |
|
760ac320fa3d
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
72121
diff
changeset
|
313 |
|
27948
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
314 # Check for a __reduce_ex__ method, fall back to __reduce__ |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
315 reduce = getattr(obj, "__reduce_ex__", None) |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
316 if reduce: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
317 rv = reduce(self.proto) |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
318 else: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
319 reduce = getattr(obj, "__reduce__", None) |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
320 if reduce: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
321 rv = reduce() |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
322 else: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
323 raise PicklingError("Can't pickle %r object: %r" % |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
324 (t.__name__, obj)) |
|
27442
6b0dbde0fed7
save(): Fiddled the control flow to put the normal case where it
Tim Peters <tim.peters@gmail.com>
parents:
27441
diff
changeset
|
325 |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
326 # Check for string returned by reduce(), meaning "save as global" |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
43521
diff
changeset
|
327 if isinstance(rv, str): |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
328 self.save_global(obj, rv) |
|
27442
6b0dbde0fed7
save(): Fiddled the control flow to put the normal case where it
Tim Peters <tim.peters@gmail.com>
parents:
27441
diff
changeset
|
329 return |
|
6b0dbde0fed7
save(): Fiddled the control flow to put the normal case where it
Tim Peters <tim.peters@gmail.com>
parents:
27441
diff
changeset
|
330 |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
331 # Assert that reduce() returned a tuple |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
332 if not isinstance(rv, tuple): |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
333 raise PicklingError("%s must return string or tuple" % reduce) |
|
27442
6b0dbde0fed7
save(): Fiddled the control flow to put the normal case where it
Tim Peters <tim.peters@gmail.com>
parents:
27441
diff
changeset
|
334 |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
335 # Assert that it returned an appropriately sized tuple |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
336 l = len(rv) |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
337 if not (2 <= l <= 5): |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
338 raise PicklingError("Tuple returned by %s must have " |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
339 "two to five elements" % reduce) |
|
27442
6b0dbde0fed7
save(): Fiddled the control flow to put the normal case where it
Tim Peters <tim.peters@gmail.com>
parents:
27441
diff
changeset
|
340 |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
341 # Save the reduce() output and finally memoize the object |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
342 self.save_reduce(obj=obj, *rv) |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
344 def persistent_id(self, obj): |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
345 # This exists so a subclass can override it |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
346 return None |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
347 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
348 def save_pers(self, pid): |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
349 # Save a persistent id reference |
|
27444
1f6327140fc2
save_pers(): Switched the order of cases, to get rid of a "not", and to
Tim Peters <tim.peters@gmail.com>
parents:
27443
diff
changeset
|
350 if self.bin: |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
351 self.save(pid, save_persistent_id=False) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
352 self.write(BINPERSID) |
|
27444
1f6327140fc2
save_pers(): Switched the order of cases, to get rid of a "not", and to
Tim Peters <tim.peters@gmail.com>
parents:
27443
diff
changeset
|
353 else: |
|
42902
62874b34d95d
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42322
diff
changeset
|
354 self.write(PERSID + str(pid).encode("ascii") + b'\n') |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
356 def save_reduce(self, func, args, state=None, |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
357 listitems=None, dictitems=None, obj=None): |
|
29681
c149dd4cfd66
Fix grammar in comment.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27952
diff
changeset
|
358 # This API is called by some subclasses |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
359 |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
360 # Assert that args is a tuple |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
361 if not isinstance(args, tuple): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
362 raise PicklingError("args from save_reduce() should be a tuple") |
|
27425
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
363 |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
364 # Assert that func is callable |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
72639
diff
changeset
|
365 if not callable(func): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
366 raise PicklingError("func from save_reduce() should be callable") |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
367 |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
368 save = self.save |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
369 write = self.write |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
370 |
|
27553
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
371 # Protocol 2 special case: if func's name is __newobj__, use NEWOBJ |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
372 if self.proto >= 2 and getattr(func, "__name__", "") == "__newobj__": |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
373 # A __reduce__ implementation can direct protocol 2 to |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
374 # use the more efficient NEWOBJ opcode, while still |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
375 # allowing protocol 0 and 1 to work normally. For this to |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
376 # work, the function returned by __reduce__ should be |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
377 # called __newobj__, and its first argument should be a |
|
73949
021e5bb297d1
Issue #13575: there is only one class type.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
73345
diff
changeset
|
378 # class. The implementation for __newobj__ |
|
27553
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
379 # should be as follows, although pickle has no way to |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
380 # verify this: |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
381 # |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
382 # def __newobj__(cls, *args): |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
383 # return cls.__new__(cls, *args) |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
384 # |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
385 # Protocols 0 and 1 will pickle a reference to __newobj__, |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
386 # while protocol 2 (and above) will pickle a reference to |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
387 # cls, the remaining args tuple, and the NEWOBJ code, |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
388 # which calls cls.__new__(cls, *args) at unpickling time |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
389 # (see load_newobj below). If __reduce__ returns a |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
390 # three-tuple, the state from the third tuple item will be |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
391 # pickled regardless of the protocol, calling __setstate__ |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
392 # at unpickling time (see load_build below). |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
393 # |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
394 # Note that no standard __newobj__ implementation exists; |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
395 # you have to provide your own. This is to enforce |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
396 # compatibility with Python 2.2 (pickles written using |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
397 # protocol 0 or 1 in Python 2.3 should be unpicklable by |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
398 # Python 2.2). |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
399 cls = args[0] |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
400 if not hasattr(cls, "__new__"): |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
401 raise PicklingError( |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
402 "args[0] from __newobj__ args has no __new__") |
|
27554
3ae0a2f87aa0
Pass the object to save_reduce(), so the memoize() call can go into
Guido van Rossum <guido@python.org>
parents:
27553
diff
changeset
|
403 if obj is not None and cls is not obj.__class__: |
|
3ae0a2f87aa0
Pass the object to save_reduce(), so the memoize() call can go into
Guido van Rossum <guido@python.org>
parents:
27553
diff
changeset
|
404 raise PicklingError( |
|
3ae0a2f87aa0
Pass the object to save_reduce(), so the memoize() call can go into
Guido van Rossum <guido@python.org>
parents:
27553
diff
changeset
|
405 "args[0] from __newobj__ args has the wrong class") |
|
27553
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
406 args = args[1:] |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
407 save(cls) |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
408 save(args) |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
409 write(NEWOBJ) |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
410 else: |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
411 save(func) |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
412 save(args) |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
413 write(REDUCE) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15605
diff
changeset
|
414 |
|
27554
3ae0a2f87aa0
Pass the object to save_reduce(), so the memoize() call can go into
Guido van Rossum <guido@python.org>
parents:
27553
diff
changeset
|
415 if obj is not None: |
|
3ae0a2f87aa0
Pass the object to save_reduce(), so the memoize() call can go into
Guido van Rossum <guido@python.org>
parents:
27553
diff
changeset
|
416 self.memoize(obj) |
|
3ae0a2f87aa0
Pass the object to save_reduce(), so the memoize() call can go into
Guido van Rossum <guido@python.org>
parents:
27553
diff
changeset
|
417 |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
418 # More new special cases (that work with older protocols as |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
419 # well): when __reduce__ returns a tuple with 4 or 5 items, |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
420 # the 4th and 5th item should be iterators that provide list |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
421 # items and dict items (as (key, value) tuples), or None. |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
422 |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
423 if listitems is not None: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
424 self._batch_appends(listitems) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
425 |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
426 if dictitems is not None: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
427 self._batch_setitems(dictitems) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
428 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
429 if state is not None: |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
430 save(state) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
431 write(BUILD) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
432 |
|
27475
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
433 # Methods below this point are dispatched through the dispatch table |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
434 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
435 dispatch = {} |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
436 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
437 def save_none(self, obj): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
438 self.write(NONE) |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
439 dispatch[type(None)] = save_none |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
440 |
|
75548
5832d3622473
Fixes #13842: cannot pickle Ellipsis or NotImplemented.
Łukasz Langa <lukasz@langa.pl>
parents:
75391
diff
changeset
|
441 def save_ellipsis(self, obj): |
|
5832d3622473
Fixes #13842: cannot pickle Ellipsis or NotImplemented.
Łukasz Langa <lukasz@langa.pl>
parents:
75391
diff
changeset
|
442 self.save_global(Ellipsis, 'Ellipsis') |
|
5832d3622473
Fixes #13842: cannot pickle Ellipsis or NotImplemented.
Łukasz Langa <lukasz@langa.pl>
parents:
75391
diff
changeset
|
443 dispatch[type(Ellipsis)] = save_ellipsis |
|
5832d3622473
Fixes #13842: cannot pickle Ellipsis or NotImplemented.
Łukasz Langa <lukasz@langa.pl>
parents:
75391
diff
changeset
|
444 |
|
5832d3622473
Fixes #13842: cannot pickle Ellipsis or NotImplemented.
Łukasz Langa <lukasz@langa.pl>
parents:
75391
diff
changeset
|
445 def save_notimplemented(self, obj): |
|
5832d3622473
Fixes #13842: cannot pickle Ellipsis or NotImplemented.
Łukasz Langa <lukasz@langa.pl>
parents:
75391
diff
changeset
|
446 self.save_global(NotImplemented, 'NotImplemented') |
|
5832d3622473
Fixes #13842: cannot pickle Ellipsis or NotImplemented.
Łukasz Langa <lukasz@langa.pl>
parents:
75391
diff
changeset
|
447 dispatch[type(NotImplemented)] = save_notimplemented |
|
5832d3622473
Fixes #13842: cannot pickle Ellipsis or NotImplemented.
Łukasz Langa <lukasz@langa.pl>
parents:
75391
diff
changeset
|
448 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
449 def save_bool(self, obj): |
|
27461
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
450 if self.proto >= 2: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
451 self.write(obj and NEWTRUE or NEWFALSE) |
|
27461
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
452 else: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
453 self.write(obj and TRUE or FALSE) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22569
diff
changeset
|
454 dispatch[bool] = save_bool |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22569
diff
changeset
|
455 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
456 def save_long(self, obj, pack=struct.pack): |
|
40626
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
457 if self.bin: |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
458 # If the int is small enough to fit in a signed 4-byte 2's-comp |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
459 # format, we can store it more efficiently than the general |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
460 # case. |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
461 # First one- and two-byte unsigned ints: |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
462 if obj >= 0: |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
463 if obj <= 0xff: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
464 self.write(BININT1 + bytes([obj])) |
|
40626
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
465 return |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
466 if obj <= 0xffff: |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
467 self.write(BININT2 + bytes([obj&0xff, obj>>8])) |
|
40626
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
468 return |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
469 # Next check for 4-byte signed ints: |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
470 high_bits = obj >> 31 # note that Python shift sign-extends |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
471 if high_bits == 0 or high_bits == -1: |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
472 # All high bits are copies of bit 2**31, so the value |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
473 # fits in a 4-byte signed int. |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
474 self.write(BININT + pack("<i", obj)) |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
475 return |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
476 if self.proto >= 2: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
477 encoded = encode_long(obj) |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
478 n = len(encoded) |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
479 if n < 256: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
480 self.write(LONG1 + bytes([n]) + encoded) |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
481 else: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
482 self.write(LONG4 + pack("<i", n) + encoded) |
|
27585
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
483 return |
|
50944
d1de077447aa
Issue #4842, patch 1/2: fix pickle in Python 3.x so that pickling with the
Mark Dickinson <dickinsm@gmail.com>
parents:
50702
diff
changeset
|
484 self.write(LONG + repr(obj).encode("ascii") + b'L\n') |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
485 dispatch[int] = save_long |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
486 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
487 def save_float(self, obj, pack=struct.pack): |
| 9439 | 488 if self.bin: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
489 self.write(BINFLOAT + pack('>d', obj)) |
| 9439 | 490 else: |
|
42902
62874b34d95d
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42322
diff
changeset
|
491 self.write(FLOAT + repr(obj).encode("ascii") + b'\n') |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
492 dispatch[float] = save_float |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
493 |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
494 def save_bytes(self, obj, pack=struct.pack): |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
495 if self.proto < 3: |
|
73957
14695b4825dc
Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
73165
diff
changeset
|
496 if len(obj) == 0: |
|
14695b4825dc
Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
73165
diff
changeset
|
497 self.save_reduce(bytes, (), obj=obj) |
|
14695b4825dc
Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
73165
diff
changeset
|
498 else: |
|
14695b4825dc
Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
73165
diff
changeset
|
499 self.save_reduce(codecs.encode, |
|
14695b4825dc
Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
73165
diff
changeset
|
500 (str(obj, 'latin1'), 'latin1'), obj=obj) |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
501 return |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
502 n = len(obj) |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
503 if n < 256: |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
504 self.write(SHORT_BINBYTES + bytes([n]) + bytes(obj)) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
505 else: |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
506 self.write(BINBYTES + pack("<i", n) + bytes(obj)) |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
507 self.memoize(obj) |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
508 dispatch[bytes] = save_bytes |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
509 |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
510 def save_str(self, obj, pack=struct.pack): |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
511 if self.bin: |
|
60578
b8f297cdfade
Merged revisions 80031 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
54584
diff
changeset
|
512 encoded = obj.encode('utf-8', 'surrogatepass') |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
513 n = len(encoded) |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
514 self.write(BINUNICODE + pack("<i", n) + encoded) |
|
11989
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
515 else: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
516 obj = obj.replace("\\", "\\u005c") |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
517 obj = obj.replace("\n", "\\u000a") |
|
41615
3b57f0b7eddf
Much more pickling now works.
Guido van Rossum <guido@python.org>
parents:
41613
diff
changeset
|
518 self.write(UNICODE + bytes(obj.encode('raw-unicode-escape')) + |
|
3b57f0b7eddf
Much more pickling now works.
Guido van Rossum <guido@python.org>
parents:
41613
diff
changeset
|
519 b'\n') |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
520 self.memoize(obj) |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
521 dispatch[str] = save_str |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16474
diff
changeset
|
522 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
523 def save_tuple(self, obj): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
524 write = self.write |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
525 proto = self.proto |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
526 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
527 n = len(obj) |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
528 if n == 0: |
|
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
529 if proto: |
|
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
530 write(EMPTY_TUPLE) |
|
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
531 else: |
|
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
532 write(MARK + TUPLE) |
|
27465
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
533 return |
|
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
534 |
|
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
535 save = self.save |
|
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
536 memo = self.memo |
|
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
537 if n <= 3 and proto >= 2: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
538 for element in obj: |
|
27465
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
539 save(element) |
|
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
540 # Subtle. Same as in the big comment below. |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
541 if id(obj) in memo: |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
542 get = self.get(memo[id(obj)][0]) |
|
27465
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
543 write(POP * n + get) |
|
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
544 else: |
|
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
545 write(_tuplesize2code[n]) |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
546 self.memoize(obj) |
|
27465
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
547 return |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
548 |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
549 # proto 0 or proto 1 and tuple isn't empty, or proto > 1 and tuple |
|
27464
ed0d4ae6c6ce
save_tuple(): I believe the new code for TUPLE{1,2,3} in proto 2 was
Tim Peters <tim.peters@gmail.com>
parents:
27461
diff
changeset
|
550 # has more than 3 elements. |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
551 write(MARK) |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
552 for element in obj: |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
553 save(element) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
554 |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
555 if id(obj) in memo: |
|
27451
a230d1200bc3
save_tuple(): Minor rewriting, and added a comment about the subtlety
Tim Peters <tim.peters@gmail.com>
parents:
27450
diff
changeset
|
556 # Subtle. d was not in memo when we entered save_tuple(), so |
|
a230d1200bc3
save_tuple(): Minor rewriting, and added a comment about the subtlety
Tim Peters <tim.peters@gmail.com>
parents:
27450
diff
changeset
|
557 # the process of saving the tuple's elements must have saved |
|
a230d1200bc3
save_tuple(): Minor rewriting, and added a comment about the subtlety
Tim Peters <tim.peters@gmail.com>
parents:
27450
diff
changeset
|
558 # the tuple itself: the tuple is recursive. The proper action |
|
a230d1200bc3
save_tuple(): Minor rewriting, and added a comment about the subtlety
Tim Peters <tim.peters@gmail.com>
parents:
27450
diff
changeset
|
559 # now is to throw away everything we put on the stack, and |
|
a230d1200bc3
save_tuple(): Minor rewriting, and added a comment about the subtlety
Tim Peters <tim.peters@gmail.com>
parents:
27450
diff
changeset
|
560 # simply GET the tuple (it's already constructed). This check |
|
a230d1200bc3
save_tuple(): Minor rewriting, and added a comment about the subtlety
Tim Peters <tim.peters@gmail.com>
parents:
27450
diff
changeset
|
561 # could have been done in the "for element" loop instead, but |
|
a230d1200bc3
save_tuple(): Minor rewriting, and added a comment about the subtlety
Tim Peters <tim.peters@gmail.com>
parents:
27450
diff
changeset
|
562 # recursive tuples are a rare thing. |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
563 get = self.get(memo[id(obj)][0]) |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
564 if proto: |
|
27451
a230d1200bc3
save_tuple(): Minor rewriting, and added a comment about the subtlety
Tim Peters <tim.peters@gmail.com>
parents:
27450
diff
changeset
|
565 write(POP_MARK + get) |
|
a230d1200bc3
save_tuple(): Minor rewriting, and added a comment about the subtlety
Tim Peters <tim.peters@gmail.com>
parents:
27450
diff
changeset
|
566 else: # proto 0 -- POP_MARK not available |
|
27465
8dce7a5e5fcc
save_tuple(): So long as the charter is rewriting for clarity, the snaky
Tim Peters <tim.peters@gmail.com>
parents:
27464
diff
changeset
|
567 write(POP * (n+1) + get) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
568 return |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
569 |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
570 # No recursion. |
|
27443
8f13729a94a9
Several routines appeared to inline the guts of memoize(), possibly for
Tim Peters <tim.peters@gmail.com>
parents:
27442
diff
changeset
|
571 self.write(TUPLE) |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
572 self.memoize(obj) |
|
27362
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
573 |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
574 dispatch[tuple] = save_tuple |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
575 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
576 def save_list(self, obj): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
577 write = self.write |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
578 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
579 if self.bin: |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
580 write(EMPTY_LIST) |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
581 else: # proto 0 -- can't use EMPTY_LIST |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
582 write(MARK + LIST) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
583 |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
584 self.memoize(obj) |
|
47005
42fc922120d9
Change Pickler._batch_appends() and Pickler._batch_setitems() to take
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46867
diff
changeset
|
585 self._batch_appends(obj) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
586 |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
587 dispatch[list] = save_list |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
588 |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
589 _BATCHSIZE = 1000 |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
590 |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
591 def _batch_appends(self, items): |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
592 # Helper to batch up APPENDS sequences |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
593 save = self.save |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
594 write = self.write |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
595 |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
596 if not self.bin: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
597 for x in items: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
598 save(x) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
599 write(APPEND) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
600 return |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
601 |
|
47005
42fc922120d9
Change Pickler._batch_appends() and Pickler._batch_setitems() to take
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46867
diff
changeset
|
602 items = iter(items) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41615
diff
changeset
|
603 r = range(self._BATCHSIZE) |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
604 while items is not None: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
605 tmp = [] |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
606 for i in r: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
607 try: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41464
diff
changeset
|
608 x = next(items) |
|
27738
d8c765c09d8c
Move _better_reduce from copy.py to copy_reg.py, and also use it in
Guido van Rossum <guido@python.org>
parents:
27730
diff
changeset
|
609 tmp.append(x) |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
610 except StopIteration: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
611 items = None |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
612 break |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
613 n = len(tmp) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
614 if n > 1: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
615 write(MARK) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
616 for x in tmp: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
617 save(x) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
618 write(APPENDS) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
619 elif n: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
620 save(tmp[0]) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
621 write(APPEND) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
622 # else tmp is empty, and we're done |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
623 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
624 def save_dict(self, obj): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
625 write = self.write |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
626 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
627 if self.bin: |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
628 write(EMPTY_DICT) |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
629 else: # proto 0 -- can't use EMPTY_DICT |
|
27448
b3cbe7a0142a
save_dict(): Untangled most of the bin-vs-not-bin logic. Also used
Tim Peters <tim.peters@gmail.com>
parents:
27447
diff
changeset
|
630 write(MARK + DICT) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
631 |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
632 self.memoize(obj) |
|
47005
42fc922120d9
Change Pickler._batch_appends() and Pickler._batch_setitems() to take
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46867
diff
changeset
|
633 self._batch_setitems(obj.items()) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
634 |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
635 dispatch[dict] = save_dict |
|
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
636 if PyStringMap is not None: |
|
8431
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
637 dispatch[PyStringMap] = save_dict |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
638 |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
639 def _batch_setitems(self, items): |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
640 # Helper to batch up SETITEMS sequences; proto >= 1 only |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
641 save = self.save |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
642 write = self.write |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
643 |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
644 if not self.bin: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
645 for k, v in items: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
646 save(k) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
647 save(v) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
648 write(SETITEM) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
649 return |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
650 |
|
47005
42fc922120d9
Change Pickler._batch_appends() and Pickler._batch_setitems() to take
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46867
diff
changeset
|
651 items = iter(items) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41615
diff
changeset
|
652 r = range(self._BATCHSIZE) |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
653 while items is not None: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
654 tmp = [] |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
655 for i in r: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
656 try: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41464
diff
changeset
|
657 tmp.append(next(items)) |
|
27561
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
658 except StopIteration: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
659 items = None |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
660 break |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
661 n = len(tmp) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
662 if n > 1: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
663 write(MARK) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
664 for k, v in tmp: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
665 save(k) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
666 save(v) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
667 write(SETITEMS) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
668 elif n: |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
669 k, v = tmp[0] |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
670 save(k) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
671 save(v) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
672 write(SETITEM) |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
673 # else tmp is empty, and we're done |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
674 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
675 def save_global(self, obj, name=None, pack=struct.pack): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
676 write = self.write |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
677 memo = self.memo |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
678 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
679 if name is None: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
680 name = obj.__name__ |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
681 |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27554
diff
changeset
|
682 module = getattr(obj, "__module__", None) |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27554
diff
changeset
|
683 if module is None: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
684 module = whichmodule(obj, name) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
685 |
|
19344
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
686 try: |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
687 __import__(module, level=0) |
|
19344
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
688 mod = sys.modules[module] |
|
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
689 klass = getattr(mod, name) |
|
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
690 except (ImportError, KeyError, AttributeError): |
|
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
691 raise PicklingError( |
|
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
692 "Can't pickle %r: it's not found as %s.%s" % |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
693 (obj, module, name)) |
|
19344
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
694 else: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
695 if klass is not obj: |
|
19344
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
696 raise PicklingError( |
|
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
697 "Can't pickle %r: it's not the same object as %s.%s" % |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
698 (obj, module, name)) |
|
19344
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
699 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
700 if self.proto >= 2: |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27649
diff
changeset
|
701 code = _extension_registry.get((module, name)) |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
702 if code: |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
703 assert code > 0 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
704 if code <= 0xff: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
705 write(EXT1 + bytes([code])) |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
706 elif code <= 0xffff: |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
707 write(EXT2 + bytes([code&0xff, code>>8])) |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
708 else: |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
709 write(EXT4 + pack("<i", code)) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
710 return |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
711 # Non-ASCII identifiers are supported only with protocols >= 3. |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
712 if self.proto >= 3: |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
713 write(GLOBAL + bytes(module, "utf-8") + b'\n' + |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
714 bytes(name, "utf-8") + b'\n') |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
715 else: |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
716 if self.fix_imports: |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
717 if (module, name) in _compat_pickle.REVERSE_NAME_MAPPING: |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
718 module, name = _compat_pickle.REVERSE_NAME_MAPPING[(module, name)] |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
719 if module in _compat_pickle.REVERSE_IMPORT_MAPPING: |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
720 module = _compat_pickle.REVERSE_IMPORT_MAPPING[module] |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
721 try: |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
722 write(GLOBAL + bytes(module, "ascii") + b'\n' + |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
723 bytes(name, "ascii") + b'\n') |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
724 except UnicodeEncodeError: |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
725 raise PicklingError( |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
726 "can't pickle global identifier '%s.%s' using " |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
727 "pickle protocol %i" % (module, name, self.proto)) |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
728 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
729 self.memoize(obj) |
|
27457
62c66893c2db
save_inst(): Rewrote to have only one branch on self.bin. Also got rid
Tim Peters <tim.peters@gmail.com>
parents:
27456
diff
changeset
|
730 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
731 dispatch[FunctionType] = save_global |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
732 dispatch[BuiltinFunctionType] = save_global |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
733 dispatch[type] = save_global |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
734 |
|
27470
17d87c5ba6e4
Add a few comments. Change the way the protocol is checked (it must
Guido van Rossum <guido@python.org>
parents:
27469
diff
changeset
|
735 # Pickling helpers |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
736 |
|
6216
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
737 def _keep_alive(x, memo): |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
738 """Keeps a reference to the object x in the memo. |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
739 |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
740 Because we remember objects by their id, we have |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
741 to assure that possibly temporary objects are kept |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
742 alive by referencing them. |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
743 We store a reference at the id of the memo, which should |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
744 normally not be used unless someone tries to deepcopy |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
745 the memo itself... |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
746 """ |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
747 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
748 memo[id(memo)].append(x) |
|
6216
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
749 except KeyError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
750 # aha, this is the first one :-) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
751 memo[id(memo)]=[x] |
|
6216
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
752 |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
753 |
|
27506
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
754 # A cache for whichmodule(), mapping a function object to the name of |
|
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
755 # the module in which the function was found. |
|
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
756 |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
757 classmap = {} # called classmap for backwards compatibility |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
758 |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
759 def whichmodule(func, funcname): |
|
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
760 """Figure out the module in which a function occurs. |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
761 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
762 Search sys.modules for the module. |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
763 Cache in classmap. |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
764 Return a module name. |
|
27506
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
765 If the function cannot be found, return "__main__". |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
766 """ |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27554
diff
changeset
|
767 # Python functions should always get an __module__ from their globals. |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27554
diff
changeset
|
768 mod = getattr(func, "__module__", None) |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27554
diff
changeset
|
769 if mod is not None: |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27554
diff
changeset
|
770 return mod |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
771 if func in classmap: |
|
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
772 return classmap[func] |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
773 |
|
40920
0d2dc3611e3b
Fix a bizarre error where test_pickletools fails if preceded by test_pyclbr.
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
774 for name, module in list(sys.modules.items()): |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
775 if module is None: |
|
25331
a302308819f0
whichmodule() should skip dummy package entries in sys.modules.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24808
diff
changeset
|
776 continue # skip dummy package entries |
|
27721
04384f4290ba
No need for a continuation line.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27720
diff
changeset
|
777 if name != '__main__' and getattr(module, funcname, None) is func: |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
778 break |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
779 else: |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
780 name = '__main__' |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
781 classmap[func] = name |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
782 return name |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
783 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
784 |
|
27470
17d87c5ba6e4
Add a few comments. Change the way the protocol is checked (it must
Guido van Rossum <guido@python.org>
parents:
27469
diff
changeset
|
785 # Unpickling machinery |
|
17d87c5ba6e4
Add a few comments. Change the way the protocol is checked (it must
Guido van Rossum <guido@python.org>
parents:
27469
diff
changeset
|
786 |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
787 class _Unpickler: |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
788 |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
789 def __init__(self, file, *, fix_imports=True, |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
790 encoding="ASCII", errors="strict"): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
791 """This takes a binary file for reading a pickle data stream. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
792 |
|
27584
65635c72135e
The C pickle now knows how to deal with a proto= argument. Assorted
Tim Peters <tim.peters@gmail.com>
parents:
27562
diff
changeset
|
793 The protocol version of the pickle is detected automatically, so no |
|
65635c72135e
The C pickle now knows how to deal with a proto= argument. Assorted
Tim Peters <tim.peters@gmail.com>
parents:
27562
diff
changeset
|
794 proto argument is needed. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
795 |
|
43521
4f9ecea8177c
Random changes having to do with readline() and bytes.
Guido van Rossum <guido@python.org>
parents:
42919
diff
changeset
|
796 The file-like object must have two methods, a read() method |
|
4f9ecea8177c
Random changes having to do with readline() and bytes.
Guido van Rossum <guido@python.org>
parents:
42919
diff
changeset
|
797 that takes an integer argument, and a readline() method that |
|
4f9ecea8177c
Random changes having to do with readline() and bytes.
Guido van Rossum <guido@python.org>
parents:
42919
diff
changeset
|
798 requires no arguments. Both methods should return bytes. |
|
4f9ecea8177c
Random changes having to do with readline() and bytes.
Guido van Rossum <guido@python.org>
parents:
42919
diff
changeset
|
799 Thus file-like object can be a binary file object opened for |
|
4f9ecea8177c
Random changes having to do with readline() and bytes.
Guido van Rossum <guido@python.org>
parents:
42919
diff
changeset
|
800 reading, a BytesIO object, or any other custom object that |
|
4f9ecea8177c
Random changes having to do with readline() and bytes.
Guido van Rossum <guido@python.org>
parents:
42919
diff
changeset
|
801 meets this interface. |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
802 |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
803 Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
804 which are used to control compatiblity support for pickle stream |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
805 generated by Python 2.x. If *fix_imports* is True, pickle will try to |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
806 map the old Python 2.x names to the new names used in Python 3.x. The |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
807 *encoding* and *errors* tell pickle how to decode 8-bit string |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
808 instances pickled by Python 2.x; these default to 'ASCII' and |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
809 'strict', respectively. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
810 """ |
|
43521
4f9ecea8177c
Random changes having to do with readline() and bytes.
Guido van Rossum <guido@python.org>
parents:
42919
diff
changeset
|
811 self.readline = file.readline |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
812 self.read = file.read |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
813 self.memo = {} |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
814 self.encoding = encoding |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
815 self.errors = errors |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
816 self.proto = 0 |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
817 self.fix_imports = fix_imports |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
818 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
819 def load(self): |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
820 """Read a pickled object representation from the open file. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
821 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
822 Return the reconstituted object hierarchy specified in the file. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
823 """ |
|
50545
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
824 # Check whether Unpickler was initialized correctly. This is |
|
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
825 # only needed to mimic the behavior of _pickle.Unpickler.dump(). |
|
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
826 if not hasattr(self, "read"): |
|
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
827 raise UnpicklingError("Unpickler.__init__() was not called by " |
|
e80c806ce02b
Merged revisions 67940 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49638
diff
changeset
|
828 "%s.__init__()" % (self.__class__.__name__,)) |
|
20923
98dbb079c5d4
A better new, unique object
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20442
diff
changeset
|
829 self.mark = object() # any new unique object |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
830 self.stack = [] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
831 self.append = self.stack.append |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
832 read = self.read |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
833 dispatch = self.dispatch |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
834 try: |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
835 while 1: |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
836 key = read(1) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
837 if not key: |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
838 raise EOFError |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
839 assert isinstance(key, bytes_types) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
840 dispatch[key[0]](self) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
841 except _Stop as stopinst: |
|
15552
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
842 return stopinst.value |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
843 |
| 27449 | 844 # Return largest index k such that self.stack[k] is self.mark. |
| 845 # If the stack doesn't contain a mark, eventually raises IndexError. | |
| 846 # This could be sped by maintaining another stack, of indices at which | |
| 847 # the mark appears. For that matter, the latter stack would suffice, | |
| 848 # and we wouldn't need to push mark objects on self.stack at all. | |
| 849 # Doing so is probably a good thing, though, since if the pickle is | |
| 850 # corrupt (or hostile) we may get a clue from finding self.mark embedded | |
| 851 # in unpickled objects. | |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
852 def marker(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
853 stack = self.stack |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
854 mark = self.mark |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
855 k = len(stack)-1 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
856 while stack[k] is not mark: k = k-1 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
857 return k |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
858 |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
859 def persistent_load(self, pid): |
| 50702 | 860 raise UnpicklingError("unsupported persistent id encountered") |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
861 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
862 dispatch = {} |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
863 |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
864 def load_proto(self): |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
865 proto = ord(self.read(1)) |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
866 if not 0 <= proto <= HIGHEST_PROTOCOL: |
|
42919
c72a3cc21ef7
More str/bytes fixes.
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
867 raise ValueError("unsupported pickle protocol: %d" % proto) |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
868 self.proto = proto |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
869 dispatch[PROTO[0]] = load_proto |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
870 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
871 def load_persid(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
872 pid = self.readline()[:-1].decode("ascii") |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
873 self.append(self.persistent_load(pid)) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
874 dispatch[PERSID[0]] = load_persid |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
875 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
876 def load_binpersid(self): |
|
24058
37ffce2d9c3c
Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i)
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
877 pid = self.stack.pop() |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
878 self.append(self.persistent_load(pid)) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
879 dispatch[BINPERSID[0]] = load_binpersid |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
880 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
881 def load_none(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
882 self.append(None) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
883 dispatch[NONE[0]] = load_none |
|
2475
30205d634a15
raise EOFError when load() hits EOF, instead of KeyError
Guido van Rossum <guido@python.org>
parents:
2356
diff
changeset
|
884 |
|
27461
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
885 def load_false(self): |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
886 self.append(False) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
887 dispatch[NEWFALSE[0]] = load_false |
|
27461
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
888 |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
889 def load_true(self): |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
890 self.append(True) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
891 dispatch[NEWTRUE[0]] = load_true |
|
27461
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
892 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
893 def load_int(self): |
|
19544
d711e82ece23
pickle.py, load_int(): Match cPickle's just-repaired ability to unpickle
Tim Peters <tim.peters@gmail.com>
parents:
19344
diff
changeset
|
894 data = self.readline() |
|
22851
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
895 if data == FALSE[1:]: |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
896 val = False |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
897 elif data == TRUE[1:]: |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
898 val = True |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
899 else: |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
900 try: |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
901 val = int(data, 0) |
|
22851
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
902 except ValueError: |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
903 val = int(data, 0) |
|
22851
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
904 self.append(val) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
905 dispatch[INT[0]] = load_int |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
906 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
907 def load_binint(self): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
908 self.append(mloads(b'i' + self.read(4))) |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
909 dispatch[BININT[0]] = load_binint |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
910 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
911 def load_binint1(self): |
|
27426
e26a2f7f224b
Using marshal functions to pack & unpack 1-byte ints is an obscure &
Tim Peters <tim.peters@gmail.com>
parents:
27425
diff
changeset
|
912 self.append(ord(self.read(1))) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
913 dispatch[BININT1[0]] = load_binint1 |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
914 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
915 def load_binint2(self): |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
916 self.append(mloads(b'i' + self.read(2) + b'\000\000')) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
917 dispatch[BININT2[0]] = load_binint2 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15605
diff
changeset
|
918 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
919 def load_long(self): |
|
43521
4f9ecea8177c
Random changes having to do with readline() and bytes.
Guido van Rossum <guido@python.org>
parents:
42919
diff
changeset
|
920 val = self.readline()[:-1].decode("ascii") |
|
50944
d1de077447aa
Issue #4842, patch 1/2: fix pickle in Python 3.x so that pickling with the
Mark Dickinson <dickinsm@gmail.com>
parents:
50702
diff
changeset
|
921 if val and val[-1] == 'L': |
|
d1de077447aa
Issue #4842, patch 1/2: fix pickle in Python 3.x so that pickling with the
Mark Dickinson <dickinsm@gmail.com>
parents:
50702
diff
changeset
|
922 val = val[:-1] |
|
43521
4f9ecea8177c
Random changes having to do with readline() and bytes.
Guido van Rossum <guido@python.org>
parents:
42919
diff
changeset
|
923 self.append(int(val, 0)) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
924 dispatch[LONG[0]] = load_long |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
925 |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
926 def load_long1(self): |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
927 n = ord(self.read(1)) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
928 data = self.read(n) |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
929 self.append(decode_long(data)) |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
930 dispatch[LONG1[0]] = load_long1 |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
931 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
932 def load_long4(self): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
933 n = mloads(b'i' + self.read(4)) |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
934 data = self.read(n) |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
935 self.append(decode_long(data)) |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
936 dispatch[LONG4[0]] = load_long4 |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
937 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
938 def load_float(self): |
|
15552
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
939 self.append(float(self.readline()[:-1])) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
940 dispatch[FLOAT[0]] = load_float |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
941 |
| 9439 | 942 def load_binfloat(self, unpack=struct.unpack): |
| 943 self.append(unpack('>d', self.read(8))[0]) | |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
944 dispatch[BINFLOAT[0]] = load_binfloat |
| 9439 | 945 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
946 def load_string(self): |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
947 orig = self.readline() |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
948 rep = orig[:-1] |
|
42919
c72a3cc21ef7
More str/bytes fixes.
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
949 for q in (b'"', b"'"): # double or single quote |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24252
diff
changeset
|
950 if rep.startswith(q): |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24252
diff
changeset
|
951 if not rep.endswith(q): |
|
42919
c72a3cc21ef7
More str/bytes fixes.
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
952 raise ValueError("insecure string pickle") |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24252
diff
changeset
|
953 rep = rep[len(q):-len(q)] |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24252
diff
changeset
|
954 break |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24252
diff
changeset
|
955 else: |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
956 raise ValueError("insecure string pickle: %r" % orig) |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
957 self.append(codecs.escape_decode(rep)[0] |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
958 .decode(self.encoding, self.errors)) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
959 dispatch[STRING[0]] = load_string |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
960 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
961 def load_binstring(self): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
962 len = mloads(b'i' + self.read(4)) |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
963 data = self.read(len) |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
964 value = str(data, self.encoding, self.errors) |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
965 self.append(value) |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
966 dispatch[BINSTRING[0]] = load_binstring |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
967 |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
968 def load_binbytes(self): |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
969 len = mloads(b'i' + self.read(4)) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
970 self.append(self.read(len)) |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
971 dispatch[BINBYTES[0]] = load_binbytes |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
972 |
|
11989
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
973 def load_unicode(self): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
974 self.append(str(self.readline()[:-1], 'raw-unicode-escape')) |
|
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
975 dispatch[UNICODE[0]] = load_unicode |
|
11989
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
976 |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
977 def load_binunicode(self): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
978 len = mloads(b'i' + self.read(4)) |
|
60578
b8f297cdfade
Merged revisions 80031 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
54584
diff
changeset
|
979 self.append(str(self.read(len), 'utf-8', 'surrogatepass')) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
980 dispatch[BINUNICODE[0]] = load_binunicode |
|
11989
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
981 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
982 def load_short_binstring(self): |
|
27426
e26a2f7f224b
Using marshal functions to pack & unpack 1-byte ints is an obscure &
Tim Peters <tim.peters@gmail.com>
parents:
27425
diff
changeset
|
983 len = ord(self.read(1)) |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
984 data = bytes(self.read(len)) |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
985 value = str(data, self.encoding, self.errors) |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
986 self.append(value) |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
987 dispatch[SHORT_BINSTRING[0]] = load_short_binstring |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
988 |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
989 def load_short_binbytes(self): |
|
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
990 len = ord(self.read(1)) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
991 self.append(bytes(self.read(len))) |
|
45812
322693bf10ca
- A new pickle protocol (protocol 3) is added with explicit support
Guido van Rossum <guido@python.org>
parents:
43947
diff
changeset
|
992 dispatch[SHORT_BINBYTES[0]] = load_short_binbytes |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
993 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
994 def load_tuple(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
995 k = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
996 self.stack[k:] = [tuple(self.stack[k+1:])] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
997 dispatch[TUPLE[0]] = load_tuple |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
998 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
999 def load_empty_tuple(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1000 self.append(()) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1001 dispatch[EMPTY_TUPLE[0]] = load_empty_tuple |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1002 |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1003 def load_tuple1(self): |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1004 self.stack[-1] = (self.stack[-1],) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1005 dispatch[TUPLE1[0]] = load_tuple1 |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1006 |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1007 def load_tuple2(self): |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1008 self.stack[-2:] = [(self.stack[-2], self.stack[-1])] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1009 dispatch[TUPLE2[0]] = load_tuple2 |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1010 |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1011 def load_tuple3(self): |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1012 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1013 dispatch[TUPLE3[0]] = load_tuple3 |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1014 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1015 def load_empty_list(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1016 self.append([]) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1017 dispatch[EMPTY_LIST[0]] = load_empty_list |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1018 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1019 def load_empty_dictionary(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1020 self.append({}) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1021 dispatch[EMPTY_DICT[0]] = load_empty_dictionary |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1022 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1023 def load_list(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1024 k = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1025 self.stack[k:] = [self.stack[k+1:]] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1026 dispatch[LIST[0]] = load_list |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1027 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1028 def load_dict(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1029 k = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1030 d = {} |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1031 items = self.stack[k+1:] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1032 for i in range(0, len(items), 2): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1033 key = items[i] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1034 value = items[i+1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1035 d[key] = value |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1036 self.stack[k:] = [d] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1037 dispatch[DICT[0]] = load_dict |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1038 |
|
27534
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1039 # INST and OBJ differ only in how they get a class object. It's not |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1040 # only sensible to do the rest in a common routine, the two routines |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1041 # previously diverged and grew different bugs. |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1042 # klass is the class to instantiate, and k points to the topmost mark |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1043 # object, following which are the arguments for klass.__init__. |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1044 def _instantiate(self, klass, k): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1045 args = tuple(self.stack[k+1:]) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1046 del self.stack[k:] |
|
63062
abf69d402883
Merged revisions 82937 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
60578
diff
changeset
|
1047 if (args or not isinstance(klass, type) or |
|
abf69d402883
Merged revisions 82937 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
60578
diff
changeset
|
1048 hasattr(klass, "__getinitargs__")): |
|
9114
d237c808a01e
In load_inst(), when instantiating an instance the old way (i.e. when
Guido van Rossum <guido@python.org>
parents:
8431
diff
changeset
|
1049 try: |
|
27498
d8750e0d3fb8
Get rid of __safe_for_unpickling__ and safe_constructors.
Guido van Rossum <guido@python.org>
parents:
27493
diff
changeset
|
1050 value = klass(*args) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
1051 except TypeError as err: |
|
42919
c72a3cc21ef7
More str/bytes fixes.
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
1052 raise TypeError("in constructor for %s: %s" % |
|
c72a3cc21ef7
More str/bytes fixes.
Guido van Rossum <guido@python.org>
parents:
42902
diff
changeset
|
1053 (klass.__name__, str(err)), sys.exc_info()[2]) |
|
63062
abf69d402883
Merged revisions 82937 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
60578
diff
changeset
|
1054 else: |
|
abf69d402883
Merged revisions 82937 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
60578
diff
changeset
|
1055 value = klass.__new__(klass) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1056 self.append(value) |
|
27534
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1057 |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1058 def load_inst(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1059 module = self.readline()[:-1].decode("ascii") |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1060 name = self.readline()[:-1].decode("ascii") |
|
27534
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1061 klass = self.find_class(module, name) |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1062 self._instantiate(klass, self.marker()) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1063 dispatch[INST[0]] = load_inst |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1064 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1065 def load_obj(self): |
|
27534
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1066 # Stack is ... markobject classobject arg1 arg2 ... |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1067 k = self.marker() |
|
27534
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1068 klass = self.stack.pop(k+1) |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1069 self._instantiate(klass, k) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1070 dispatch[OBJ[0]] = load_obj |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1071 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
1072 def load_newobj(self): |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
1073 args = self.stack.pop() |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
1074 cls = self.stack[-1] |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
1075 obj = cls.__new__(cls, *args) |
|
27482
7c8dfd3f4ae9
Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents:
27478
diff
changeset
|
1076 self.stack[-1] = obj |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1077 dispatch[NEWOBJ[0]] = load_newobj |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
1078 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1079 def load_global(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1080 module = self.readline()[:-1].decode("utf-8") |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1081 name = self.readline()[:-1].decode("utf-8") |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1082 klass = self.find_class(module, name) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1083 self.append(klass) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1084 dispatch[GLOBAL[0]] = load_global |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1085 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1086 def load_ext1(self): |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1087 code = ord(self.read(1)) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1088 self.get_extension(code) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1089 dispatch[EXT1[0]] = load_ext1 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1090 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1091 def load_ext2(self): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1092 code = mloads(b'i' + self.read(2) + b'\000\000') |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1093 self.get_extension(code) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1094 dispatch[EXT2[0]] = load_ext2 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1095 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1096 def load_ext4(self): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1097 code = mloads(b'i' + self.read(4)) |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1098 self.get_extension(code) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1099 dispatch[EXT4[0]] = load_ext4 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1100 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1101 def get_extension(self, code): |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1102 nil = [] |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27649
diff
changeset
|
1103 obj = _extension_cache.get(code, nil) |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1104 if obj is not nil: |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1105 self.append(obj) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1106 return |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27649
diff
changeset
|
1107 key = _inverted_registry.get(code) |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1108 if not key: |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1109 raise ValueError("unregistered extension code %d" % code) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1110 obj = self.find_class(*key) |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27649
diff
changeset
|
1111 _extension_cache[code] = obj |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1112 self.append(obj) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1113 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1114 def find_class(self, module, name): |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1115 # Subclasses may override this. |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1116 if self.proto < 3 and self.fix_imports: |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1117 if (module, name) in _compat_pickle.NAME_MAPPING: |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1118 module, name = _compat_pickle.NAME_MAPPING[(module, name)] |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1119 if module in _compat_pickle.IMPORT_MAPPING: |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1120 module = _compat_pickle.IMPORT_MAPPING[module] |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1121 __import__(module, level=0) |
| 20991 | 1122 mod = sys.modules[module] |
| 1123 klass = getattr(mod, name) | |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1124 return klass |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1125 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1126 def load_reduce(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1127 stack = self.stack |
|
27498
d8750e0d3fb8
Get rid of __safe_for_unpickling__ and safe_constructors.
Guido van Rossum <guido@python.org>
parents:
27493
diff
changeset
|
1128 args = stack.pop() |
|
d8750e0d3fb8
Get rid of __safe_for_unpickling__ and safe_constructors.
Guido van Rossum <guido@python.org>
parents:
27493
diff
changeset
|
1129 func = stack[-1] |
|
42322
3e33215c66ed
Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents:
42319
diff
changeset
|
1130 try: |
|
3e33215c66ed
Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents:
42319
diff
changeset
|
1131 value = func(*args) |
|
3e33215c66ed
Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents:
42319
diff
changeset
|
1132 except: |
|
3e33215c66ed
Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents:
42319
diff
changeset
|
1133 print(sys.exc_info()) |
|
3e33215c66ed
Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents:
42319
diff
changeset
|
1134 print(func, args) |
|
3e33215c66ed
Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents:
42319
diff
changeset
|
1135 raise |
|
27498
d8750e0d3fb8
Get rid of __safe_for_unpickling__ and safe_constructors.
Guido van Rossum <guido@python.org>
parents:
27493
diff
changeset
|
1136 stack[-1] = value |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1137 dispatch[REDUCE[0]] = load_reduce |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1138 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1139 def load_pop(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1140 del self.stack[-1] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1141 dispatch[POP[0]] = load_pop |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1142 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1143 def load_pop_mark(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1144 k = self.marker() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1145 del self.stack[k:] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1146 dispatch[POP_MARK[0]] = load_pop_mark |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1147 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1148 def load_dup(self): |
|
7775
57f11ab4e292
Correct dumb typo found by kjpylint (stack should be self.stack).
Guido van Rossum <guido@python.org>
parents:
7774
diff
changeset
|
1149 self.append(self.stack[-1]) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1150 dispatch[DUP[0]] = load_dup |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1151 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1152 def load_get(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1153 i = int(self.readline()[:-1]) |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1154 self.append(self.memo[i]) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1155 dispatch[GET[0]] = load_get |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1156 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1157 def load_binget(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1158 i = self.read(1)[0] |
|
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1159 self.append(self.memo[i]) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1160 dispatch[BINGET[0]] = load_binget |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1161 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1162 def load_long_binget(self): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1163 i = mloads(b'i' + self.read(4)) |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1164 self.append(self.memo[i]) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1165 dispatch[LONG_BINGET[0]] = load_long_binget |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1166 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1167 def load_put(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1168 i = int(self.readline()[:-1]) |
|
72121
0d9e4ce1c010
Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
1169 if i < 0: |
|
0d9e4ce1c010
Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
1170 raise ValueError("negative PUT argument") |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1171 self.memo[i] = self.stack[-1] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1172 dispatch[PUT[0]] = load_put |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1173 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1174 def load_binput(self): |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1175 i = self.read(1)[0] |
|
72121
0d9e4ce1c010
Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
1176 if i < 0: |
|
0d9e4ce1c010
Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
1177 raise ValueError("negative BINPUT argument") |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1178 self.memo[i] = self.stack[-1] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1179 dispatch[BINPUT[0]] = load_binput |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1180 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1181 def load_long_binput(self): |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1182 i = mloads(b'i' + self.read(4)) |
|
72121
0d9e4ce1c010
Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
1183 if i < 0: |
|
0d9e4ce1c010
Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
1184 raise ValueError("negative LONG_BINPUT argument") |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1185 self.memo[i] = self.stack[-1] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1186 dispatch[LONG_BINPUT[0]] = load_long_binput |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1187 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1188 def load_append(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1189 stack = self.stack |
|
24058
37ffce2d9c3c
Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i)
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
1190 value = stack.pop() |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1191 list = stack[-1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1192 list.append(value) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1193 dispatch[APPEND[0]] = load_append |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1194 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1195 def load_appends(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1196 stack = self.stack |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1197 mark = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1198 list = stack[mark - 1] |
|
27450
4015a27a1d13
load_appends(): replaced .append() loop with an .extend().
Tim Peters <tim.peters@gmail.com>
parents:
27449
diff
changeset
|
1199 list.extend(stack[mark + 1:]) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1200 del stack[mark:] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1201 dispatch[APPENDS[0]] = load_appends |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15605
diff
changeset
|
1202 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1203 def load_setitem(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1204 stack = self.stack |
|
24058
37ffce2d9c3c
Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i)
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
1205 value = stack.pop() |
|
37ffce2d9c3c
Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i)
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
1206 key = stack.pop() |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1207 dict = stack[-1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1208 dict[key] = value |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1209 dispatch[SETITEM[0]] = load_setitem |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1210 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1211 def load_setitems(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1212 stack = self.stack |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1213 mark = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1214 dict = stack[mark - 1] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1215 for i in range(mark + 1, len(stack), 2): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1216 dict[stack[i]] = stack[i + 1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1217 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1218 del stack[mark:] |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1219 dispatch[SETITEMS[0]] = load_setitems |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1220 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1221 def load_build(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1222 stack = self.stack |
|
27493
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1223 state = stack.pop() |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1224 inst = stack[-1] |
|
27493
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1225 setstate = getattr(inst, "__setstate__", None) |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1226 if setstate: |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1227 setstate(state) |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1228 return |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1229 slotstate = None |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1230 if isinstance(state, tuple) and len(state) == 2: |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1231 state, slotstate = state |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1232 if state: |
|
54332
5aea603b2308
Only try to intern str objects when unpickling attributes.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
53694
diff
changeset
|
1233 inst_dict = inst.__dict__ |
|
53694
64d9a9e7f68e
Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
50944
diff
changeset
|
1234 intern = sys.intern |
|
54332
5aea603b2308
Only try to intern str objects when unpickling attributes.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
53694
diff
changeset
|
1235 for k, v in state.items(): |
|
5aea603b2308
Only try to intern str objects when unpickling attributes.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
53694
diff
changeset
|
1236 if type(k) is str: |
|
5aea603b2308
Only try to intern str objects when unpickling attributes.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
53694
diff
changeset
|
1237 inst_dict[intern(k)] = v |
|
5aea603b2308
Only try to intern str objects when unpickling attributes.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
53694
diff
changeset
|
1238 else: |
|
5aea603b2308
Only try to intern str objects when unpickling attributes.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
53694
diff
changeset
|
1239 inst_dict[k] = v |
|
27493
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1240 if slotstate: |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1241 for k, v in slotstate.items(): |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1242 setattr(inst, k, v) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1243 dispatch[BUILD[0]] = load_build |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1244 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1245 def load_mark(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1246 self.append(self.mark) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1247 dispatch[MARK[0]] = load_mark |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1248 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1249 def load_stop(self): |
|
24058
37ffce2d9c3c
Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i)
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
1250 value = self.stack.pop() |
|
15552
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
1251 raise _Stop(value) |
|
41613
52b99a915faa
Checkpoint. Make pickle.py read/write bytes.
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
1252 dispatch[STOP[0]] = load_stop |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1253 |
|
63061
3c78b3cfd462
Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
60577
diff
changeset
|
1254 # Encode/decode longs. |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1255 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1256 def encode_long(x): |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1257 r"""Encode a long to a two's complement little-endian binary string. |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40626
diff
changeset
|
1258 Note that 0 is a special case, returning an empty string, to save a |
|
27552
50c734250e05
It's Official: for LONG1/LONG4, a "byte count" of 0 is taken as a
Tim Peters <tim.peters@gmail.com>
parents:
27544
diff
changeset
|
1259 byte in the LONG1 pickling context. |
|
50c734250e05
It's Official: for LONG1/LONG4, a "byte count" of 0 is taken as a
Tim Peters <tim.peters@gmail.com>
parents:
27544
diff
changeset
|
1260 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40626
diff
changeset
|
1261 >>> encode_long(0) |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1262 b'' |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40626
diff
changeset
|
1263 >>> encode_long(255) |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1264 b'\xff\x00' |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40626
diff
changeset
|
1265 >>> encode_long(32767) |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1266 b'\xff\x7f' |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40626
diff
changeset
|
1267 >>> encode_long(-256) |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1268 b'\x00\xff' |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40626
diff
changeset
|
1269 >>> encode_long(-32768) |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1270 b'\x00\x80' |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40626
diff
changeset
|
1271 >>> encode_long(-128) |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1272 b'\x80' |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40626
diff
changeset
|
1273 >>> encode_long(127) |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1274 b'\x7f' |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1275 >>> |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1276 """ |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1277 if x == 0: |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1278 return b'' |
|
58267
9f90db2760d9
Simplified long coding in pickle.py.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54584
diff
changeset
|
1279 nbytes = (x.bit_length() >> 3) + 1 |
|
9f90db2760d9
Simplified long coding in pickle.py.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54584
diff
changeset
|
1280 result = x.to_bytes(nbytes, byteorder='little', signed=True) |
|
9f90db2760d9
Simplified long coding in pickle.py.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54584
diff
changeset
|
1281 if x < 0 and nbytes > 1: |
|
9f90db2760d9
Simplified long coding in pickle.py.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54584
diff
changeset
|
1282 if result[-1] == 0xff and (result[-2] & 0x80) != 0: |
|
9f90db2760d9
Simplified long coding in pickle.py.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54584
diff
changeset
|
1283 result = result[:-1] |
|
9f90db2760d9
Simplified long coding in pickle.py.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54584
diff
changeset
|
1284 return result |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1285 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1286 def decode_long(data): |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1287 r"""Decode a long from a two's complement little-endian binary string. |
|
27552
50c734250e05
It's Official: for LONG1/LONG4, a "byte count" of 0 is taken as a
Tim Peters <tim.peters@gmail.com>
parents:
27544
diff
changeset
|
1288 |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1289 >>> decode_long(b'') |
|
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
|
1290 0 |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1291 >>> decode_long(b"\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
|
1292 255 |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1293 >>> decode_long(b"\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
|
1294 32767 |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1295 >>> decode_long(b"\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
|
1296 -256 |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1297 >>> decode_long(b"\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
|
1298 -32768 |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1299 >>> decode_long(b"\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
|
1300 -128 |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1301 >>> decode_long(b"\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
|
1302 127 |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1303 """ |
|
58267
9f90db2760d9
Simplified long coding in pickle.py.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54584
diff
changeset
|
1304 return int.from_bytes(data, byteorder='little', signed=True) |
|
47645
0ae50aa7d97c
Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47634
diff
changeset
|
1305 |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1306 # Shorthands |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1307 |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1308 def dump(obj, file, protocol=None, *, fix_imports=True): |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1309 Pickler(file, protocol, fix_imports=fix_imports).dump(obj) |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1310 |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1311 def dumps(obj, protocol=None, *, fix_imports=True): |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1312 f = io.BytesIO() |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1313 Pickler(f, protocol, fix_imports=fix_imports).dump(obj) |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1314 res = f.getvalue() |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
1315 assert isinstance(res, bytes_types) |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1316 return res |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1317 |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1318 def load(file, *, fix_imports=True, encoding="ASCII", errors="strict"): |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1319 return Unpickler(file, fix_imports=fix_imports, |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1320 encoding=encoding, errors=errors).load() |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1321 |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1322 def loads(s, *, fix_imports=True, encoding="ASCII", errors="strict"): |
|
41651
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1323 if isinstance(s, str): |
|
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1324 raise TypeError("Can't load pickle from unicode string") |
|
44ebae4f61c4
Got test_pickletools and test_pickle working.
Guido van Rossum <guido@python.org>
parents:
41646
diff
changeset
|
1325 file = io.BytesIO(s) |
|
54584
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1326 return Unpickler(file, fix_imports=fix_imports, |
|
0805ea211122
Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents:
54332
diff
changeset
|
1327 encoding=encoding, errors=errors).load() |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1328 |
|
64588
027f81579b4a
Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents:
63250
diff
changeset
|
1329 # Use the faster _pickle if possible |
|
027f81579b4a
Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents:
63250
diff
changeset
|
1330 try: |
|
027f81579b4a
Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents:
63250
diff
changeset
|
1331 from _pickle import * |
|
027f81579b4a
Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents:
63250
diff
changeset
|
1332 except ImportError: |
|
027f81579b4a
Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents:
63250
diff
changeset
|
1333 Pickler, Unpickler = _Pickler, _Unpickler |
|
027f81579b4a
Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents:
63250
diff
changeset
|
1334 |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1335 # Doctest |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1336 def _test(): |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1337 import doctest |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1338 return doctest.testmod() |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1339 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1340 if __name__ == "__main__": |
|
73345
fa82071bb7e1
Remove redundant imports.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
73166
diff
changeset
|
1341 import argparse |
|
63250
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1342 parser = argparse.ArgumentParser( |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1343 description='display contents of the pickle files') |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1344 parser.add_argument( |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1345 'pickle_file', type=argparse.FileType('br'), |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1346 nargs='*', help='the pickle file') |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1347 parser.add_argument( |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1348 '-t', '--test', action='store_true', |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1349 help='run self-test suite') |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1350 parser.add_argument( |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1351 '-v', action='store_true', |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1352 help='run verbosely; only affects self-test run') |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1353 args = parser.parse_args() |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1354 if args.test: |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1355 _test() |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1356 else: |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1357 if not args.pickle_file: |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1358 parser.print_help() |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1359 else: |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1360 import pprint |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1361 for f in args.pickle_file: |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1362 obj = load(f) |
|
685d3918f2a5
Issue #9378: python -m pickle <pickle file> will now load and display
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63061
diff
changeset
|
1363 pprint.pprint(obj) |
