Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Create portable serialized representations of Python objects. |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 3 | See module cPickle for a (much) faster implementation. |
| 4 | See module copy_reg for a mechanism for registering custom picklers. |
Tim Peters | 22a449a | 2003-01-27 20:16:36 +0000 | [diff] [blame] | 5 | See module pickletools source for extensive comments. |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 7 | Classes: |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 9 | Pickler |
| 10 | Unpickler |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 12 | Functions: |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 14 | dump(object, file) |
| 15 | dumps(object) -> string |
| 16 | load(file) -> object |
| 17 | loads(string) -> object |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 19 | Misc variables: |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 20 | |
Fred Drake | fe82acc | 1998-02-13 03:24:48 +0000 | [diff] [blame] | 21 | __version__ |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 22 | format_version |
| 23 | compatible_formats |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 25 | """ |
| 26 | |
Guido van Rossum | 743d17e | 1998-09-15 20:25:57 +0000 | [diff] [blame] | 27 | __version__ = "$Revision$" # Code version |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 28 | |
| 29 | from types import * |
Guido van Rossum | 443ada4 | 2003-02-18 22:49:10 +0000 | [diff] [blame] | 30 | from copy_reg import dispatch_table |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 31 | from copy_reg import _extension_registry, _inverted_registry, _extension_cache |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 32 | import marshal |
| 33 | import sys |
| 34 | import struct |
Skip Montanaro | 23bafc6 | 2001-02-18 03:10:09 +0000 | [diff] [blame] | 35 | import re |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 36 | import warnings |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 37 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 38 | __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler", |
| 39 | "Unpickler", "dump", "dumps", "load", "loads"] |
| 40 | |
Tim Peters | c0c12b5 | 2003-01-29 00:56:17 +0000 | [diff] [blame] | 41 | # These are purely informational; no code uses these. |
Guido van Rossum | f29d3d6 | 2003-01-27 22:47:53 +0000 | [diff] [blame] | 42 | format_version = "2.0" # File format version we write |
| 43 | compatible_formats = ["1.0", # Original protocol 0 |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 44 | "1.1", # Protocol 0 with INST added |
Guido van Rossum | f29d3d6 | 2003-01-27 22:47:53 +0000 | [diff] [blame] | 45 | "1.2", # Original protocol 1 |
| 46 | "1.3", # Protocol 1 with BINFLOAT added |
| 47 | "2.0", # Protocol 2 |
| 48 | ] # Old format versions we can read |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 49 | |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 50 | # Keep in synch with cPickle. This is the highest protocol number we |
| 51 | # know how to read. |
| 52 | HIGHEST_PROTOCOL = 2 |
| 53 | |
Guido van Rossum | e0b9042 | 2003-01-28 03:17:21 +0000 | [diff] [blame] | 54 | # Why use struct.pack() for pickling but marshal.loads() for |
Tim Peters | c0c12b5 | 2003-01-29 00:56:17 +0000 | [diff] [blame] | 55 | # unpickling? struct.pack() is 40% faster than marshal.dumps(), but |
Guido van Rossum | e0b9042 | 2003-01-28 03:17:21 +0000 | [diff] [blame] | 56 | # marshal.loads() is twice as fast as struct.unpack()! |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 57 | mloads = marshal.loads |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 58 | |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 59 | class PickleError(Exception): |
Neal Norwitz | efbb67b | 2002-05-30 12:12:04 +0000 | [diff] [blame] | 60 | """A common base class for the other pickling exceptions.""" |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 61 | pass |
| 62 | |
| 63 | class PicklingError(PickleError): |
| 64 | """This exception is raised when an unpicklable object is passed to the |
| 65 | dump() method. |
| 66 | |
| 67 | """ |
| 68 | pass |
| 69 | |
| 70 | class UnpicklingError(PickleError): |
| 71 | """This exception is raised when there is a problem unpickling an object, |
| 72 | such as a security violation. |
| 73 | |
| 74 | Note that other exceptions may also be raised during unpickling, including |
| 75 | (but not necessarily limited to) AttributeError, EOFError, ImportError, |
| 76 | and IndexError. |
| 77 | |
| 78 | """ |
| 79 | pass |
Guido van Rossum | 7849da8 | 1995-03-09 14:08:35 +0000 | [diff] [blame] | 80 | |
Tim Peters | c0c12b5 | 2003-01-29 00:56:17 +0000 | [diff] [blame] | 81 | # An instance of _Stop is raised by Unpickler.load_stop() in response to |
| 82 | # the STOP opcode, passing the object that is the result of unpickling. |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 83 | class _Stop(Exception): |
| 84 | def __init__(self, value): |
| 85 | self.value = value |
| 86 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 87 | # Jython has PyStringMap; it's a dict subclass with string keys |
Jeremy Hylton | 2b9d029 | 1998-05-27 22:38:22 +0000 | [diff] [blame] | 88 | try: |
| 89 | from org.python.core import PyStringMap |
| 90 | except ImportError: |
| 91 | PyStringMap = None |
| 92 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 93 | # UnicodeType may or may not be exported (normally imported from types) |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 94 | try: |
| 95 | UnicodeType |
| 96 | except NameError: |
| 97 | UnicodeType = None |
| 98 | |
Tim Peters | 22a449a | 2003-01-27 20:16:36 +0000 | [diff] [blame] | 99 | # Pickle opcodes. See pickletools.py for extensive docs. The listing |
| 100 | # here is in kind-of alphabetical order of 1-character pickle code. |
| 101 | # pickletools groups them by purpose. |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 102 | |
Tim Peters | 22a449a | 2003-01-27 20:16:36 +0000 | [diff] [blame] | 103 | MARK = '(' # push special markobject on stack |
| 104 | STOP = '.' # every pickle ends with STOP |
| 105 | POP = '0' # discard topmost stack item |
| 106 | POP_MARK = '1' # discard stack top through topmost markobject |
| 107 | DUP = '2' # duplicate top stack item |
| 108 | FLOAT = 'F' # push float object; decimal string argument |
| 109 | INT = 'I' # push integer or bool; decimal string argument |
| 110 | BININT = 'J' # push four-byte signed int |
| 111 | BININT1 = 'K' # push 1-byte unsigned int |
| 112 | LONG = 'L' # push long; decimal string argument |
| 113 | BININT2 = 'M' # push 2-byte unsigned int |
| 114 | NONE = 'N' # push None |
| 115 | PERSID = 'P' # push persistent object; id is taken from string arg |
| 116 | BINPERSID = 'Q' # " " " ; " " " " stack |
| 117 | REDUCE = 'R' # apply callable to argtuple, both on stack |
| 118 | STRING = 'S' # push string; NL-terminated string argument |
| 119 | BINSTRING = 'T' # push string; counted binary string argument |
| 120 | SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes |
| 121 | UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument |
| 122 | BINUNICODE = 'X' # " " " ; counted UTF-8 string argument |
| 123 | APPEND = 'a' # append stack top to list below it |
| 124 | BUILD = 'b' # call __setstate__ or __dict__.update() |
| 125 | GLOBAL = 'c' # push self.find_class(modname, name); 2 string args |
| 126 | DICT = 'd' # build a dict from stack items |
| 127 | EMPTY_DICT = '}' # push empty dict |
| 128 | APPENDS = 'e' # extend list on stack by topmost stack slice |
| 129 | GET = 'g' # push item from memo on stack; index is string arg |
| 130 | BINGET = 'h' # " " " " " " ; " " 1-byte arg |
| 131 | INST = 'i' # build & push class instance |
| 132 | LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg |
| 133 | LIST = 'l' # build list from topmost stack items |
| 134 | EMPTY_LIST = ']' # push empty list |
| 135 | OBJ = 'o' # build & push class instance |
| 136 | PUT = 'p' # store stack top in memo; index is string arg |
| 137 | BINPUT = 'q' # " " " " " ; " " 1-byte arg |
| 138 | LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg |
| 139 | SETITEM = 's' # add key+value pair to dict |
| 140 | TUPLE = 't' # build tuple from topmost stack items |
| 141 | EMPTY_TUPLE = ')' # push empty tuple |
| 142 | SETITEMS = 'u' # modify dict by adding topmost key+value pairs |
| 143 | BINFLOAT = 'G' # push float; arg is 8-byte float encoding |
| 144 | |
| 145 | TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py |
| 146 | FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | 586c9e8 | 2003-01-29 06:16:12 +0000 | [diff] [blame] | 148 | # Protocol 2 |
Guido van Rossum | 5a2d8f5 | 2003-01-27 21:44:25 +0000 | [diff] [blame] | 149 | |
Tim Peters | e105478 | 2003-01-28 00:22:12 +0000 | [diff] [blame] | 150 | PROTO = '\x80' # identify pickle protocol |
| 151 | NEWOBJ = '\x81' # build object by applying cls.__new__ to argtuple |
| 152 | EXT1 = '\x82' # push object from extension registry; 1-byte index |
| 153 | EXT2 = '\x83' # ditto, but 2-byte index |
| 154 | EXT4 = '\x84' # ditto, but 4-byte index |
| 155 | TUPLE1 = '\x85' # build 1-tuple from stack top |
| 156 | TUPLE2 = '\x86' # build 2-tuple from two topmost stack items |
| 157 | TUPLE3 = '\x87' # build 3-tuple from three topmost stack items |
| 158 | NEWTRUE = '\x88' # push True |
| 159 | NEWFALSE = '\x89' # push False |
| 160 | LONG1 = '\x8a' # push long from < 256 bytes |
| 161 | LONG4 = '\x8b' # push really big long |
Guido van Rossum | 5a2d8f5 | 2003-01-27 21:44:25 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 163 | _tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3] |
| 164 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 165 | |
Skip Montanaro | 23bafc6 | 2001-02-18 03:10:09 +0000 | [diff] [blame] | 166 | __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)]) |
Neal Norwitz | d5ba4ae | 2002-02-11 18:12:06 +0000 | [diff] [blame] | 167 | del x |
Skip Montanaro | 23bafc6 | 2001-02-18 03:10:09 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | 1be3175 | 2003-01-28 15:19:53 +0000 | [diff] [blame] | 169 | |
| 170 | # Pickling machinery |
| 171 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 172 | class Pickler: |
| 173 | |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 174 | def __init__(self, file, protocol=None, bin=None): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 175 | """This takes a file-like object for writing a pickle data stream. |
| 176 | |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 177 | The optional protocol argument tells the pickler to use the |
| 178 | given protocol; supported protocols are 0, 1, 2. The default |
Guido van Rossum | 7eff63a | 2003-01-31 19:42:31 +0000 | [diff] [blame] | 179 | protocol is 0, to be backwards compatible. (Protocol 0 is the |
| 180 | only protocol that can be written to a file opened in text |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 181 | mode and read back successfully. When using a protocol higher |
| 182 | than 0, make sure the file is opened in binary mode, both when |
| 183 | pickling and unpickling.) |
Guido van Rossum | f29d3d6 | 2003-01-27 22:47:53 +0000 | [diff] [blame] | 184 | |
| 185 | Protocol 1 is more efficient than protocol 0; protocol 2 is |
Guido van Rossum | 7eff63a | 2003-01-31 19:42:31 +0000 | [diff] [blame] | 186 | more efficient than protocol 1. |
Guido van Rossum | f29d3d6 | 2003-01-27 22:47:53 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 7eff63a | 2003-01-31 19:42:31 +0000 | [diff] [blame] | 188 | Specifying a negative protocol version selects the highest |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 189 | protocol version supported. The higher the protocol used, the |
| 190 | more recent the version of Python needed to read the pickle |
| 191 | produced. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 192 | |
| 193 | The file parameter must have a write() method that accepts a single |
| 194 | string argument. It can thus be an open file object, a StringIO |
| 195 | object, or any other custom object that meets this interface. |
| 196 | |
| 197 | """ |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 198 | if protocol is not None and bin is not None: |
| 199 | raise ValueError, "can't specify both 'protocol' and 'bin'" |
Guido van Rossum | 795ea89 | 2003-02-03 16:59:48 +0000 | [diff] [blame] | 200 | if bin is not None: |
| 201 | warnings.warn("The 'bin' argument to Pickler() is deprecated", |
| 202 | PendingDeprecationWarning) |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 203 | protocol = bin |
| 204 | if protocol is None: |
| 205 | protocol = 0 |
| 206 | if protocol < 0: |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 207 | protocol = HIGHEST_PROTOCOL |
| 208 | elif not 0 <= protocol <= HIGHEST_PROTOCOL: |
| 209 | raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 210 | self.write = file.write |
| 211 | self.memo = {} |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 212 | self.proto = int(protocol) |
| 213 | self.bin = protocol >= 1 |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 214 | self.fast = 0 |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 215 | |
Fred Drake | 7f781c9 | 2002-05-01 20:33:53 +0000 | [diff] [blame] | 216 | def clear_memo(self): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 217 | """Clears the pickler's "memo". |
| 218 | |
| 219 | The memo is the data structure that remembers which objects the |
Tim Peters | b377f8a | 2003-01-28 00:23:36 +0000 | [diff] [blame] | 220 | pickler has already seen, so that shared or recursive objects are |
| 221 | pickled by reference and not by value. This method is useful when |
| 222 | re-using picklers. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 223 | |
| 224 | """ |
Fred Drake | 7f781c9 | 2002-05-01 20:33:53 +0000 | [diff] [blame] | 225 | self.memo.clear() |
| 226 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 227 | def dump(self, obj): |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 228 | """Write a pickled representation of obj to the open file.""" |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 229 | if self.proto >= 2: |
| 230 | self.write(PROTO + chr(self.proto)) |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 231 | self.save(obj) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 232 | self.write(STOP) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 233 | |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 234 | def memoize(self, obj): |
| 235 | """Store an object in the memo.""" |
| 236 | |
Tim Peters | e46b73f | 2003-01-27 21:22:10 +0000 | [diff] [blame] | 237 | # The Pickler memo is a dictionary mapping object ids to 2-tuples |
| 238 | # that contain the Unpickler memo key and the object being memoized. |
| 239 | # The memo key is written to the pickle and will become |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 240 | # the key in the Unpickler's memo. The object is stored in the |
Tim Peters | e46b73f | 2003-01-27 21:22:10 +0000 | [diff] [blame] | 241 | # Pickler memo so that transient objects are kept alive during |
| 242 | # pickling. |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 243 | |
Tim Peters | e46b73f | 2003-01-27 21:22:10 +0000 | [diff] [blame] | 244 | # The use of the Unpickler memo length as the memo key is just a |
| 245 | # convention. The only requirement is that the memo values be unique. |
| 246 | # But there appears no advantage to any other scheme, and this |
Tim Peters | cbd0a32 | 2003-01-28 00:24:43 +0000 | [diff] [blame] | 247 | # scheme allows the Unpickler memo to be implemented as a plain (but |
Tim Peters | e46b73f | 2003-01-27 21:22:10 +0000 | [diff] [blame] | 248 | # growable) array, indexed by memo key. |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 249 | if self.fast: |
| 250 | return |
Guido van Rossum | 9b40e80 | 2003-01-30 06:37:41 +0000 | [diff] [blame] | 251 | assert id(obj) not in self.memo |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 252 | memo_len = len(self.memo) |
| 253 | self.write(self.put(memo_len)) |
Tim Peters | 518df0d | 2003-01-28 01:00:38 +0000 | [diff] [blame] | 254 | self.memo[id(obj)] = memo_len, obj |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 255 | |
Tim Peters | bb38e30 | 2003-01-27 21:25:41 +0000 | [diff] [blame] | 256 | # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i. |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 257 | def put(self, i, pack=struct.pack): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 258 | if self.bin: |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 259 | if i < 256: |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 260 | return BINPUT + chr(i) |
| 261 | else: |
| 262 | return LONG_BINPUT + pack("<i", i) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 264 | return PUT + `i` + '\n' |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 265 | |
Tim Peters | bb38e30 | 2003-01-27 21:25:41 +0000 | [diff] [blame] | 266 | # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i. |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 267 | def get(self, i, pack=struct.pack): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 268 | if self.bin: |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 269 | if i < 256: |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 270 | return BINGET + chr(i) |
| 271 | else: |
| 272 | return LONG_BINGET + pack("<i", i) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 274 | return GET + `i` + '\n' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 276 | def save(self, obj): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 277 | # Check for persistent id (defined by a subclass) |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 278 | pid = self.persistent_id(obj) |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 279 | if pid: |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 280 | self.save_pers(pid) |
| 281 | return |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 283 | # Check the memo |
| 284 | x = self.memo.get(id(obj)) |
| 285 | if x: |
| 286 | self.write(self.get(x[0])) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 287 | return |
| 288 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 289 | # Check the type dispatch table |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 290 | t = type(obj) |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 291 | f = self.dispatch.get(t) |
| 292 | if f: |
| 293 | f(self, obj) # Call unbound method with explicit self |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 294 | return |
| 295 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 296 | # Check for a class with a custom metaclass; treat as regular class |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 297 | try: |
| 298 | issc = issubclass(t, TypeType) |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 299 | except TypeError: # t is not a class (old Boost; see SF #502085) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 300 | issc = 0 |
| 301 | if issc: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 302 | self.save_global(obj) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 303 | return |
| 304 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 305 | # Check copy_reg.dispatch_table |
| 306 | reduce = dispatch_table.get(t) |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 307 | if reduce: |
| 308 | rv = reduce(obj) |
| 309 | else: |
| 310 | # Check for a __reduce_ex__ method, fall back to __reduce__ |
| 311 | reduce = getattr(obj, "__reduce_ex__", None) |
| 312 | if reduce: |
| 313 | rv = reduce(self.proto) |
| 314 | else: |
| 315 | reduce = getattr(obj, "__reduce__", None) |
| 316 | if reduce: |
| 317 | rv = reduce() |
| 318 | else: |
| 319 | raise PicklingError("Can't pickle %r object: %r" % |
| 320 | (t.__name__, obj)) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 322 | # Check for string returned by reduce(), meaning "save as global" |
| 323 | if type(rv) is StringType: |
| 324 | self.save_global(obj, rv) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 325 | return |
| 326 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 327 | # Assert that reduce() returned a tuple |
| 328 | if type(rv) is not TupleType: |
| 329 | raise PicklingError("%s must return string or tuple" % reduce) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 330 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 331 | # Assert that it returned an appropriately sized tuple |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 332 | l = len(rv) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 333 | if not (2 <= l <= 5): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 334 | raise PicklingError("Tuple returned by %s must have " |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 335 | "two to five elements" % reduce) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 337 | # Save the reduce() output and finally memoize the object |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 338 | self.save_reduce(obj=obj, *rv) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 340 | def persistent_id(self, obj): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 341 | # This exists so a subclass can override it |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 342 | return None |
| 343 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 344 | def save_pers(self, pid): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 345 | # Save a persistent id reference |
Tim Peters | bd1cdb9 | 2003-01-28 01:03:10 +0000 | [diff] [blame] | 346 | if self.bin: |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 347 | self.save(pid) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 348 | self.write(BINPERSID) |
Tim Peters | bd1cdb9 | 2003-01-28 01:03:10 +0000 | [diff] [blame] | 349 | else: |
| 350 | self.write(PERSID + str(pid) + '\n') |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 351 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 352 | def save_reduce(self, func, args, state=None, |
| 353 | listitems=None, dictitems=None, obj=None): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 354 | # This API is be called by some subclasses |
| 355 | |
| 356 | # Assert that args is a tuple or None |
| 357 | if not isinstance(args, TupleType): |
| 358 | if args is None: |
| 359 | # A hack for Jim Fulton's ExtensionClass, now deprecated. |
| 360 | # See load_reduce() |
| 361 | warnings.warn("__basicnew__ special case is deprecated", |
| 362 | DeprecationWarning) |
| 363 | else: |
| 364 | raise PicklingError( |
| 365 | "args from reduce() should be a tuple") |
| 366 | |
| 367 | # Assert that func is callable |
| 368 | if not callable(func): |
| 369 | raise PicklingError("func from reduce should be callable") |
| 370 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 371 | save = self.save |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 372 | write = self.write |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | d053b4b | 2003-01-31 16:51:45 +0000 | [diff] [blame] | 374 | # Protocol 2 special case: if func's name is __newobj__, use NEWOBJ |
| 375 | if self.proto >= 2 and getattr(func, "__name__", "") == "__newobj__": |
| 376 | # A __reduce__ implementation can direct protocol 2 to |
| 377 | # use the more efficient NEWOBJ opcode, while still |
| 378 | # allowing protocol 0 and 1 to work normally. For this to |
| 379 | # work, the function returned by __reduce__ should be |
| 380 | # called __newobj__, and its first argument should be a |
| 381 | # new-style class. The implementation for __newobj__ |
| 382 | # should be as follows, although pickle has no way to |
| 383 | # verify this: |
| 384 | # |
| 385 | # def __newobj__(cls, *args): |
| 386 | # return cls.__new__(cls, *args) |
| 387 | # |
| 388 | # Protocols 0 and 1 will pickle a reference to __newobj__, |
| 389 | # while protocol 2 (and above) will pickle a reference to |
| 390 | # cls, the remaining args tuple, and the NEWOBJ code, |
| 391 | # which calls cls.__new__(cls, *args) at unpickling time |
| 392 | # (see load_newobj below). If __reduce__ returns a |
| 393 | # three-tuple, the state from the third tuple item will be |
| 394 | # pickled regardless of the protocol, calling __setstate__ |
| 395 | # at unpickling time (see load_build below). |
| 396 | # |
| 397 | # Note that no standard __newobj__ implementation exists; |
| 398 | # you have to provide your own. This is to enforce |
| 399 | # compatibility with Python 2.2 (pickles written using |
| 400 | # protocol 0 or 1 in Python 2.3 should be unpicklable by |
| 401 | # Python 2.2). |
| 402 | cls = args[0] |
| 403 | if not hasattr(cls, "__new__"): |
| 404 | raise PicklingError( |
| 405 | "args[0] from __newobj__ args has no __new__") |
Guido van Rossum | f7f4517 | 2003-01-31 17:17:49 +0000 | [diff] [blame] | 406 | if obj is not None and cls is not obj.__class__: |
| 407 | raise PicklingError( |
| 408 | "args[0] from __newobj__ args has the wrong class") |
Guido van Rossum | d053b4b | 2003-01-31 16:51:45 +0000 | [diff] [blame] | 409 | args = args[1:] |
| 410 | save(cls) |
| 411 | save(args) |
| 412 | write(NEWOBJ) |
| 413 | else: |
| 414 | save(func) |
| 415 | save(args) |
| 416 | write(REDUCE) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | f7f4517 | 2003-01-31 17:17:49 +0000 | [diff] [blame] | 418 | if obj is not None: |
| 419 | self.memoize(obj) |
| 420 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 421 | # More new special cases (that work with older protocols as |
| 422 | # well): when __reduce__ returns a tuple with 4 or 5 items, |
| 423 | # the 4th and 5th item should be iterators that provide list |
| 424 | # items and dict items (as (key, value) tuples), or None. |
| 425 | |
| 426 | if listitems is not None: |
| 427 | self._batch_appends(listitems) |
| 428 | |
| 429 | if dictitems is not None: |
| 430 | self._batch_setitems(dictitems) |
| 431 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 432 | if state is not None: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 433 | save(state) |
| 434 | write(BUILD) |
| 435 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 436 | # Methods below this point are dispatched through the dispatch table |
| 437 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 438 | dispatch = {} |
| 439 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 440 | def save_none(self, obj): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 441 | self.write(NONE) |
| 442 | dispatch[NoneType] = save_none |
| 443 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 444 | def save_bool(self, obj): |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 445 | if self.proto >= 2: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 446 | self.write(obj and NEWTRUE or NEWFALSE) |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 447 | else: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 448 | self.write(obj and TRUE or FALSE) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 449 | dispatch[bool] = save_bool |
| 450 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 451 | def save_int(self, obj, pack=struct.pack): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 452 | if self.bin: |
Tim Peters | 4471400 | 2001-04-10 05:02:52 +0000 | [diff] [blame] | 453 | # If the int is small enough to fit in a signed 4-byte 2's-comp |
| 454 | # format, we can store it more efficiently than the general |
| 455 | # case. |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 456 | # First one- and two-byte unsigned ints: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 457 | if obj >= 0: |
| 458 | if obj <= 0xff: |
| 459 | self.write(BININT1 + chr(obj)) |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 460 | return |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 461 | if obj <= 0xffff: |
Guido van Rossum | ba884f3 | 2003-01-29 20:14:23 +0000 | [diff] [blame] | 462 | self.write("%c%c%c" % (BININT2, obj&0xff, obj>>8)) |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 463 | return |
| 464 | # Next check for 4-byte signed ints: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 465 | high_bits = obj >> 31 # note that Python shift sign-extends |
Tim Peters | d95c2df | 2003-01-28 03:41:54 +0000 | [diff] [blame] | 466 | if high_bits == 0 or high_bits == -1: |
Tim Peters | 4471400 | 2001-04-10 05:02:52 +0000 | [diff] [blame] | 467 | # All high bits are copies of bit 2**31, so the value |
| 468 | # fits in a 4-byte signed int. |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 469 | self.write(BININT + pack("<i", obj)) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 470 | return |
Tim Peters | 4471400 | 2001-04-10 05:02:52 +0000 | [diff] [blame] | 471 | # Text pickle, or int too big to fit in signed 4-byte format. |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 472 | self.write(INT + `obj` + '\n') |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 473 | dispatch[IntType] = save_int |
| 474 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 475 | def save_long(self, obj, pack=struct.pack): |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 476 | if self.proto >= 2: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 477 | bytes = encode_long(obj) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 478 | n = len(bytes) |
| 479 | if n < 256: |
| 480 | self.write(LONG1 + chr(n) + bytes) |
| 481 | else: |
| 482 | self.write(LONG4 + pack("<i", n) + bytes) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 483 | return |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 484 | self.write(LONG + `obj` + '\n') |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 485 | dispatch[LongType] = save_long |
| 486 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 487 | def save_float(self, obj, pack=struct.pack): |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 488 | if self.bin: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 489 | self.write(BINFLOAT + pack('>d', obj)) |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 490 | else: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 491 | self.write(FLOAT + `obj` + '\n') |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 492 | dispatch[FloatType] = save_float |
| 493 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 494 | def save_string(self, obj, pack=struct.pack): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 495 | if self.bin: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 496 | n = len(obj) |
Tim Peters | bbf63cd | 2003-01-27 21:15:36 +0000 | [diff] [blame] | 497 | if n < 256: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 498 | self.write(SHORT_BINSTRING + chr(n) + obj) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 499 | else: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 500 | self.write(BINSTRING + pack("<i", n) + obj) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 501 | else: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 502 | self.write(STRING + `obj` + '\n') |
| 503 | self.memoize(obj) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 504 | dispatch[StringType] = save_string |
| 505 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 506 | def save_unicode(self, obj, pack=struct.pack): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 507 | if self.bin: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 508 | encoding = obj.encode('utf-8') |
Tim Peters | bbf63cd | 2003-01-27 21:15:36 +0000 | [diff] [blame] | 509 | n = len(encoding) |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 510 | self.write(BINUNICODE + pack("<i", n) + encoding) |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 511 | else: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 512 | obj = obj.replace("\\", "\\u005c") |
| 513 | obj = obj.replace("\n", "\\u000a") |
| 514 | self.write(UNICODE + obj.encode('raw-unicode-escape') + '\n') |
| 515 | self.memoize(obj) |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 516 | dispatch[UnicodeType] = save_unicode |
| 517 | |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 518 | if StringType == UnicodeType: |
| 519 | # This is true for Jython |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 520 | def save_string(self, obj, pack=struct.pack): |
| 521 | unicode = obj.isunicode() |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 522 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 523 | if self.bin: |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 524 | if unicode: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 525 | obj = obj.encode("utf-8") |
| 526 | l = len(obj) |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 527 | if l < 256 and not unicode: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 528 | self.write(SHORT_BINSTRING + chr(l) + obj) |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 529 | else: |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 530 | s = pack("<i", l) |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 531 | if unicode: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 532 | self.write(BINUNICODE + s + obj) |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 533 | else: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 534 | self.write(BINSTRING + s + obj) |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 535 | else: |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 536 | if unicode: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 537 | obj = obj.replace("\\", "\\u005c") |
| 538 | obj = obj.replace("\n", "\\u000a") |
| 539 | obj = obj.encode('raw-unicode-escape') |
| 540 | self.write(UNICODE + obj + '\n') |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 541 | else: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 542 | self.write(STRING + `obj` + '\n') |
| 543 | self.memoize(obj) |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 544 | dispatch[StringType] = save_string |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 545 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 546 | def save_tuple(self, obj): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 547 | write = self.write |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 548 | proto = self.proto |
| 549 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 550 | n = len(obj) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 551 | if n == 0: |
| 552 | if proto: |
| 553 | write(EMPTY_TUPLE) |
| 554 | else: |
| 555 | write(MARK + TUPLE) |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 556 | return |
| 557 | |
| 558 | save = self.save |
| 559 | memo = self.memo |
| 560 | if n <= 3 and proto >= 2: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 561 | for element in obj: |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 562 | save(element) |
| 563 | # Subtle. Same as in the big comment below. |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 564 | if id(obj) in memo: |
| 565 | get = self.get(memo[id(obj)][0]) |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 566 | write(POP * n + get) |
| 567 | else: |
| 568 | write(_tuplesize2code[n]) |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 569 | self.memoize(obj) |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 570 | return |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 571 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 572 | # proto 0 or proto 1 and tuple isn't empty, or proto > 1 and tuple |
Tim Peters | ff57bff | 2003-01-28 05:34:53 +0000 | [diff] [blame] | 573 | # has more than 3 elements. |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 574 | write(MARK) |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 575 | for element in obj: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 576 | save(element) |
| 577 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 578 | if id(obj) in memo: |
Tim Peters | f558da0 | 2003-01-28 02:09:55 +0000 | [diff] [blame] | 579 | # Subtle. d was not in memo when we entered save_tuple(), so |
| 580 | # the process of saving the tuple's elements must have saved |
| 581 | # the tuple itself: the tuple is recursive. The proper action |
| 582 | # now is to throw away everything we put on the stack, and |
| 583 | # simply GET the tuple (it's already constructed). This check |
| 584 | # could have been done in the "for element" loop instead, but |
| 585 | # recursive tuples are a rare thing. |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 586 | get = self.get(memo[id(obj)][0]) |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 587 | if proto: |
Tim Peters | f558da0 | 2003-01-28 02:09:55 +0000 | [diff] [blame] | 588 | write(POP_MARK + get) |
| 589 | else: # proto 0 -- POP_MARK not available |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 590 | write(POP * (n+1) + get) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 591 | return |
| 592 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 593 | # No recursion. |
Tim Peters | 518df0d | 2003-01-28 01:00:38 +0000 | [diff] [blame] | 594 | self.write(TUPLE) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 595 | self.memoize(obj) |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 597 | dispatch[TupleType] = save_tuple |
| 598 | |
Tim Peters | a6ae9a2 | 2003-01-28 16:58:41 +0000 | [diff] [blame] | 599 | # save_empty_tuple() isn't used by anything in Python 2.3. However, I |
| 600 | # found a Pickler subclass in Zope3 that calls it, so it's not harmless |
| 601 | # to remove it. |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 602 | def save_empty_tuple(self, obj): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 603 | self.write(EMPTY_TUPLE) |
| 604 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 605 | def save_list(self, obj): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 606 | write = self.write |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 607 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 608 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 609 | write(EMPTY_LIST) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 610 | else: # proto 0 -- can't use EMPTY_LIST |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 611 | write(MARK + LIST) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 612 | |
| 613 | self.memoize(obj) |
| 614 | self._batch_appends(iter(obj)) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 615 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 616 | dispatch[ListType] = save_list |
| 617 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 618 | # Keep in synch with cPickle's BATCHSIZE. Nothing will break if it gets |
| 619 | # out of synch, though. |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 620 | _BATCHSIZE = 1000 |
| 621 | |
| 622 | def _batch_appends(self, items): |
| 623 | # Helper to batch up APPENDS sequences |
| 624 | save = self.save |
| 625 | write = self.write |
| 626 | |
| 627 | if not self.bin: |
| 628 | for x in items: |
| 629 | save(x) |
| 630 | write(APPEND) |
| 631 | return |
| 632 | |
| 633 | r = xrange(self._BATCHSIZE) |
| 634 | while items is not None: |
| 635 | tmp = [] |
| 636 | for i in r: |
| 637 | try: |
Guido van Rossum | 5aac4e6 | 2003-02-06 22:57:00 +0000 | [diff] [blame] | 638 | x = items.next() |
| 639 | tmp.append(x) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 640 | except StopIteration: |
| 641 | items = None |
| 642 | break |
| 643 | n = len(tmp) |
| 644 | if n > 1: |
| 645 | write(MARK) |
| 646 | for x in tmp: |
| 647 | save(x) |
| 648 | write(APPENDS) |
| 649 | elif n: |
| 650 | save(tmp[0]) |
| 651 | write(APPEND) |
| 652 | # else tmp is empty, and we're done |
| 653 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 654 | def save_dict(self, obj): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 655 | write = self.write |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 656 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 657 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 658 | write(EMPTY_DICT) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 659 | else: # proto 0 -- can't use EMPTY_DICT |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 660 | write(MARK + DICT) |
| 661 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 662 | self.memoize(obj) |
| 663 | self._batch_setitems(obj.iteritems()) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 664 | |
| 665 | dispatch[DictionaryType] = save_dict |
Jeremy Hylton | 2b9d029 | 1998-05-27 22:38:22 +0000 | [diff] [blame] | 666 | if not PyStringMap is None: |
| 667 | dispatch[PyStringMap] = save_dict |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 668 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 669 | def _batch_setitems(self, items): |
| 670 | # Helper to batch up SETITEMS sequences; proto >= 1 only |
| 671 | save = self.save |
| 672 | write = self.write |
| 673 | |
| 674 | if not self.bin: |
| 675 | for k, v in items: |
| 676 | save(k) |
| 677 | save(v) |
| 678 | write(SETITEM) |
| 679 | return |
| 680 | |
| 681 | r = xrange(self._BATCHSIZE) |
| 682 | while items is not None: |
| 683 | tmp = [] |
| 684 | for i in r: |
| 685 | try: |
| 686 | tmp.append(items.next()) |
| 687 | except StopIteration: |
| 688 | items = None |
| 689 | break |
| 690 | n = len(tmp) |
| 691 | if n > 1: |
| 692 | write(MARK) |
| 693 | for k, v in tmp: |
| 694 | save(k) |
| 695 | save(v) |
| 696 | write(SETITEMS) |
| 697 | elif n: |
| 698 | k, v = tmp[0] |
| 699 | save(k) |
| 700 | save(v) |
| 701 | write(SETITEM) |
| 702 | # else tmp is empty, and we're done |
| 703 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 704 | def save_inst(self, obj): |
| 705 | cls = obj.__class__ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 706 | |
| 707 | memo = self.memo |
| 708 | write = self.write |
| 709 | save = self.save |
| 710 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 711 | if hasattr(obj, '__getinitargs__'): |
| 712 | args = obj.__getinitargs__() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 713 | len(args) # XXX Assert it's a sequence |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 714 | _keep_alive(args, memo) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 715 | else: |
| 716 | args = () |
| 717 | |
| 718 | write(MARK) |
| 719 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 720 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 721 | save(cls) |
Tim Peters | 3b76983 | 2003-01-28 03:51:36 +0000 | [diff] [blame] | 722 | for arg in args: |
| 723 | save(arg) |
| 724 | write(OBJ) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 725 | else: |
Tim Peters | 3b76983 | 2003-01-28 03:51:36 +0000 | [diff] [blame] | 726 | for arg in args: |
| 727 | save(arg) |
| 728 | write(INST + cls.__module__ + '\n' + cls.__name__ + '\n') |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 729 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 730 | self.memoize(obj) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 731 | |
| 732 | try: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 733 | getstate = obj.__getstate__ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 734 | except AttributeError: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 735 | stuff = obj.__dict__ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 736 | else: |
| 737 | stuff = getstate() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 738 | _keep_alive(stuff, memo) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 739 | save(stuff) |
| 740 | write(BUILD) |
Tim Peters | 3b76983 | 2003-01-28 03:51:36 +0000 | [diff] [blame] | 741 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 742 | dispatch[InstanceType] = save_inst |
| 743 | |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 744 | def save_global(self, obj, name=None, pack=struct.pack): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 745 | write = self.write |
| 746 | memo = self.memo |
| 747 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 748 | if name is None: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 749 | name = obj.__name__ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 750 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 751 | module = getattr(obj, "__module__", None) |
| 752 | if module is None: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 753 | module = whichmodule(obj, name) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 754 | |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 755 | try: |
| 756 | __import__(module) |
| 757 | mod = sys.modules[module] |
| 758 | klass = getattr(mod, name) |
| 759 | except (ImportError, KeyError, AttributeError): |
| 760 | raise PicklingError( |
| 761 | "Can't pickle %r: it's not found as %s.%s" % |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 762 | (obj, module, name)) |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 763 | else: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 764 | if klass is not obj: |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 765 | raise PicklingError( |
| 766 | "Can't pickle %r: it's not the same object as %s.%s" % |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 767 | (obj, module, name)) |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 768 | |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 769 | if self.proto >= 2: |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 770 | code = _extension_registry.get((module, name)) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 771 | if code: |
| 772 | assert code > 0 |
| 773 | if code <= 0xff: |
| 774 | write(EXT1 + chr(code)) |
| 775 | elif code <= 0xffff: |
Guido van Rossum | ba884f3 | 2003-01-29 20:14:23 +0000 | [diff] [blame] | 776 | write("%c%c%c" % (EXT2, code&0xff, code>>8)) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 777 | else: |
| 778 | write(EXT4 + pack("<i", code)) |
| 779 | return |
| 780 | |
Tim Peters | 518df0d | 2003-01-28 01:00:38 +0000 | [diff] [blame] | 781 | write(GLOBAL + module + '\n' + name + '\n') |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 782 | self.memoize(obj) |
Tim Peters | 3b76983 | 2003-01-28 03:51:36 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 784 | dispatch[ClassType] = save_global |
| 785 | dispatch[FunctionType] = save_global |
| 786 | dispatch[BuiltinFunctionType] = save_global |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 787 | dispatch[TypeType] = save_global |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 788 | |
Guido van Rossum | 1be3175 | 2003-01-28 15:19:53 +0000 | [diff] [blame] | 789 | # Pickling helpers |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 790 | |
Guido van Rossum | 5ed5c4c | 1997-09-03 00:23:54 +0000 | [diff] [blame] | 791 | def _keep_alive(x, memo): |
| 792 | """Keeps a reference to the object x in the memo. |
| 793 | |
| 794 | Because we remember objects by their id, we have |
| 795 | to assure that possibly temporary objects are kept |
| 796 | alive by referencing them. |
| 797 | We store a reference at the id of the memo, which should |
| 798 | normally not be used unless someone tries to deepcopy |
| 799 | the memo itself... |
| 800 | """ |
| 801 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 802 | memo[id(memo)].append(x) |
Guido van Rossum | 5ed5c4c | 1997-09-03 00:23:54 +0000 | [diff] [blame] | 803 | except KeyError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 804 | # aha, this is the first one :-) |
| 805 | memo[id(memo)]=[x] |
Guido van Rossum | 5ed5c4c | 1997-09-03 00:23:54 +0000 | [diff] [blame] | 806 | |
| 807 | |
Tim Peters | c0c12b5 | 2003-01-29 00:56:17 +0000 | [diff] [blame] | 808 | # A cache for whichmodule(), mapping a function object to the name of |
| 809 | # the module in which the function was found. |
| 810 | |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 811 | classmap = {} # called classmap for backwards compatibility |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 812 | |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 813 | def whichmodule(func, funcname): |
| 814 | """Figure out the module in which a function occurs. |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 815 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 816 | Search sys.modules for the module. |
| 817 | Cache in classmap. |
| 818 | Return a module name. |
Tim Peters | c0c12b5 | 2003-01-29 00:56:17 +0000 | [diff] [blame] | 819 | If the function cannot be found, return "__main__". |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 820 | """ |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 821 | # Python functions should always get an __module__ from their globals. |
| 822 | mod = getattr(func, "__module__", None) |
| 823 | if mod is not None: |
| 824 | return mod |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 825 | if func in classmap: |
| 826 | return classmap[func] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 827 | |
| 828 | for name, module in sys.modules.items(): |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 829 | if module is None: |
Jeremy Hylton | 065a5ab | 2002-09-19 22:57:26 +0000 | [diff] [blame] | 830 | continue # skip dummy package entries |
Jeremy Hylton | cc1fccb | 2003-02-06 16:23:01 +0000 | [diff] [blame] | 831 | if name != '__main__' and getattr(module, funcname, None) is func: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 832 | break |
| 833 | else: |
| 834 | name = '__main__' |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 835 | classmap[func] = name |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 836 | return name |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 837 | |
| 838 | |
Guido van Rossum | 1be3175 | 2003-01-28 15:19:53 +0000 | [diff] [blame] | 839 | # Unpickling machinery |
| 840 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 841 | class Unpickler: |
| 842 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 843 | def __init__(self, file): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 844 | """This takes a file-like object for reading a pickle data stream. |
| 845 | |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 846 | The protocol version of the pickle is detected automatically, so no |
| 847 | proto argument is needed. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 848 | |
| 849 | The file-like object must have two methods, a read() method that |
| 850 | takes an integer argument, and a readline() method that requires no |
| 851 | arguments. Both methods should return a string. Thus file-like |
| 852 | object can be a file object opened for reading, a StringIO object, |
| 853 | or any other custom object that meets this interface. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 854 | """ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 855 | self.readline = file.readline |
| 856 | self.read = file.read |
| 857 | self.memo = {} |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 858 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 859 | def load(self): |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 860 | """Read a pickled object representation from the open file. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 861 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 862 | Return the reconstituted object hierarchy specified in the file. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 863 | """ |
Jeremy Hylton | 20747fa | 2001-11-09 16:15:04 +0000 | [diff] [blame] | 864 | self.mark = object() # any new unique object |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 865 | self.stack = [] |
| 866 | self.append = self.stack.append |
| 867 | read = self.read |
| 868 | dispatch = self.dispatch |
| 869 | try: |
| 870 | while 1: |
| 871 | key = read(1) |
| 872 | dispatch[key](self) |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 873 | except _Stop, stopinst: |
| 874 | return stopinst.value |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 875 | |
Tim Peters | c23d18a | 2003-01-28 01:41:51 +0000 | [diff] [blame] | 876 | # Return largest index k such that self.stack[k] is self.mark. |
| 877 | # If the stack doesn't contain a mark, eventually raises IndexError. |
| 878 | # This could be sped by maintaining another stack, of indices at which |
| 879 | # the mark appears. For that matter, the latter stack would suffice, |
| 880 | # and we wouldn't need to push mark objects on self.stack at all. |
| 881 | # Doing so is probably a good thing, though, since if the pickle is |
| 882 | # corrupt (or hostile) we may get a clue from finding self.mark embedded |
| 883 | # in unpickled objects. |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 884 | def marker(self): |
| 885 | stack = self.stack |
| 886 | mark = self.mark |
| 887 | k = len(stack)-1 |
| 888 | while stack[k] is not mark: k = k-1 |
| 889 | return k |
| 890 | |
| 891 | dispatch = {} |
| 892 | |
| 893 | def load_eof(self): |
| 894 | raise EOFError |
| 895 | dispatch[''] = load_eof |
| 896 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 897 | def load_proto(self): |
| 898 | proto = ord(self.read(1)) |
| 899 | if not 0 <= proto <= 2: |
| 900 | raise ValueError, "unsupported pickle protocol: %d" % proto |
| 901 | dispatch[PROTO] = load_proto |
| 902 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 903 | def load_persid(self): |
| 904 | pid = self.readline()[:-1] |
| 905 | self.append(self.persistent_load(pid)) |
| 906 | dispatch[PERSID] = load_persid |
| 907 | |
| 908 | def load_binpersid(self): |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 909 | pid = self.stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 910 | self.append(self.persistent_load(pid)) |
| 911 | dispatch[BINPERSID] = load_binpersid |
| 912 | |
| 913 | def load_none(self): |
| 914 | self.append(None) |
| 915 | dispatch[NONE] = load_none |
| 916 | |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 917 | def load_false(self): |
| 918 | self.append(False) |
| 919 | dispatch[NEWFALSE] = load_false |
| 920 | |
| 921 | def load_true(self): |
| 922 | self.append(True) |
| 923 | dispatch[NEWTRUE] = load_true |
| 924 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 925 | def load_int(self): |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 926 | data = self.readline() |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 927 | if data == FALSE[1:]: |
| 928 | val = False |
| 929 | elif data == TRUE[1:]: |
| 930 | val = True |
| 931 | else: |
| 932 | try: |
| 933 | val = int(data) |
| 934 | except ValueError: |
| 935 | val = long(data) |
| 936 | self.append(val) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 937 | dispatch[INT] = load_int |
| 938 | |
| 939 | def load_binint(self): |
| 940 | self.append(mloads('i' + self.read(4))) |
| 941 | dispatch[BININT] = load_binint |
| 942 | |
| 943 | def load_binint1(self): |
Tim Peters | bbf63cd | 2003-01-27 21:15:36 +0000 | [diff] [blame] | 944 | self.append(ord(self.read(1))) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 945 | dispatch[BININT1] = load_binint1 |
| 946 | |
| 947 | def load_binint2(self): |
| 948 | self.append(mloads('i' + self.read(2) + '\000\000')) |
| 949 | dispatch[BININT2] = load_binint2 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 950 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 951 | def load_long(self): |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 952 | self.append(long(self.readline()[:-1], 0)) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 953 | dispatch[LONG] = load_long |
| 954 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 955 | def load_long1(self): |
| 956 | n = ord(self.read(1)) |
| 957 | bytes = self.read(n) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 958 | self.append(decode_long(bytes)) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 959 | dispatch[LONG1] = load_long1 |
| 960 | |
| 961 | def load_long4(self): |
| 962 | n = mloads('i' + self.read(4)) |
| 963 | bytes = self.read(n) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 964 | self.append(decode_long(bytes)) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 965 | dispatch[LONG4] = load_long4 |
| 966 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 967 | def load_float(self): |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 968 | self.append(float(self.readline()[:-1])) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 969 | dispatch[FLOAT] = load_float |
| 970 | |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 971 | def load_binfloat(self, unpack=struct.unpack): |
| 972 | self.append(unpack('>d', self.read(8))[0]) |
| 973 | dispatch[BINFLOAT] = load_binfloat |
| 974 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 975 | def load_string(self): |
Jeremy Hylton | be467e5 | 2000-09-15 15:14:51 +0000 | [diff] [blame] | 976 | rep = self.readline()[:-1] |
Tim Peters | ad5a771 | 2003-01-28 16:23:33 +0000 | [diff] [blame] | 977 | for q in "\"'": # double or single quote |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 978 | if rep.startswith(q): |
| 979 | if not rep.endswith(q): |
| 980 | raise ValueError, "insecure string pickle" |
| 981 | rep = rep[len(q):-len(q)] |
| 982 | break |
| 983 | else: |
Jeremy Hylton | be467e5 | 2000-09-15 15:14:51 +0000 | [diff] [blame] | 984 | raise ValueError, "insecure string pickle" |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 985 | self.append(rep.decode("string-escape")) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 986 | dispatch[STRING] = load_string |
| 987 | |
| 988 | def load_binstring(self): |
| 989 | len = mloads('i' + self.read(4)) |
| 990 | self.append(self.read(len)) |
| 991 | dispatch[BINSTRING] = load_binstring |
| 992 | |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 993 | def load_unicode(self): |
| 994 | self.append(unicode(self.readline()[:-1],'raw-unicode-escape')) |
| 995 | dispatch[UNICODE] = load_unicode |
| 996 | |
| 997 | def load_binunicode(self): |
| 998 | len = mloads('i' + self.read(4)) |
| 999 | self.append(unicode(self.read(len),'utf-8')) |
| 1000 | dispatch[BINUNICODE] = load_binunicode |
| 1001 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1002 | def load_short_binstring(self): |
Tim Peters | bbf63cd | 2003-01-27 21:15:36 +0000 | [diff] [blame] | 1003 | len = ord(self.read(1)) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1004 | self.append(self.read(len)) |
| 1005 | dispatch[SHORT_BINSTRING] = load_short_binstring |
| 1006 | |
| 1007 | def load_tuple(self): |
| 1008 | k = self.marker() |
| 1009 | self.stack[k:] = [tuple(self.stack[k+1:])] |
| 1010 | dispatch[TUPLE] = load_tuple |
| 1011 | |
| 1012 | def load_empty_tuple(self): |
| 1013 | self.stack.append(()) |
| 1014 | dispatch[EMPTY_TUPLE] = load_empty_tuple |
| 1015 | |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 1016 | def load_tuple1(self): |
| 1017 | self.stack[-1] = (self.stack[-1],) |
| 1018 | dispatch[TUPLE1] = load_tuple1 |
| 1019 | |
| 1020 | def load_tuple2(self): |
| 1021 | self.stack[-2:] = [(self.stack[-2], self.stack[-1])] |
| 1022 | dispatch[TUPLE2] = load_tuple2 |
| 1023 | |
| 1024 | def load_tuple3(self): |
| 1025 | self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])] |
| 1026 | dispatch[TUPLE3] = load_tuple3 |
| 1027 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1028 | def load_empty_list(self): |
| 1029 | self.stack.append([]) |
| 1030 | dispatch[EMPTY_LIST] = load_empty_list |
| 1031 | |
| 1032 | def load_empty_dictionary(self): |
| 1033 | self.stack.append({}) |
| 1034 | dispatch[EMPTY_DICT] = load_empty_dictionary |
| 1035 | |
| 1036 | def load_list(self): |
| 1037 | k = self.marker() |
| 1038 | self.stack[k:] = [self.stack[k+1:]] |
| 1039 | dispatch[LIST] = load_list |
| 1040 | |
| 1041 | def load_dict(self): |
| 1042 | k = self.marker() |
| 1043 | d = {} |
| 1044 | items = self.stack[k+1:] |
| 1045 | for i in range(0, len(items), 2): |
| 1046 | key = items[i] |
| 1047 | value = items[i+1] |
| 1048 | d[key] = value |
| 1049 | self.stack[k:] = [d] |
| 1050 | dispatch[DICT] = load_dict |
| 1051 | |
Tim Peters | d01c1e9 | 2003-01-30 15:41:46 +0000 | [diff] [blame] | 1052 | # INST and OBJ differ only in how they get a class object. It's not |
| 1053 | # only sensible to do the rest in a common routine, the two routines |
| 1054 | # previously diverged and grew different bugs. |
| 1055 | # klass is the class to instantiate, and k points to the topmost mark |
| 1056 | # object, following which are the arguments for klass.__init__. |
| 1057 | def _instantiate(self, klass, k): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1058 | args = tuple(self.stack[k+1:]) |
| 1059 | del self.stack[k:] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1060 | instantiated = 0 |
Tim Peters | d01c1e9 | 2003-01-30 15:41:46 +0000 | [diff] [blame] | 1061 | if (not args and |
| 1062 | type(klass) is ClassType and |
| 1063 | not hasattr(klass, "__getinitargs__")): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1064 | try: |
| 1065 | value = _EmptyClass() |
| 1066 | value.__class__ = klass |
Guido van Rossum | b19e2a3 | 1998-04-13 18:08:45 +0000 | [diff] [blame] | 1067 | instantiated = 1 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1068 | except RuntimeError: |
| 1069 | # In restricted execution, assignment to inst.__class__ is |
| 1070 | # prohibited |
| 1071 | pass |
| 1072 | if not instantiated: |
Guido van Rossum | 743d17e | 1998-09-15 20:25:57 +0000 | [diff] [blame] | 1073 | try: |
Guido van Rossum | b26a97a | 2003-01-28 22:29:13 +0000 | [diff] [blame] | 1074 | value = klass(*args) |
Guido van Rossum | 743d17e | 1998-09-15 20:25:57 +0000 | [diff] [blame] | 1075 | except TypeError, err: |
| 1076 | raise TypeError, "in constructor for %s: %s" % ( |
| 1077 | klass.__name__, str(err)), sys.exc_info()[2] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1078 | self.append(value) |
Tim Peters | d01c1e9 | 2003-01-30 15:41:46 +0000 | [diff] [blame] | 1079 | |
| 1080 | def load_inst(self): |
| 1081 | module = self.readline()[:-1] |
| 1082 | name = self.readline()[:-1] |
| 1083 | klass = self.find_class(module, name) |
| 1084 | self._instantiate(klass, self.marker()) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1085 | dispatch[INST] = load_inst |
| 1086 | |
| 1087 | def load_obj(self): |
Tim Peters | d01c1e9 | 2003-01-30 15:41:46 +0000 | [diff] [blame] | 1088 | # Stack is ... markobject classobject arg1 arg2 ... |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1089 | k = self.marker() |
Tim Peters | d01c1e9 | 2003-01-30 15:41:46 +0000 | [diff] [blame] | 1090 | klass = self.stack.pop(k+1) |
| 1091 | self._instantiate(klass, k) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1092 | dispatch[OBJ] = load_obj |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1093 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 1094 | def load_newobj(self): |
| 1095 | args = self.stack.pop() |
| 1096 | cls = self.stack[-1] |
| 1097 | obj = cls.__new__(cls, *args) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1098 | self.stack[-1] = obj |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 1099 | dispatch[NEWOBJ] = load_newobj |
| 1100 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1101 | def load_global(self): |
| 1102 | module = self.readline()[:-1] |
| 1103 | name = self.readline()[:-1] |
| 1104 | klass = self.find_class(module, name) |
| 1105 | self.append(klass) |
| 1106 | dispatch[GLOBAL] = load_global |
| 1107 | |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1108 | def load_ext1(self): |
| 1109 | code = ord(self.read(1)) |
| 1110 | self.get_extension(code) |
| 1111 | dispatch[EXT1] = load_ext1 |
| 1112 | |
| 1113 | def load_ext2(self): |
| 1114 | code = mloads('i' + self.read(2) + '\000\000') |
| 1115 | self.get_extension(code) |
| 1116 | dispatch[EXT2] = load_ext2 |
| 1117 | |
| 1118 | def load_ext4(self): |
| 1119 | code = mloads('i' + self.read(4)) |
| 1120 | self.get_extension(code) |
| 1121 | dispatch[EXT4] = load_ext4 |
| 1122 | |
| 1123 | def get_extension(self, code): |
| 1124 | nil = [] |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 1125 | obj = _extension_cache.get(code, nil) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1126 | if obj is not nil: |
| 1127 | self.append(obj) |
| 1128 | return |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 1129 | key = _inverted_registry.get(code) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1130 | if not key: |
| 1131 | raise ValueError("unregistered extension code %d" % code) |
| 1132 | obj = self.find_class(*key) |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 1133 | _extension_cache[code] = obj |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1134 | self.append(obj) |
| 1135 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1136 | def find_class(self, module, name): |
Guido van Rossum | b26a97a | 2003-01-28 22:29:13 +0000 | [diff] [blame] | 1137 | # Subclasses may override this |
Barry Warsaw | bf4d959 | 2001-11-15 23:42:58 +0000 | [diff] [blame] | 1138 | __import__(module) |
| 1139 | mod = sys.modules[module] |
| 1140 | klass = getattr(mod, name) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1141 | return klass |
| 1142 | |
| 1143 | def load_reduce(self): |
| 1144 | stack = self.stack |
Guido van Rossum | b26a97a | 2003-01-28 22:29:13 +0000 | [diff] [blame] | 1145 | args = stack.pop() |
| 1146 | func = stack[-1] |
| 1147 | if args is None: |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 1148 | # A hack for Jim Fulton's ExtensionClass, now deprecated |
| 1149 | warnings.warn("__basicnew__ special case is deprecated", |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 1150 | DeprecationWarning) |
Guido van Rossum | b26a97a | 2003-01-28 22:29:13 +0000 | [diff] [blame] | 1151 | value = func.__basicnew__() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1152 | else: |
Guido van Rossum | b26a97a | 2003-01-28 22:29:13 +0000 | [diff] [blame] | 1153 | value = func(*args) |
| 1154 | stack[-1] = value |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1155 | dispatch[REDUCE] = load_reduce |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1156 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1157 | def load_pop(self): |
| 1158 | del self.stack[-1] |
| 1159 | dispatch[POP] = load_pop |
Guido van Rossum | 7b5430f | 1995-03-04 22:25:21 +0000 | [diff] [blame] | 1160 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1161 | def load_pop_mark(self): |
| 1162 | k = self.marker() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1163 | del self.stack[k:] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1164 | dispatch[POP_MARK] = load_pop_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1165 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1166 | def load_dup(self): |
Guido van Rossum | b1062fc | 1998-03-31 17:00:46 +0000 | [diff] [blame] | 1167 | self.append(self.stack[-1]) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1168 | dispatch[DUP] = load_dup |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1169 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1170 | def load_get(self): |
| 1171 | self.append(self.memo[self.readline()[:-1]]) |
| 1172 | dispatch[GET] = load_get |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 1173 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1174 | def load_binget(self): |
Tim Peters | bbf63cd | 2003-01-27 21:15:36 +0000 | [diff] [blame] | 1175 | i = ord(self.read(1)) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1176 | self.append(self.memo[`i`]) |
| 1177 | dispatch[BINGET] = load_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 1178 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1179 | def load_long_binget(self): |
| 1180 | i = mloads('i' + self.read(4)) |
| 1181 | self.append(self.memo[`i`]) |
| 1182 | dispatch[LONG_BINGET] = load_long_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 1183 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1184 | def load_put(self): |
| 1185 | self.memo[self.readline()[:-1]] = self.stack[-1] |
| 1186 | dispatch[PUT] = load_put |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1187 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1188 | def load_binput(self): |
Tim Peters | bbf63cd | 2003-01-27 21:15:36 +0000 | [diff] [blame] | 1189 | i = ord(self.read(1)) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1190 | self.memo[`i`] = self.stack[-1] |
| 1191 | dispatch[BINPUT] = load_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1192 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1193 | def load_long_binput(self): |
| 1194 | i = mloads('i' + self.read(4)) |
| 1195 | self.memo[`i`] = self.stack[-1] |
| 1196 | dispatch[LONG_BINPUT] = load_long_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1197 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1198 | def load_append(self): |
| 1199 | stack = self.stack |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1200 | value = stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1201 | list = stack[-1] |
| 1202 | list.append(value) |
| 1203 | dispatch[APPEND] = load_append |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1204 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1205 | def load_appends(self): |
| 1206 | stack = self.stack |
| 1207 | mark = self.marker() |
| 1208 | list = stack[mark - 1] |
Tim Peters | 209ad95 | 2003-01-28 01:44:45 +0000 | [diff] [blame] | 1209 | list.extend(stack[mark + 1:]) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1210 | del stack[mark:] |
| 1211 | dispatch[APPENDS] = load_appends |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1212 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1213 | def load_setitem(self): |
| 1214 | stack = self.stack |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1215 | value = stack.pop() |
| 1216 | key = stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1217 | dict = stack[-1] |
| 1218 | dict[key] = value |
| 1219 | dispatch[SETITEM] = load_setitem |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1220 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1221 | def load_setitems(self): |
| 1222 | stack = self.stack |
| 1223 | mark = self.marker() |
| 1224 | dict = stack[mark - 1] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1225 | for i in range(mark + 1, len(stack), 2): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1226 | dict[stack[i]] = stack[i + 1] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1227 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1228 | del stack[mark:] |
| 1229 | dispatch[SETITEMS] = load_setitems |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1230 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1231 | def load_build(self): |
| 1232 | stack = self.stack |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1233 | state = stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1234 | inst = stack[-1] |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1235 | setstate = getattr(inst, "__setstate__", None) |
| 1236 | if setstate: |
| 1237 | setstate(state) |
| 1238 | return |
| 1239 | slotstate = None |
| 1240 | if isinstance(state, tuple) and len(state) == 2: |
| 1241 | state, slotstate = state |
| 1242 | if state: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1243 | try: |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1244 | inst.__dict__.update(state) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1245 | except RuntimeError: |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1246 | # XXX In restricted execution, the instance's __dict__ |
| 1247 | # is not accessible. Use the old way of unpickling |
| 1248 | # the instance variables. This is a semantic |
| 1249 | # difference when unpickling in restricted |
| 1250 | # vs. unrestricted modes. |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1251 | # Note, however, that cPickle has never tried to do the |
| 1252 | # .update() business, and always uses |
| 1253 | # PyObject_SetItem(inst.__dict__, key, value) in a |
| 1254 | # loop over state.items(). |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1255 | for k, v in state.items(): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1256 | setattr(inst, k, v) |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1257 | if slotstate: |
| 1258 | for k, v in slotstate.items(): |
| 1259 | setattr(inst, k, v) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1260 | dispatch[BUILD] = load_build |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1261 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1262 | def load_mark(self): |
| 1263 | self.append(self.mark) |
| 1264 | dispatch[MARK] = load_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1265 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1266 | def load_stop(self): |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1267 | value = self.stack.pop() |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 1268 | raise _Stop(value) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1269 | dispatch[STOP] = load_stop |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1270 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 1271 | # Helper class for load_inst/load_obj |
| 1272 | |
| 1273 | class _EmptyClass: |
| 1274 | pass |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1275 | |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1276 | # Encode/decode longs in linear time. |
| 1277 | |
| 1278 | import binascii as _binascii |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1279 | |
| 1280 | def encode_long(x): |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1281 | r"""Encode a long to a two's complement little-endian binary string. |
Tim Peters | 4b23f2b | 2003-01-31 16:43:39 +0000 | [diff] [blame] | 1282 | Note that 0L is a special case, returning an empty string, to save a |
| 1283 | byte in the LONG1 pickling context. |
| 1284 | |
| 1285 | >>> encode_long(0L) |
| 1286 | '' |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1287 | >>> encode_long(255L) |
| 1288 | '\xff\x00' |
| 1289 | >>> encode_long(32767L) |
| 1290 | '\xff\x7f' |
| 1291 | >>> encode_long(-256L) |
| 1292 | '\x00\xff' |
| 1293 | >>> encode_long(-32768L) |
| 1294 | '\x00\x80' |
| 1295 | >>> encode_long(-128L) |
| 1296 | '\x80' |
| 1297 | >>> encode_long(127L) |
| 1298 | '\x7f' |
| 1299 | >>> |
| 1300 | """ |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1301 | |
| 1302 | if x == 0: |
Tim Peters | 4b23f2b | 2003-01-31 16:43:39 +0000 | [diff] [blame] | 1303 | return '' |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1304 | if x > 0: |
| 1305 | ashex = hex(x) |
| 1306 | assert ashex.startswith("0x") |
| 1307 | njunkchars = 2 + ashex.endswith('L') |
| 1308 | nibbles = len(ashex) - njunkchars |
| 1309 | if nibbles & 1: |
| 1310 | # need an even # of nibbles for unhexlify |
| 1311 | ashex = "0x0" + ashex[2:] |
Tim Peters | 4b23f2b | 2003-01-31 16:43:39 +0000 | [diff] [blame] | 1312 | elif int(ashex[2], 16) >= 8: |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1313 | # "looks negative", so need a byte of sign bits |
| 1314 | ashex = "0x00" + ashex[2:] |
| 1315 | else: |
| 1316 | # Build the 256's-complement: (1L << nbytes) + x. The trick is |
| 1317 | # to find the number of bytes in linear time (although that should |
| 1318 | # really be a constant-time task). |
| 1319 | ashex = hex(-x) |
| 1320 | assert ashex.startswith("0x") |
| 1321 | njunkchars = 2 + ashex.endswith('L') |
| 1322 | nibbles = len(ashex) - njunkchars |
| 1323 | if nibbles & 1: |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1324 | # Extend to a full byte. |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1325 | nibbles += 1 |
Tim Peters | 4b23f2b | 2003-01-31 16:43:39 +0000 | [diff] [blame] | 1326 | nbits = nibbles * 4 |
| 1327 | x += 1L << nbits |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1328 | assert x > 0 |
| 1329 | ashex = hex(x) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1330 | njunkchars = 2 + ashex.endswith('L') |
| 1331 | newnibbles = len(ashex) - njunkchars |
| 1332 | if newnibbles < nibbles: |
| 1333 | ashex = "0x" + "0" * (nibbles - newnibbles) + ashex[2:] |
| 1334 | if int(ashex[2], 16) < 8: |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1335 | # "looks positive", so need a byte of sign bits |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1336 | ashex = "0xff" + ashex[2:] |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1337 | |
| 1338 | if ashex.endswith('L'): |
| 1339 | ashex = ashex[2:-1] |
| 1340 | else: |
| 1341 | ashex = ashex[2:] |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1342 | assert len(ashex) & 1 == 0, (x, ashex) |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1343 | binary = _binascii.unhexlify(ashex) |
| 1344 | return binary[::-1] |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1345 | |
| 1346 | def decode_long(data): |
| 1347 | r"""Decode a long from a two's complement little-endian binary string. |
Tim Peters | 4b23f2b | 2003-01-31 16:43:39 +0000 | [diff] [blame] | 1348 | |
| 1349 | >>> decode_long('') |
| 1350 | 0L |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1351 | >>> decode_long("\xff\x00") |
| 1352 | 255L |
| 1353 | >>> decode_long("\xff\x7f") |
| 1354 | 32767L |
| 1355 | >>> decode_long("\x00\xff") |
| 1356 | -256L |
| 1357 | >>> decode_long("\x00\x80") |
| 1358 | -32768L |
| 1359 | >>> decode_long("\x80") |
| 1360 | -128L |
| 1361 | >>> decode_long("\x7f") |
| 1362 | 127L |
| 1363 | """ |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1364 | |
Tim Peters | 4b23f2b | 2003-01-31 16:43:39 +0000 | [diff] [blame] | 1365 | nbytes = len(data) |
| 1366 | if nbytes == 0: |
| 1367 | return 0L |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1368 | ashex = _binascii.hexlify(data[::-1]) |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1369 | n = long(ashex, 16) # quadratic time before Python 2.3; linear now |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1370 | if data[-1] >= '\x80': |
Tim Peters | 4b23f2b | 2003-01-31 16:43:39 +0000 | [diff] [blame] | 1371 | n -= 1L << (nbytes * 8) |
Tim Peters | 9114982 | 2003-01-31 03:43:58 +0000 | [diff] [blame] | 1372 | return n |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1373 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1374 | # Shorthands |
| 1375 | |
Jeremy Hylton | abe2c62 | 2001-10-15 21:29:28 +0000 | [diff] [blame] | 1376 | try: |
| 1377 | from cStringIO import StringIO |
| 1378 | except ImportError: |
| 1379 | from StringIO import StringIO |
Guido van Rossum | c7c5e69 | 1996-07-22 22:26:07 +0000 | [diff] [blame] | 1380 | |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 1381 | def dump(obj, file, protocol=None, bin=None): |
| 1382 | Pickler(file, protocol, bin).dump(obj) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1383 | |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 1384 | def dumps(obj, protocol=None, bin=None): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1385 | file = StringIO() |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 1386 | Pickler(file, protocol, bin).dump(obj) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1387 | return file.getvalue() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1388 | |
| 1389 | def load(file): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1390 | return Unpickler(file).load() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1391 | |
| 1392 | def loads(str): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1393 | file = StringIO(str) |
| 1394 | return Unpickler(file).load() |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1395 | |
| 1396 | # Doctest |
| 1397 | |
| 1398 | def _test(): |
| 1399 | import doctest |
| 1400 | return doctest.testmod() |
| 1401 | |
| 1402 | if __name__ == "__main__": |
| 1403 | _test() |