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