Mercurial > cpython-lru_cache-weakref
annotate Lib/pickle.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 64053bd79590 |
| children | 527b7f88b53c |
| 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 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
3 See module cPickle for a (much) faster implementation. |
|
47189
f419b2e96a98
Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents:
47108
diff
changeset
|
4 See module copy_reg 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
|
5 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
|
6 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
7 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
|
8 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
9 Pickler |
|
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
10 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
|
11 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
12 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
|
13 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
14 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
|
15 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
|
16 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
|
17 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
|
18 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
19 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
|
20 |
|
7274
f5ea1b30cb3f
Fixed typo in docstring: "__ version__" --> "__version__"
Fred Drake <fdrake@acm.org>
parents:
7166
diff
changeset
|
21 __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
|
22 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
|
23 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
|
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 """ |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
71539
f15442543e24
Fix closes Issue11439 - Handle the SVN Keywords in 2.7 by replacing them with a high number so that code relying on them does not break.
Senthil Kumaran <senthil@uthcode.com>
parents:
53693
diff
changeset
|
27 __version__ = "$Revision: 72223 $" # Code version |
|
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
|
28 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 from types import * |
|
47189
f419b2e96a98
Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents:
47108
diff
changeset
|
30 from copy_reg import dispatch_table |
|
f419b2e96a98
Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents:
47108
diff
changeset
|
31 from copy_reg import _extension_registry, _inverted_registry, _extension_cache |
| 9439 | 32 import marshal |
| 33 import sys | |
| 34 import struct | |
|
16655
1d7aed076964
add module-level constants to __all__
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
35 import re |
|
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 | |
|
27506
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
40 # These are purely informational; no code uses these. |
|
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
|
41 format_version = "2.0" # File format version we write |
|
f8a90085acc4
Begin the change from 'binary vs. text mode' to 'protocol 0, 1, 2'.
Guido van Rossum <guido@python.org>
parents:
27429
diff
changeset
|
42 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
|
43 "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
|
44 "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
|
45 "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
|
46 "2.0", # Protocol 2 |
|
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 ] # 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
|
48 |
|
27864
b4eb34372bdb
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents:
27825
diff
changeset
|
49 # Keep in synch with cPickle. This is the highest protocol number we |
|
b4eb34372bdb
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents:
27825
diff
changeset
|
50 # know how to read. |
|
b4eb34372bdb
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents:
27825
diff
changeset
|
51 HIGHEST_PROTOCOL = 2 |
|
b4eb34372bdb
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents:
27825
diff
changeset
|
52 |
|
27453
3509ad07d156
Add a comment explaining that struct.pack() beats marshal.dumps(), but
Guido van Rossum <guido@python.org>
parents:
27452
diff
changeset
|
53 # 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
|
54 # 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
|
55 # 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
|
56 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
|
57 |
|
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
|
58 class PickleError(Exception): |
|
23579
4e3ea55f7e11
Remove comment about inheritance, look one line up
Neal Norwitz <nnorwitz@gmail.com>
parents:
23565
diff
changeset
|
59 """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
|
60 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
|
61 |
|
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
|
62 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
|
63 """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
|
64 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
|
65 |
|
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 """ |
|
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
|
67 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
|
68 |
|
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 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
|
70 """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
|
71 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
|
72 |
|
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 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
|
74 (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
|
75 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
|
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 """ |
|
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 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
|
79 |
|
27506
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
80 # 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
|
81 # 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
|
82 class _Stop(Exception): |
|
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
83 def __init__(self, value): |
|
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
84 self.value = value |
|
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
85 |
|
27482
7c8dfd3f4ae9
Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents:
27478
diff
changeset
|
86 # 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
|
87 try: |
|
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
88 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
|
89 except ImportError: |
|
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
90 PyStringMap = None |
|
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
91 |
|
27482
7c8dfd3f4ae9
Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents:
27478
diff
changeset
|
92 # UnicodeType may or may not be exported (normally imported from types) |
|
20020
b70c08813ba9
Make these modules work when Python is compiled without Unicode support.
Guido van Rossum <guido@python.org>
parents:
19544
diff
changeset
|
93 try: |
|
b70c08813ba9
Make these modules work when Python is compiled without Unicode support.
Guido van Rossum <guido@python.org>
parents:
19544
diff
changeset
|
94 UnicodeType |
|
b70c08813ba9
Make these modules work when Python is compiled without Unicode support.
Guido van Rossum <guido@python.org>
parents:
19544
diff
changeset
|
95 except NameError: |
|
b70c08813ba9
Make these modules work when Python is compiled without Unicode support.
Guido van Rossum <guido@python.org>
parents:
19544
diff
changeset
|
96 UnicodeType = None |
|
b70c08813ba9
Make these modules work when Python is compiled without Unicode support.
Guido van Rossum <guido@python.org>
parents:
19544
diff
changeset
|
97 |
|
27425
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
98 # 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
|
99 # 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
|
100 # 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
|
101 |
|
27425
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
102 MARK = '(' # push special markobject on stack |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
103 STOP = '.' # every pickle ends with STOP |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
104 POP = '0' # discard topmost stack item |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
105 POP_MARK = '1' # discard stack top through topmost markobject |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
106 DUP = '2' # duplicate top stack item |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
107 FLOAT = 'F' # push float object; decimal string argument |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
108 INT = 'I' # push integer or bool; decimal string argument |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
109 BININT = 'J' # push four-byte signed int |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
110 BININT1 = 'K' # push 1-byte unsigned int |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
111 LONG = 'L' # push long; decimal string argument |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
112 BININT2 = 'M' # push 2-byte unsigned int |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
113 NONE = 'N' # push None |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
114 PERSID = 'P' # push persistent object; id is taken from string arg |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
115 BINPERSID = 'Q' # " " " ; " " " " stack |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
116 REDUCE = 'R' # apply callable to argtuple, both on stack |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
117 STRING = 'S' # push string; NL-terminated string argument |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
118 BINSTRING = 'T' # push string; counted binary string argument |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
119 SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
120 UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
121 BINUNICODE = 'X' # " " " ; counted UTF-8 string argument |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
122 APPEND = 'a' # append stack top to list below it |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
123 BUILD = 'b' # call __setstate__ or __dict__.update() |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
124 GLOBAL = 'c' # push self.find_class(modname, name); 2 string args |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
125 DICT = 'd' # build a dict from stack items |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
126 EMPTY_DICT = '}' # push empty dict |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
127 APPENDS = 'e' # extend list on stack by topmost stack slice |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
128 GET = 'g' # push item from memo on stack; index is string arg |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
129 BINGET = 'h' # " " " " " " ; " " 1-byte arg |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
130 INST = 'i' # build & push class instance |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
131 LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
132 LIST = 'l' # build list from topmost stack items |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
133 EMPTY_LIST = ']' # push empty list |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
134 OBJ = 'o' # build & push class instance |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
135 PUT = 'p' # store stack top in memo; index is string arg |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
136 BINPUT = 'q' # " " " " " ; " " 1-byte arg |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
137 LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
138 SETITEM = 's' # add key+value pair to dict |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
139 TUPLE = 't' # build tuple from topmost stack items |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
140 EMPTY_TUPLE = ')' # push empty tuple |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
141 SETITEMS = 'u' # modify dict by adding topmost key+value pairs |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
142 BINFLOAT = 'G' # push float; arg is 8-byte float encoding |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
143 |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
144 TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py |
|
fa24538f8650
Added a brief comment to each pickle opcode declaration.
Tim Peters <tim.peters@gmail.com>
parents:
27362
diff
changeset
|
145 FALSE = '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
|
146 |
|
27512
a2d96e7d91a1
Declare Protocol 2 as implemented.
Guido van Rossum <guido@python.org>
parents:
27511
diff
changeset
|
147 # Protocol 2 |
|
27429
b465c9d2196a
Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents:
27428
diff
changeset
|
148 |
|
27437
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
149 PROTO = '\x80' # identify pickle protocol |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
150 NEWOBJ = '\x81' # build object by applying cls.__new__ to argtuple |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
151 EXT1 = '\x82' # push object from extension registry; 1-byte index |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
152 EXT2 = '\x83' # ditto, but 2-byte index |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
153 EXT4 = '\x84' # ditto, but 4-byte index |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
154 TUPLE1 = '\x85' # build 1-tuple from stack top |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
155 TUPLE2 = '\x86' # build 2-tuple from two topmost stack items |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
156 TUPLE3 = '\x87' # build 3-tuple from three topmost stack items |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
157 NEWTRUE = '\x88' # push True |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
158 NEWFALSE = '\x89' # push False |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
159 LONG1 = '\x8a' # push long from < 256 bytes |
|
91ae292c3a97
Added one-line comments to the proto 2 opcodes.
Tim Peters <tim.peters@gmail.com>
parents:
27435
diff
changeset
|
160 LONG4 = '\x8b' # push really big long |
|
27429
b465c9d2196a
Begin documenting protocol 2.
Guido van Rossum <guido@python.org>
parents:
27428
diff
changeset
|
161 |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
162 _tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3] |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
163 |
|
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
|
164 |
|
16655
1d7aed076964
add module-level constants to __all__
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
165 __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)]) |
|
21923
efb6e617aea3
SF #515018, delete global variable that was apparently used only
Neal Norwitz <nnorwitz@gmail.com>
parents:
21485
diff
changeset
|
166 del x |
|
16655
1d7aed076964
add module-level constants to __all__
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
167 |
|
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
|
168 |
|
17d87c5ba6e4
Add a few comments. Change the way the protocol is checked (it must
Guido van Rossum <guido@python.org>
parents:
27469
diff
changeset
|
169 # 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
|
170 |
|
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
|
171 class Pickler: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 |
|
34102
cddd016ae06a
Removed the deprecated bin parameter from the pickle module.
Raymond Hettinger <python@rcn.com>
parents:
33000
diff
changeset
|
173 def __init__(self, file, protocol=None): |
|
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
|
174 """This takes a file-like object for writing a pickle data stream. |
|
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
|
175 |
|
27771
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
176 The optional protocol argument tells the pickler to use the |
|
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
177 given protocol; supported protocols are 0, 1, 2. The default |
|
27562
d42d83e5686c
Change the default protocol back to 0.
Guido van Rossum <guido@python.org>
parents:
27561
diff
changeset
|
178 protocol is 0, to be backwards compatible. (Protocol 0 is the |
|
d42d83e5686c
Change the default protocol back to 0.
Guido van Rossum <guido@python.org>
parents:
27561
diff
changeset
|
179 only protocol that can be written to a file opened in text |
|
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
|
180 mode and read back successfully. When using a protocol higher |
|
65635c72135e
The C pickle now knows how to deal with a proto= argument. Assorted
Tim Peters <tim.peters@gmail.com>
parents:
27562
diff
changeset
|
181 than 0, make sure the file is opened in binary mode, both when |
|
65635c72135e
The C pickle now knows how to deal with a proto= argument. Assorted
Tim Peters <tim.peters@gmail.com>
parents:
27562
diff
changeset
|
182 pickling and unpickling.) |
|
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
|
183 |
|
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 Protocol 1 is more efficient than protocol 0; protocol 2 is |
|
27562
d42d83e5686c
Change the default protocol back to 0.
Guido van Rossum <guido@python.org>
parents:
27561
diff
changeset
|
185 more efficient than protocol 1. |
|
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
|
186 |
|
27562
d42d83e5686c
Change the default protocol back to 0.
Guido van Rossum <guido@python.org>
parents:
27561
diff
changeset
|
187 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
|
188 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
|
189 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
|
190 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
|
191 |
|
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
|
192 The file parameter must have a write() method that accepts a single |
|
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
|
193 string argument. It can thus be an open file object, a StringIO |
|
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
|
194 object, or any other custom object that meets this interface. |
|
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
|
195 |
|
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
|
196 """ |
|
27771
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
197 if protocol is None: |
|
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
198 protocol = 0 |
|
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
199 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
|
200 protocol = HIGHEST_PROTOCOL |
|
b4eb34372bdb
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents:
27825
diff
changeset
|
201 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
|
202 raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
203 self.write = file.write |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
204 self.memo = {} |
|
27771
9f713c7b7fd9
Rename 'proto' keyword arg to 'protocol' . Greg Ward's suggestion.
Guido van Rossum <guido@python.org>
parents:
27738
diff
changeset
|
205 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
|
206 self.bin = protocol >= 1 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27512
diff
changeset
|
207 self.fast = 0 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
208 |
|
23331
aae6dc6b2a58
Add Pickler.clear_memo() so the pickle and cPickle modules are more similar.
Fred Drake <fdrake@acm.org>
parents:
22851
diff
changeset
|
209 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
|
210 """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
|
211 |
|
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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 |
|
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 """ |
|
23331
aae6dc6b2a58
Add Pickler.clear_memo() so the pickle and cPickle modules are more similar.
Fred Drake <fdrake@acm.org>
parents:
22851
diff
changeset
|
218 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
|
219 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
220 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
|
221 """Write a pickled representation of obj to the open file.""" |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
222 if self.proto >= 2: |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
223 self.write(PROTO + chr(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
|
224 self.save(obj) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
225 self.write(STOP) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
226 |
|
27362
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
227 def memoize(self, obj): |
|
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
228 """Store an object in the memo.""" |
|
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
229 |
|
27427
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
230 # 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
|
231 # 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
|
232 # 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
|
233 # 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
|
234 # 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
|
235 # pickling. |
|
27362
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
236 |
|
27427
bf0bc8481502
memoize(): Reworded the docs to try to disentangle the Pickler's memo
Tim Peters <tim.peters@gmail.com>
parents:
27426
diff
changeset
|
237 # 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
|
238 # 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
|
239 # 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
|
240 # 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
|
241 # 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
|
242 if self.fast: |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27512
diff
changeset
|
243 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
|
244 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
|
245 memo_len = len(self.memo) |
|
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
246 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
|
247 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
|
248 |
| 27428 | 249 # 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
|
250 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
|
251 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
|
252 if i < 256: |
|
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
|
253 return BINPUT + chr(i) |
|
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
|
254 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
|
255 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
|
256 |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
257 return PUT + repr(i) + '\n' |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
258 |
| 27428 | 259 # 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
|
260 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
|
261 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
|
262 if i < 256: |
|
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
|
263 return BINGET + chr(i) |
|
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
|
264 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
|
265 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
|
266 |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
267 return GET + repr(i) + '\n' |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15605
diff
changeset
|
268 |
|
27493
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
269 def save(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
|
270 # 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
|
271 pid = self.persistent_id(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
|
272 if pid: |
|
26201
7e58a1504872
Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25332
diff
changeset
|
273 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
|
274 return |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
275 |
|
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
|
276 # 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
|
277 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
|
278 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
|
279 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
|
280 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
|
281 |
|
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
|
282 # 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
|
283 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
|
284 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
|
285 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
|
286 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
|
287 return |
| 6824 | 288 |
|
47189
f419b2e96a98
Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents:
47108
diff
changeset
|
289 # Check copy_reg.dispatch_table |
|
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
|
290 reduce = 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
|
291 if reduce: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
292 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
|
293 else: |
|
72641
64053bd79590
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
71539
diff
changeset
|
294 # Check for a class with a custom metaclass; treat as regular class |
|
64053bd79590
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
71539
diff
changeset
|
295 try: |
|
64053bd79590
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
71539
diff
changeset
|
296 issc = issubclass(t, TypeType) |
|
64053bd79590
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
71539
diff
changeset
|
297 except TypeError: # t is not a class (old Boost; see SF #502085) |
|
64053bd79590
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
71539
diff
changeset
|
298 issc = 0 |
|
64053bd79590
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
71539
diff
changeset
|
299 if issc: |
|
64053bd79590
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
71539
diff
changeset
|
300 self.save_global(obj) |
|
64053bd79590
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
71539
diff
changeset
|
301 return |
|
64053bd79590
Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents:
71539
diff
changeset
|
302 |
|
27948
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
303 # 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
|
304 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
|
305 if reduce: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
306 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
|
307 else: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
308 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
|
309 if reduce: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
310 rv = reduce() |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
311 else: |
|
db1d4584a5e1
Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents:
27901
diff
changeset
|
312 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
|
313 (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
|
314 |
|
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
|
315 # Check for string returned by reduce(), meaning "save as global" |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
316 if type(rv) is StringType: |
|
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
|
317 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
|
318 return |
|
6b0dbde0fed7
save(): Fiddled the control flow to put the normal case where it
Tim Peters <tim.peters@gmail.com>
parents:
27441
diff
changeset
|
319 |
|
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
|
320 # Assert that reduce() returned a tuple |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
321 if type(rv) is not TupleType: |
|
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
|
322 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
|
323 |
|
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
|
324 # 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
|
325 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
|
326 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
|
327 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
|
328 "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
|
329 |
|
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
|
330 # 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
|
331 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
|
332 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
333 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
|
334 # 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
|
335 return None |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
336 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
337 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
|
338 # 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
|
339 if self.bin: |
|
26201
7e58a1504872
Remove inst_persistent_id() WANNI (we ain't never needed it).
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25332
diff
changeset
|
340 self.save(pid) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
341 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
|
342 else: |
|
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
|
343 self.write(PERSID + str(pid) + '\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
|
344 |
|
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
|
345 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
|
346 listitems=None, dictitems=None, obj=None): |
|
29681
c149dd4cfd66
Fix grammar in comment.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27952
diff
changeset
|
347 # 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
|
348 |
|
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 # Assert that args is a tuple or None |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
350 if not isinstance(args, TupleType): |
|
34126
79066ea38c50
Eliminate the deprecated option to return None instead of a tuple of arguments in __reduce__().
Raymond Hettinger <python@rcn.com>
parents:
34102
diff
changeset
|
351 raise PicklingError("args from 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
|
352 |
|
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
|
353 # Assert that func is callable |
|
48180
381a71df060e
Remove use of callable() from pickle to silence warnings under -3.
Brett Cannon <bcannon@gmail.com>
parents:
47189
diff
changeset
|
354 if not hasattr(func, '__call__'): |
|
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
|
355 raise PicklingError("func from reduce should be callable") |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
356 |
|
924ade80104b
Made save() fit on a page, while adding comments. (I moved some type
Guido van Rossum <guido@python.org>
parents:
27474
diff
changeset
|
357 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
|
358 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
|
359 |
|
27553
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
360 # 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
|
361 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
|
362 # 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
|
363 # 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
|
364 # 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
|
365 # 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
|
366 # called __newobj__, and its first argument should be a |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
367 # new-style class. The implementation for __newobj__ |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
368 # 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
|
369 # 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
|
370 # |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
371 # 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
|
372 # 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
|
373 # |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
374 # 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
|
375 # 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
|
376 # 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
|
377 # 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
|
378 # (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
|
379 # 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
|
380 # 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
|
381 # 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
|
382 # |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
383 # 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
|
384 # 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
|
385 # 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
|
386 # 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
|
387 # 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
|
388 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
|
389 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
|
390 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
|
391 "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
|
392 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
|
393 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
|
394 "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
|
395 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
|
396 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
|
397 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
|
398 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
|
399 else: |
|
6597c09b1e6d
Add a magical feature to save_reduce so that __reduce__ can cause
Guido van Rossum <guido@python.org>
parents:
27552
diff
changeset
|
400 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
|
401 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
|
402 write(REDUCE) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15605
diff
changeset
|
403 |
|
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
|
404 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
|
405 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
|
406 |
|
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
|
407 # 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
|
408 # 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
|
409 # 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
|
410 # 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
|
411 |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
412 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
|
413 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
|
414 |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
415 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
|
416 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
|
417 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
418 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
|
419 save(state) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
420 write(BUILD) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
421 |
|
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
|
422 # 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
|
423 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
424 dispatch = {} |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
425 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
426 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
|
427 self.write(NONE) |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
428 dispatch[NoneType] = save_none |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
429 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
430 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
|
431 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
|
432 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
|
433 else: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
434 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
|
435 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
|
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_int(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
|
438 if self.bin: |
|
17368
bb75d93f6a31
test_pickle works on sizeof(long)==8 boxes again.
Tim Peters <tim.peters@gmail.com>
parents:
17362
diff
changeset
|
439 # If the int is small enough to fit in a signed 4-byte 2's-comp |
|
bb75d93f6a31
test_pickle works on sizeof(long)==8 boxes again.
Tim Peters <tim.peters@gmail.com>
parents:
17362
diff
changeset
|
440 # format, we can store it more efficiently than the general |
|
bb75d93f6a31
test_pickle works on sizeof(long)==8 boxes again.
Tim Peters <tim.peters@gmail.com>
parents:
17362
diff
changeset
|
441 # case. |
|
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
|
442 # First one- and two-byte unsigned ints: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
443 if obj >= 0: |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
444 if obj <= 0xff: |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
445 self.write(BININT1 + chr(obj)) |
|
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
|
446 return |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
447 if obj <= 0xffff: |
|
27523
b173a2a23082
Use %c rather than chr() to turn some ints into chars.
Guido van Rossum <guido@python.org>
parents:
27521
diff
changeset
|
448 self.write("%c%c%c" % (BININT2, obj&0xff, obj>>8)) |
|
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
|
449 return |
|
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
|
450 # Next check for 4-byte signed ints: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
451 high_bits = obj >> 31 # note that Python shift sign-extends |
|
27455
b2ce9c637a2b
Fixed odd whitespace after "if", which I believe I introduced long ago.
Tim Peters <tim.peters@gmail.com>
parents:
27454
diff
changeset
|
452 if high_bits == 0 or high_bits == -1: |
|
17368
bb75d93f6a31
test_pickle works on sizeof(long)==8 boxes again.
Tim Peters <tim.peters@gmail.com>
parents:
17362
diff
changeset
|
453 # All high bits are copies of bit 2**31, so the value |
|
bb75d93f6a31
test_pickle works on sizeof(long)==8 boxes again.
Tim Peters <tim.peters@gmail.com>
parents:
17362
diff
changeset
|
454 # fits in a 4-byte signed int. |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
455 self.write(BININT + pack("<i", obj)) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
456 return |
|
17368
bb75d93f6a31
test_pickle works on sizeof(long)==8 boxes again.
Tim Peters <tim.peters@gmail.com>
parents:
17362
diff
changeset
|
457 # Text pickle, or int too big to fit in signed 4-byte format. |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
458 self.write(INT + repr(obj) + '\n') |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
459 dispatch[IntType] = save_int |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
460 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
461 def save_long(self, obj, pack=struct.pack): |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
462 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
|
463 bytes = encode_long(obj) |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
464 n = len(bytes) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
465 if n < 256: |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
466 self.write(LONG1 + chr(n) + bytes) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
467 else: |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
468 self.write(LONG4 + pack("<i", n) + bytes) |
|
27585
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
469 return |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
470 self.write(LONG + repr(obj) + '\n') |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
471 dispatch[LongType] = save_long |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
472 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
473 def save_float(self, obj, pack=struct.pack): |
| 9439 | 474 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
|
475 self.write(BINFLOAT + pack('>d', obj)) |
| 9439 | 476 else: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
477 self.write(FLOAT + repr(obj) + '\n') |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
478 dispatch[FloatType] = save_float |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
479 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
480 def save_string(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
|
481 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
|
482 n = len(obj) |
|
27426
e26a2f7f224b
Using marshal functions to pack & unpack 1-byte ints is an obscure &
Tim Peters <tim.peters@gmail.com>
parents:
27425
diff
changeset
|
483 if n < 256: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
484 self.write(SHORT_BINSTRING + chr(n) + obj) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
485 else: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
486 self.write(BINSTRING + pack("<i", n) + obj) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
487 else: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
488 self.write(STRING + repr(obj) + '\n') |
|
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.memoize(obj) |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
490 dispatch[StringType] = save_string |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
491 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
492 def save_unicode(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
|
493 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
|
494 encoding = obj.encode('utf-8') |
|
27426
e26a2f7f224b
Using marshal functions to pack & unpack 1-byte ints is an obscure &
Tim Peters <tim.peters@gmail.com>
parents:
27425
diff
changeset
|
495 n = len(encoding) |
|
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
|
496 self.write(BINUNICODE + pack("<i", n) + encoding) |
|
11989
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
497 else: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
498 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
|
499 obj = obj.replace("\n", "\\u000a") |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
500 self.write(UNICODE + obj.encode('raw-unicode-escape') + '\n') |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
501 self.memoize(obj) |
|
11989
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
502 dispatch[UnicodeType] = save_unicode |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
503 |
|
52856
e6e5a0e43543
compare types with is
Benjamin Peterson <benjamin@python.org>
parents:
48180
diff
changeset
|
504 if StringType is UnicodeType: |
|
16105
d69571bf7842
Finn Bock (SF patch #103349):
Guido van Rossum <guido@python.org>
parents:
15870
diff
changeset
|
505 # This is true for Jython |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
506 def save_string(self, obj, pack=struct.pack): |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
507 unicode = obj.isunicode() |
|
16105
d69571bf7842
Finn Bock (SF patch #103349):
Guido van Rossum <guido@python.org>
parents:
15870
diff
changeset
|
508 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
509 if self.bin: |
|
16105
d69571bf7842
Finn Bock (SF patch #103349):
Guido van Rossum <guido@python.org>
parents:
15870
diff
changeset
|
510 if unicode: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
511 obj = obj.encode("utf-8") |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
512 l = len(obj) |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
513 if l < 256 and not unicode: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
514 self.write(SHORT_BINSTRING + chr(l) + obj) |
|
16105
d69571bf7842
Finn Bock (SF patch #103349):
Guido van Rossum <guido@python.org>
parents:
15870
diff
changeset
|
515 else: |
|
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
|
516 s = pack("<i", l) |
|
16105
d69571bf7842
Finn Bock (SF patch #103349):
Guido van Rossum <guido@python.org>
parents:
15870
diff
changeset
|
517 if unicode: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
518 self.write(BINUNICODE + s + obj) |
|
16105
d69571bf7842
Finn Bock (SF patch #103349):
Guido van Rossum <guido@python.org>
parents:
15870
diff
changeset
|
519 else: |
|
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.write(BINSTRING + s + obj) |
|
16105
d69571bf7842
Finn Bock (SF patch #103349):
Guido van Rossum <guido@python.org>
parents:
15870
diff
changeset
|
521 else: |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16474
diff
changeset
|
522 if unicode: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
523 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
|
524 obj = obj.replace("\n", "\\u000a") |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
525 obj = obj.encode('raw-unicode-escape') |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
526 self.write(UNICODE + obj + '\n') |
|
16105
d69571bf7842
Finn Bock (SF patch #103349):
Guido van Rossum <guido@python.org>
parents:
15870
diff
changeset
|
527 else: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
528 self.write(STRING + repr(obj) + '\n') |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
529 self.memoize(obj) |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
530 dispatch[StringType] = save_string |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16474
diff
changeset
|
531 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
532 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
|
533 write = self.write |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
534 proto = self.proto |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
535 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
536 n = len(obj) |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
537 if n == 0: |
|
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
538 if proto: |
|
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
539 write(EMPTY_TUPLE) |
|
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
540 else: |
|
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
541 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
|
542 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
|
543 |
|
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 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
|
545 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
|
546 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
|
547 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
|
548 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
|
549 # 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
|
550 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
|
551 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
|
552 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
|
553 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
|
554 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
|
555 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
|
556 return |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
557 |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
558 # 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
|
559 # 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
|
560 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
|
561 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
|
562 save(element) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
563 |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
564 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
|
565 # 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
|
566 # 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
|
567 # 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
|
568 # 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
|
569 # 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
|
570 # 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
|
571 # 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
|
572 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
|
573 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
|
574 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
|
575 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
|
576 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
|
577 return |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
578 |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
579 # 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
|
580 self.write(TUPLE) |
|
27615
45ad77a43d87
cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
581 self.memoize(obj) |
|
27362
4a49b5aa5d21
Raise PicklingError when __reduce__() fails, and
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26201
diff
changeset
|
582 |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
583 dispatch[TupleType] = save_tuple |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
584 |
|
27478
712b01d52e8c
save_empty_tuple(): Comment on why we can't get rid of this.
Tim Peters <tim.peters@gmail.com>
parents:
27477
diff
changeset
|
585 # save_empty_tuple() isn't used by anything in Python 2.3. However, I |
|
712b01d52e8c
save_empty_tuple(): Comment on why we can't get rid of this.
Tim Peters <tim.peters@gmail.com>
parents:
27477
diff
changeset
|
586 # found a Pickler subclass in Zope3 that calls it, so it's not harmless |
|
712b01d52e8c
save_empty_tuple(): Comment on why we can't get rid of this.
Tim Peters <tim.peters@gmail.com>
parents:
27477
diff
changeset
|
587 # to remove it. |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
588 def save_empty_tuple(self, obj): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
589 self.write(EMPTY_TUPLE) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
590 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
591 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
|
592 write = self.write |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
593 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
594 if self.bin: |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
595 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
|
596 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
|
597 write(MARK + LIST) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
598 |
|
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
|
599 self.memoize(obj) |
|
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 self._batch_appends(iter(obj)) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
601 |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
602 dispatch[ListType] = save_list |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
603 |
|
27825
011f734258db
Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents:
27822
diff
changeset
|
604 # Keep in synch with cPickle's BATCHSIZE. Nothing will break if it gets |
|
011f734258db
Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents:
27822
diff
changeset
|
605 # out of synch, though. |
|
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
|
606 _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
|
607 |
|
3c89049fe628
Another extension to reduce(). It can return a 4- or 5-tuple now.
Guido van Rossum <guido@python.org>
parents:
27559
diff
changeset
|
608 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
|
609 # 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
|
610 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
|
611 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
|
612 |
|
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 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
|
614 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
|
615 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
|
616 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
|
617 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
|
618 |
|
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 r = xrange(self._BATCHSIZE) |
|
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 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
|
621 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
|
622 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
|
623 try: |
|
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
|
624 x = items.next() |
|
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
|
625 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
|
626 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
|
627 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
|
628 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
|
629 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
|
630 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
|
631 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
|
632 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
|
633 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
|
634 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
|
635 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
|
636 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
|
637 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
|
638 # 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
|
639 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
640 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
|
641 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
|
642 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
643 if self.bin: |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
644 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
|
645 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
|
646 write(MARK + DICT) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
647 |
|
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
|
648 self.memoize(obj) |
|
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 self._batch_setitems(obj.iteritems()) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
650 |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
651 dispatch[DictionaryType] = save_dict |
|
8431
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
652 if not PyStringMap is None: |
|
82f021eece2c
add handler for JPython's org.python.core.PyStringMap object, which
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
8036
diff
changeset
|
653 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
|
654 |
|
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
|
655 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
|
656 # 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
|
657 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
|
658 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
|
659 |
|
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 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
|
661 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
|
662 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
|
663 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
|
664 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
|
665 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
|
666 |
|
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 r = xrange(self._BATCHSIZE) |
|
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 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
|
669 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
|
670 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
|
671 try: |
|
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 tmp.append(items.next()) |
|
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 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
|
674 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
|
675 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
|
676 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
|
677 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
|
678 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
|
679 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
|
680 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
|
681 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
|
682 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
|
683 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
|
684 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
|
685 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
|
686 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
|
687 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
|
688 # 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
|
689 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
690 def save_inst(self, obj): |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
691 cls = obj.__class__ |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
692 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
693 memo = self.memo |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
694 write = self.write |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
695 save = self.save |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
696 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
697 if hasattr(obj, '__getinitargs__'): |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
698 args = obj.__getinitargs__() |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
699 len(args) # XXX Assert it's a sequence |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
700 _keep_alive(args, memo) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
701 else: |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
702 args = () |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
703 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
704 write(MARK) |
|
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
|
705 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
706 if self.bin: |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
707 save(cls) |
|
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
|
708 for arg in args: |
|
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
|
709 save(arg) |
|
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
|
710 write(OBJ) |
|
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
|
711 else: |
|
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
|
712 for arg in args: |
|
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
|
713 save(arg) |
|
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
|
714 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n') |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
715 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
716 self.memoize(obj) |
|
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
|
717 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
718 try: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
719 getstate = obj.__getstate__ |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
720 except AttributeError: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
721 stuff = obj.__dict__ |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
722 else: |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
723 stuff = getstate() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
724 _keep_alive(stuff, memo) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
725 save(stuff) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
726 write(BUILD) |
|
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
|
727 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
728 dispatch[InstanceType] = save_inst |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
729 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
730 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
|
731 write = self.write |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
732 memo = self.memo |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
733 |
|
17362
6b8d91ea9de3
Mechanical fiddling for easier reading: every "if" test was enclosed in
Tim Peters <tim.peters@gmail.com>
parents:
16655
diff
changeset
|
734 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
|
735 name = obj.__name__ |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
736 |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27554
diff
changeset
|
737 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
|
738 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
|
739 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
|
740 |
|
19344
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
741 try: |
|
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
742 __import__(module) |
|
db9762529f45
Address SF #451547. The approach is a bit draconian: any object that
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
743 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
|
744 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
|
745 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
|
746 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
|
747 "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
|
748 (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
|
749 else: |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
750 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
|
751 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
|
752 "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
|
753 (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
|
754 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
755 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
|
756 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
|
757 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
|
758 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
|
759 if code <= 0xff: |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
760 write(EXT1 + chr(code)) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
761 elif code <= 0xffff: |
|
27523
b173a2a23082
Use %c rather than chr() to turn some ints into chars.
Guido van Rossum <guido@python.org>
parents:
27521
diff
changeset
|
762 write("%c%c%c" % (EXT2, 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
|
763 else: |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
764 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
|
765 return |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
766 |
|
27443
8f13729a94a9
Several routines appeared to inline the guts of memoize(), possibly for
Tim Peters <tim.peters@gmail.com>
parents:
27442
diff
changeset
|
767 write(GLOBAL + module + '\n' + name + '\n') |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
768 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
|
769 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
770 dispatch[ClassType] = save_global |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
771 dispatch[FunctionType] = save_global |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
772 dispatch[BuiltinFunctionType] = save_global |
|
34552
a24e70f4684b
Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents:
34551
diff
changeset
|
773 dispatch[TypeType] = save_global |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
774 |
|
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
|
775 # 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
|
776 |
|
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
|
777 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
|
778 """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
|
779 |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
780 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
|
781 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
|
782 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
|
783 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
|
784 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
|
785 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
|
786 """ |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
787 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
788 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
|
789 except KeyError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
790 # 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
|
791 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
|
792 |
|
33cef01961a7
Add the same _keep_alive patch (by Michael Scharff) that was added to
Guido van Rossum <guido@python.org>
parents:
5343
diff
changeset
|
793 |
|
27506
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
794 # 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
|
795 # the module in which the function was found. |
|
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
796 |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
797 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
|
798 |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
799 def whichmodule(func, funcname): |
|
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
800 """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
|
801 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
802 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
|
803 Cache in classmap. |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
804 Return a module name. |
|
27506
b480a4f707f6
pickle: Comment repair.
Tim Peters <tim.peters@gmail.com>
parents:
27499
diff
changeset
|
805 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
|
806 """ |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27554
diff
changeset
|
807 # 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
|
808 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
|
809 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
|
810 return mod |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
811 if func in classmap: |
|
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
812 return classmap[func] |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
813 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
814 for name, module in sys.modules.items(): |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
815 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
|
816 continue # skip dummy package entries |
|
27721
04384f4290ba
No need for a continuation line.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27720
diff
changeset
|
817 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
|
818 break |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
819 else: |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
820 name = '__main__' |
|
25332
2f44a90fff07
Fiddle comments and variable names in whichmodule().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25331
diff
changeset
|
821 classmap[func] = name |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
822 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
|
823 |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
824 |
|
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
|
825 # 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
|
826 |
|
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
|
827 class Unpickler: |
|
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
828 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
829 def __init__(self, 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
|
830 """This takes a file-like object for reading a pickle data stream. |
|
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
|
831 |
|
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
|
832 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
|
833 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
|
834 |
|
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
|
835 The file-like object must have two methods, a read() method that |
|
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
|
836 takes an integer argument, and a readline() method that requires no |
|
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
|
837 arguments. Both methods should return a string. Thus file-like |
|
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
|
838 object can be a file object opened for reading, a StringIO 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
|
839 or any other custom object that meets this interface. |
|
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
|
840 """ |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
841 self.readline = file.readline |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
842 self.read = file.read |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
843 self.memo = {} |
|
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
|
844 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
845 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
|
846 """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
|
847 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
848 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
|
849 """ |
|
20923
98dbb079c5d4
A better new, unique object
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20442
diff
changeset
|
850 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
|
851 self.stack = [] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
852 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
|
853 read = self.read |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
854 dispatch = self.dispatch |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
855 try: |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
856 while 1: |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
857 key = read(1) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
858 dispatch[key](self) |
|
15552
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
859 except _Stop, stopinst: |
|
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
860 return stopinst.value |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
861 |
| 27449 | 862 # Return largest index k such that self.stack[k] is self.mark. |
| 863 # If the stack doesn't contain a mark, eventually raises IndexError. | |
| 864 # This could be sped by maintaining another stack, of indices at which | |
| 865 # the mark appears. For that matter, the latter stack would suffice, | |
| 866 # and we wouldn't need to push mark objects on self.stack at all. | |
| 867 # Doing so is probably a good thing, though, since if the pickle is | |
| 868 # corrupt (or hostile) we may get a clue from finding self.mark embedded | |
| 869 # in unpickled objects. | |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
870 def marker(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
871 stack = self.stack |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
872 mark = self.mark |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
873 k = len(stack)-1 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
874 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
|
875 return k |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
876 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
877 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
|
878 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
879 def load_eof(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
880 raise EOFError |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
881 dispatch[''] = load_eof |
|
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
|
882 |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
883 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
|
884 proto = ord(self.read(1)) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
885 if not 0 <= proto <= 2: |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
886 raise ValueError, "unsupported pickle protocol: %d" % proto |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
887 dispatch[PROTO] = load_proto |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
888 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
889 def load_persid(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
890 pid = self.readline()[:-1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
891 self.append(self.persistent_load(pid)) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
892 dispatch[PERSID] = load_persid |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
893 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
894 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
|
895 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
|
896 self.append(self.persistent_load(pid)) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
897 dispatch[BINPERSID] = load_binpersid |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
898 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
899 def load_none(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
900 self.append(None) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
901 dispatch[NONE] = load_none |
|
2475
30205d634a15
raise EOFError when load() hits EOF, instead of KeyError
Guido van Rossum <guido@python.org>
parents:
2356
diff
changeset
|
902 |
|
27461
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
903 def load_false(self): |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
904 self.append(False) |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
905 dispatch[NEWFALSE] = load_false |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
906 |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
907 def load_true(self): |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
908 self.append(True) |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
909 dispatch[NEWTRUE] = load_true |
|
883af28b9a0d
OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents:
27459
diff
changeset
|
910 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
911 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
|
912 data = self.readline() |
|
22851
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
913 if data == FALSE[1:]: |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
914 val = False |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
915 elif data == TRUE[1:]: |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
916 val = True |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
917 else: |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
918 try: |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
919 val = int(data) |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
920 except ValueError: |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
921 val = long(data) |
|
ed03806d88cd
Implement an idea by Paul Rubin:
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
922 self.append(val) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
923 dispatch[INT] = load_int |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
924 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
925 def load_binint(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
926 self.append(mloads('i' + self.read(4))) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
927 dispatch[BININT] = load_binint |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
928 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
929 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
|
930 self.append(ord(self.read(1))) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
931 dispatch[BININT1] = 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
|
932 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
933 def load_binint2(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
934 self.append(mloads('i' + self.read(2) + '\000\000')) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
935 dispatch[BININT2] = load_binint2 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15605
diff
changeset
|
936 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
937 def load_long(self): |
|
15552
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
938 self.append(long(self.readline()[:-1], 0)) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
939 dispatch[LONG] = 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
|
940 |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
941 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
|
942 n = ord(self.read(1)) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
943 bytes = self.read(n) |
|
27585
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
944 self.append(decode_long(bytes)) |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
945 dispatch[LONG1] = load_long1 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
946 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
947 def load_long4(self): |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
948 n = mloads('i' + self.read(4)) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
949 bytes = self.read(n) |
|
27585
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
950 self.append(decode_long(bytes)) |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
951 dispatch[LONG4] = load_long4 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
952 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
953 def load_float(self): |
|
15552
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
954 self.append(float(self.readline()[:-1])) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
955 dispatch[FLOAT] = load_float |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
956 |
| 9439 | 957 def load_binfloat(self, unpack=struct.unpack): |
| 958 self.append(unpack('>d', self.read(8))[0]) | |
| 959 dispatch[BINFLOAT] = load_binfloat | |
| 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_string(self): |
| 14594 | 962 rep = self.readline()[:-1] |
|
27474
38c7c2d4cd5a
Got rid of the _quotes global. Used only once, and is trivial.
Tim Peters <tim.peters@gmail.com>
parents:
27470
diff
changeset
|
963 for q in "\"'": # 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
|
964 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
|
965 if not rep.endswith(q): |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24252
diff
changeset
|
966 raise ValueError, "insecure string pickle" |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24252
diff
changeset
|
967 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
|
968 break |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24252
diff
changeset
|
969 else: |
| 14594 | 970 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
|
971 self.append(rep.decode("string-escape")) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
972 dispatch[STRING] = load_string |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
973 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
974 def load_binstring(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
975 len = mloads('i' + self.read(4)) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
976 self.append(self.read(len)) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
977 dispatch[BINSTRING] = load_binstring |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
978 |
|
11989
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
979 def load_unicode(self): |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
980 self.append(unicode(self.readline()[:-1],'raw-unicode-escape')) |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
981 dispatch[UNICODE] = load_unicode |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
982 |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
983 def load_binunicode(self): |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
984 len = mloads('i' + self.read(4)) |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
985 self.append(unicode(self.read(len),'utf-8')) |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
986 dispatch[BINUNICODE] = load_binunicode |
|
ee6492a5fdee
Marc-Andre Lemburg: support pickling Unicode objects, both in text
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
987 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
988 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
|
989 len = ord(self.read(1)) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
990 self.append(self.read(len)) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
991 dispatch[SHORT_BINSTRING] = load_short_binstring |
|
3513
4361a5ffb638
Don't atoi() the memo key.
Guido van Rossum <guido@python.org>
parents:
2923
diff
changeset
|
992 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
993 def load_tuple(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
994 k = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
995 self.stack[k:] = [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
|
996 dispatch[TUPLE] = load_tuple |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
997 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
998 def load_empty_tuple(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
999 self.stack.append(()) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1000 dispatch[EMPTY_TUPLE] = 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
|
1001 |
|
27459
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1002 def load_tuple1(self): |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1003 self.stack[-1] = (self.stack[-1],) |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1004 dispatch[TUPLE1] = load_tuple1 |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1005 |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1006 def load_tuple2(self): |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1007 self.stack[-2:] = [(self.stack[-2], self.stack[-1])] |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1008 dispatch[TUPLE2] = load_tuple2 |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1009 |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1010 def load_tuple3(self): |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1011 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])] |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1012 dispatch[TUPLE3] = load_tuple3 |
|
ea480406cc65
More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents:
27457
diff
changeset
|
1013 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1014 def load_empty_list(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1015 self.stack.append([]) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1016 dispatch[EMPTY_LIST] = 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
|
1017 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1018 def load_empty_dictionary(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1019 self.stack.append({}) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1020 dispatch[EMPTY_DICT] = load_empty_dictionary |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1021 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1022 def load_list(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1023 k = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1024 self.stack[k:] = [self.stack[k+1:]] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1025 dispatch[LIST] = 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
|
1026 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1027 def load_dict(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1028 k = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1029 d = {} |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1030 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
|
1031 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
|
1032 key = items[i] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1033 value = items[i+1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1034 d[key] = value |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1035 self.stack[k:] = [d] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1036 dispatch[DICT] = load_dict |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1037 |
|
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
|
1038 # 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
|
1039 # 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
|
1040 # 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
|
1041 # 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
|
1042 # 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
|
1043 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
|
1044 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
|
1045 del self.stack[k:] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1046 instantiated = 0 |
|
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
|
1047 if (not args and |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1048 type(klass) is ClassType and |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1049 not hasattr(klass, "__getinitargs__")): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1050 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1051 value = _EmptyClass() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1052 value.__class__ = klass |
|
8036
e091e9d495d5
Whoops! Add a missing 'instantiated = 1' to load_inst(); otherwise it
Guido van Rossum <guido@python.org>
parents:
7775
diff
changeset
|
1053 instantiated = 1 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1054 except RuntimeError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1055 # In restricted execution, assignment to inst.__class__ is |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1056 # prohibited |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1057 pass |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1058 if not instantiated: |
|
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
|
1059 try: |
|
27498
d8750e0d3fb8
Get rid of __safe_for_unpickling__ and safe_constructors.
Guido van Rossum <guido@python.org>
parents:
27493
diff
changeset
|
1060 value = klass(*args) |
|
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
|
1061 except TypeError, err: |
|
d237c808a01e
In load_inst(), when instantiating an instance the old way (i.e. when
Guido van Rossum <guido@python.org>
parents:
8431
diff
changeset
|
1062 raise TypeError, "in constructor for %s: %s" % ( |
|
d237c808a01e
In load_inst(), when instantiating an instance the old way (i.e. when
Guido van Rossum <guido@python.org>
parents:
8431
diff
changeset
|
1063 klass.__name__, str(err)), sys.exc_info()[2] |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1064 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
|
1065 |
|
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 def load_inst(self): |
|
99099deb6026
load_inst(), load_obj(): Put the bulk of these into a common new
Tim Peters <tim.peters@gmail.com>
parents:
27529
diff
changeset
|
1067 module = self.readline()[:-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
|
1068 name = self.readline()[:-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 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
|
1070 self._instantiate(klass, self.marker()) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1071 dispatch[INST] = 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
|
1072 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1073 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
|
1074 # 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
|
1075 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
|
1076 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
|
1077 self._instantiate(klass, k) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15605
diff
changeset
|
1078 dispatch[OBJ] = load_obj |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1079 |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
1080 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
|
1081 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
|
1082 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
|
1083 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
|
1084 self.stack[-1] = obj |
|
27469
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
1085 dispatch[NEWOBJ] = load_newobj |
|
5c7548bf377b
Rename all variables 'object' to 'obj' to avoid conflicts with the
Guido van Rossum <guido@python.org>
parents:
27467
diff
changeset
|
1086 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1087 def load_global(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1088 module = self.readline()[:-1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1089 name = self.readline()[:-1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1090 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
|
1091 self.append(klass) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1092 dispatch[GLOBAL] = load_global |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1093 |
|
27511
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1094 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
|
1095 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
|
1096 self.get_extension(code) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1097 dispatch[EXT1] = load_ext1 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1098 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1099 def load_ext2(self): |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1100 code = mloads('i' + self.read(2) + '\000\000') |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1101 self.get_extension(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 dispatch[EXT2] = load_ext2 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1103 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1104 def load_ext4(self): |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1105 code = mloads('i' + self.read(4)) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1106 self.get_extension(code) |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1107 dispatch[EXT4] = load_ext4 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1108 |
|
0de14252d26c
Support for extension codes. (By accident I checked in the tests first.)
Guido van Rossum <guido@python.org>
parents:
27506
diff
changeset
|
1109 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
|
1110 nil = [] |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27649
diff
changeset
|
1111 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
|
1112 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
|
1113 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
|
1114 return |
|
27665
d5eb12bccd1e
Rename the extension registry variables to have leading underscores --
Guido van Rossum <guido@python.org>
parents:
27649
diff
changeset
|
1115 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
|
1116 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
|
1117 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
|
1118 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
|
1119 _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
|
1120 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
|
1121 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1122 def find_class(self, module, name): |
|
27498
d8750e0d3fb8
Get rid of __safe_for_unpickling__ and safe_constructors.
Guido van Rossum <guido@python.org>
parents:
27493
diff
changeset
|
1123 # Subclasses may override this |
| 20991 | 1124 __import__(module) |
| 1125 mod = sys.modules[module] | |
| 1126 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
|
1127 return klass |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1128 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1129 def load_reduce(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1130 stack = self.stack |
|
27498
d8750e0d3fb8
Get rid of __safe_for_unpickling__ and safe_constructors.
Guido van Rossum <guido@python.org>
parents:
27493
diff
changeset
|
1131 args = stack.pop() |
|
d8750e0d3fb8
Get rid of __safe_for_unpickling__ and safe_constructors.
Guido van Rossum <guido@python.org>
parents:
27493
diff
changeset
|
1132 func = stack[-1] |
|
34126
79066ea38c50
Eliminate the deprecated option to return None instead of a tuple of arguments in __reduce__().
Raymond Hettinger <python@rcn.com>
parents:
34102
diff
changeset
|
1133 value = func(*args) |
|
27498
d8750e0d3fb8
Get rid of __safe_for_unpickling__ and safe_constructors.
Guido van Rossum <guido@python.org>
parents:
27493
diff
changeset
|
1134 stack[-1] = value |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1135 dispatch[REDUCE] = load_reduce |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1136 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1137 def load_pop(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1138 del self.stack[-1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1139 dispatch[POP] = 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
|
1140 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1141 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
|
1142 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
|
1143 del self.stack[k:] |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1144 dispatch[POP_MARK] = load_pop_mark |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1145 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1146 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
|
1147 self.append(self.stack[-1]) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1148 dispatch[DUP] = 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
|
1149 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1150 def load_get(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1151 self.append(self.memo[self.readline()[:-1]]) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1152 dispatch[GET] = 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
|
1153 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1154 def load_binget(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
|
1155 i = ord(self.read(1)) |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
1156 self.append(self.memo[repr(i)]) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1157 dispatch[BINGET] = 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
|
1158 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1159 def load_long_binget(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1160 i = mloads('i' + self.read(4)) |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
1161 self.append(self.memo[repr(i)]) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1162 dispatch[LONG_BINGET] = load_long_binget |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1163 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1164 def load_put(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1165 self.memo[self.readline()[:-1]] = self.stack[-1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1166 dispatch[PUT] = load_put |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1167 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1168 def load_binput(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
|
1169 i = ord(self.read(1)) |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
1170 self.memo[repr(i)] = self.stack[-1] |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1171 dispatch[BINPUT] = 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
|
1172 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1173 def load_long_binput(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1174 i = mloads('i' + self.read(4)) |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29681
diff
changeset
|
1175 self.memo[repr(i)] = self.stack[-1] |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1176 dispatch[LONG_BINPUT] = 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
|
1177 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1178 def load_append(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1179 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
|
1180 value = stack.pop() |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1181 list = stack[-1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1182 list.append(value) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1183 dispatch[APPEND] = load_append |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1184 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1185 def load_appends(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1186 stack = self.stack |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1187 mark = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1188 list = stack[mark - 1] |
|
27450
4015a27a1d13
load_appends(): replaced .append() loop with an .extend().
Tim Peters <tim.peters@gmail.com>
parents:
27449
diff
changeset
|
1189 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
|
1190 del stack[mark:] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1191 dispatch[APPENDS] = load_appends |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15605
diff
changeset
|
1192 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1193 def load_setitem(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1194 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
|
1195 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
|
1196 key = stack.pop() |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1197 dict = stack[-1] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1198 dict[key] = value |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1199 dispatch[SETITEM] = load_setitem |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1200 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1201 def load_setitems(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1202 stack = self.stack |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1203 mark = self.marker() |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1204 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
|
1205 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
|
1206 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
|
1207 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1208 del stack[mark:] |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1209 dispatch[SETITEMS] = 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
|
1210 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1211 def load_build(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 |
|
27493
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1213 state = stack.pop() |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1214 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
|
1215 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
|
1216 if setstate: |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1217 setstate(state) |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1218 return |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1219 slotstate = None |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1220 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
|
1221 state, slotstate = state |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1222 if state: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1223 try: |
|
53693
e54141d63e01
Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
52856
diff
changeset
|
1224 d = inst.__dict__ |
|
e54141d63e01
Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
52856
diff
changeset
|
1225 try: |
|
e54141d63e01
Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
52856
diff
changeset
|
1226 for k, v in state.iteritems(): |
|
e54141d63e01
Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
52856
diff
changeset
|
1227 d[intern(k)] = v |
|
e54141d63e01
Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
52856
diff
changeset
|
1228 # keys in state don't have to be strings |
|
e54141d63e01
Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
52856
diff
changeset
|
1229 # don't blow up, but don't go out of our way |
|
e54141d63e01
Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
52856
diff
changeset
|
1230 except TypeError: |
|
e54141d63e01
Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
52856
diff
changeset
|
1231 d.update(state) |
|
e54141d63e01
Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
52856
diff
changeset
|
1232 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1233 except RuntimeError: |
|
27493
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1234 # XXX In restricted execution, the instance's __dict__ |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1235 # is not accessible. Use the old way of unpickling |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1236 # the instance variables. This is a semantic |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1237 # difference when unpickling in restricted |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1238 # vs. unrestricted modes. |
|
27901
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
27864
diff
changeset
|
1239 # Note, however, that cPickle has never tried to do the |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
27864
diff
changeset
|
1240 # .update() business, and always uses |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
27864
diff
changeset
|
1241 # PyObject_SetItem(inst.__dict__, key, value) in a |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
27864
diff
changeset
|
1242 # loop over state.items(). |
|
27493
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1243 for k, v in state.items(): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7274
diff
changeset
|
1244 setattr(inst, k, v) |
|
27493
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1245 if slotstate: |
|
7b7d3b906363
Instead of bad hacks trying to worm around the inherited
Guido van Rossum <guido@python.org>
parents:
27486
diff
changeset
|
1246 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
|
1247 setattr(inst, k, v) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1248 dispatch[BUILD] = 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
|
1249 |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1250 def load_mark(self): |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1251 self.append(self.mark) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1252 dispatch[MARK] = load_mark |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1253 |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1254 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
|
1255 value = self.stack.pop() |
|
15552
4d9c31f32c09
Get rid of string module and string exceptions.
Guido van Rossum <guido@python.org>
parents:
14594
diff
changeset
|
1256 raise _Stop(value) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1257 dispatch[STOP] = 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
|
1258 |
|
6758
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
1259 # Helper class for load_inst/load_obj |
|
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
1260 |
|
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
1261 class _EmptyClass: |
|
0b5eb32b551d
When instantiating a class with no arguments and where the class does
Guido van Rossum <guido@python.org>
parents:
6310
diff
changeset
|
1262 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
|
1263 |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1264 # Encode/decode longs in linear time. |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1265 |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1266 import binascii as _binascii |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1267 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1268 def encode_long(x): |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1269 r"""Encode a long to 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
|
1270 Note that 0L is a special case, returning an empty string, to save a |
|
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
|
1271 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
|
1272 |
|
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
|
1273 >>> encode_long(0L) |
|
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
|
1274 '' |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1275 >>> encode_long(255L) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1276 '\xff\x00' |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1277 >>> encode_long(32767L) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1278 '\xff\x7f' |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1279 >>> encode_long(-256L) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1280 '\x00\xff' |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1281 >>> encode_long(-32768L) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1282 '\x00\x80' |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1283 >>> encode_long(-128L) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1284 '\x80' |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1285 >>> encode_long(127L) |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1286 '\x7f' |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1287 >>> |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1288 """ |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1289 |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1290 if x == 0: |
|
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
|
1291 return '' |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1292 if x > 0: |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1293 ashex = hex(x) |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1294 assert ashex.startswith("0x") |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1295 njunkchars = 2 + ashex.endswith('L') |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1296 nibbles = len(ashex) - njunkchars |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1297 if nibbles & 1: |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1298 # need an even # of nibbles for unhexlify |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1299 ashex = "0x0" + ashex[2:] |
|
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
|
1300 elif int(ashex[2], 16) >= 8: |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1301 # "looks negative", so need a byte of sign bits |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1302 ashex = "0x00" + ashex[2:] |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1303 else: |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1304 # Build the 256's-complement: (1L << nbytes) + x. The trick is |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1305 # to find the number of bytes in linear time (although that should |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1306 # really be a constant-time task). |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1307 ashex = hex(-x) |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1308 assert ashex.startswith("0x") |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1309 njunkchars = 2 + ashex.endswith('L') |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1310 nibbles = len(ashex) - njunkchars |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1311 if nibbles & 1: |
|
27585
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
1312 # Extend to a full byte. |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1313 nibbles += 1 |
|
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
|
1314 nbits = nibbles * 4 |
|
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
|
1315 x += 1L << nbits |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1316 assert x > 0 |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1317 ashex = hex(x) |
|
27585
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
1318 njunkchars = 2 + ashex.endswith('L') |
|
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
1319 newnibbles = len(ashex) - njunkchars |
|
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
1320 if newnibbles < nibbles: |
|
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
1321 ashex = "0x" + "0" * (nibbles - newnibbles) + ashex[2:] |
|
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
1322 if int(ashex[2], 16) < 8: |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1323 # "looks positive", so need a byte of sign bits |
|
27585
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
1324 ashex = "0xff" + ashex[2:] |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1325 |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1326 if ashex.endswith('L'): |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1327 ashex = ashex[2:-1] |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1328 else: |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1329 ashex = ashex[2:] |
|
27585
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27584
diff
changeset
|
1330 assert len(ashex) & 1 == 0, (x, ashex) |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1331 binary = _binascii.unhexlify(ashex) |
|
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1332 return binary[::-1] |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1333 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1334 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
|
1335 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
|
1336 |
|
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
|
1337 >>> decode_long('') |
|
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
|
1338 0L |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1339 >>> decode_long("\xff\x00") |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1340 255L |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1341 >>> decode_long("\xff\x7f") |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1342 32767L |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1343 >>> decode_long("\x00\xff") |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1344 -256L |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1345 >>> decode_long("\x00\x80") |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1346 -32768L |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1347 >>> decode_long("\x80") |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1348 -128L |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1349 >>> decode_long("\x7f") |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1350 127L |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1351 """ |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1352 |
|
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
|
1353 nbytes = len(data) |
|
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
|
1354 if nbytes == 0: |
|
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
|
1355 return 0L |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1356 ashex = _binascii.hexlify(data[::-1]) |
|
27587
7283140d4319
long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents:
27585
diff
changeset
|
1357 n = long(ashex, 16) # quadratic time before Python 2.3; linear now |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1358 if data[-1] >= '\x80': |
|
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
|
1359 n -= 1L << (nbytes * 8) |
|
27543
ed2d23d2fd41
Linear-time implementations of {encode,decode}_long.
Tim Peters <tim.peters@gmail.com>
parents:
27534
diff
changeset
|
1360 return n |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1361 |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1362 # Shorthands |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1363 |
|
20442
467ac453ccc3
Use cStringIO when available.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20020
diff
changeset
|
1364 try: |
|
467ac453ccc3
Use cStringIO when available.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20020
diff
changeset
|
1365 from cStringIO import StringIO |
|
467ac453ccc3
Use cStringIO when available.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20020
diff
changeset
|
1366 except ImportError: |
|
467ac453ccc3
Use cStringIO when available.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20020
diff
changeset
|
1367 from StringIO import StringIO |
|
3791
2f57392b2fde
Optimizations and one intentional loophole by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
3550
diff
changeset
|
1368 |
|
34102
cddd016ae06a
Removed the deprecated bin parameter from the pickle module.
Raymond Hettinger <python@rcn.com>
parents:
33000
diff
changeset
|
1369 def dump(obj, file, protocol=None): |
|
cddd016ae06a
Removed the deprecated bin parameter from the pickle module.
Raymond Hettinger <python@rcn.com>
parents:
33000
diff
changeset
|
1370 Pickler(file, protocol).dump(obj) |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1371 |
|
34102
cddd016ae06a
Removed the deprecated bin parameter from the pickle module.
Raymond Hettinger <python@rcn.com>
parents:
33000
diff
changeset
|
1372 def dumps(obj, protocol=None): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1373 file = StringIO() |
|
34102
cddd016ae06a
Removed the deprecated bin parameter from the pickle module.
Raymond Hettinger <python@rcn.com>
parents:
33000
diff
changeset
|
1374 Pickler(file, protocol).dump(obj) |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1375 return file.getvalue() |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1376 |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1377 def load(file): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1378 return Unpickler(file).load() |
|
2534
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1379 |
|
73d193eaa900
pickle classes; add format_version, load(s)/dump(s) shortcuts
Guido van Rossum <guido@python.org>
parents:
2507
diff
changeset
|
1380 def loads(str): |
|
5278
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1381 file = StringIO(str) |
|
01c3e64bc824
New version by Digital Creations, supports binary format compatible
Guido van Rossum <guido@python.org>
parents:
5192
diff
changeset
|
1382 return Unpickler(file).load() |
|
27456
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1383 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1384 # Doctest |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1385 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1386 def _test(): |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1387 import doctest |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1388 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
|
1389 |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1390 if __name__ == "__main__": |
|
115ee40518a8
First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents:
27455
diff
changeset
|
1391 _test() |
