blob: 863702dc9f1c884f8cd573c540a877e0ca0d1bad [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Create portable serialized representations of Python objects.
Guido van Rossuma48061a1995-01-10 00:31:14 +00002
Guido van Rossume467be61997-12-05 19:42:42 +00003See module cPickle for a (much) faster implementation.
4See module copy_reg for a mechanism for registering custom picklers.
Tim Peters22a449a2003-01-27 20:16:36 +00005See module pickletools source for extensive comments.
Guido van Rossuma48061a1995-01-10 00:31:14 +00006
Guido van Rossume467be61997-12-05 19:42:42 +00007Classes:
Guido van Rossuma48061a1995-01-10 00:31:14 +00008
Guido van Rossume467be61997-12-05 19:42:42 +00009 Pickler
10 Unpickler
Guido van Rossuma48061a1995-01-10 00:31:14 +000011
Guido van Rossume467be61997-12-05 19:42:42 +000012Functions:
Guido van Rossuma48061a1995-01-10 00:31:14 +000013
Guido van Rossume467be61997-12-05 19:42:42 +000014 dump(object, file)
15 dumps(object) -> string
16 load(file) -> object
17 loads(string) -> object
Guido van Rossuma48061a1995-01-10 00:31:14 +000018
Guido van Rossume467be61997-12-05 19:42:42 +000019Misc variables:
Guido van Rossuma48061a1995-01-10 00:31:14 +000020
Fred Drakefe82acc1998-02-13 03:24:48 +000021 __version__
Guido van Rossume467be61997-12-05 19:42:42 +000022 format_version
23 compatible_formats
Guido van Rossuma48061a1995-01-10 00:31:14 +000024
Guido van Rossuma48061a1995-01-10 00:31:14 +000025"""
26
Guido van Rossum743d17e1998-09-15 20:25:57 +000027__version__ = "$Revision$" # Code version
Guido van Rossuma48061a1995-01-10 00:31:14 +000028
29from types import *
Guido van Rossumb26a97a2003-01-28 22:29:13 +000030from copy_reg import dispatch_table, _reconstructor
Guido van Rossumd3703791998-10-22 20:15:36 +000031import marshal
32import sys
33import struct
Skip Montanaro23bafc62001-02-18 03:10:09 +000034import re
Guido van Rossumbc64e222003-01-28 16:34:19 +000035import warnings
Guido van Rossuma48061a1995-01-10 00:31:14 +000036
Skip Montanaro352674d2001-02-07 23:14:30 +000037__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
38 "Unpickler", "dump", "dumps", "load", "loads"]
39
Tim Petersc0c12b52003-01-29 00:56:17 +000040# These are purely informational; no code uses these.
Guido van Rossumf29d3d62003-01-27 22:47:53 +000041format_version = "2.0" # File format version we write
42compatible_formats = ["1.0", # Original protocol 0
Guido van Rossumbc64e222003-01-28 16:34:19 +000043 "1.1", # Protocol 0 with INST added
Guido van Rossumf29d3d62003-01-27 22:47:53 +000044 "1.2", # Original protocol 1
45 "1.3", # Protocol 1 with BINFLOAT added
46 "2.0", # Protocol 2
47 ] # Old format versions we can read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000048
Guido van Rossume0b90422003-01-28 03:17:21 +000049# Why use struct.pack() for pickling but marshal.loads() for
Tim Petersc0c12b52003-01-29 00:56:17 +000050# unpickling? struct.pack() is 40% faster than marshal.dumps(), but
Guido van Rossume0b90422003-01-28 03:17:21 +000051# marshal.loads() is twice as fast as struct.unpack()!
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000052mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +000053
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000054class PickleError(Exception):
Neal Norwitzefbb67b2002-05-30 12:12:04 +000055 """A common base class for the other pickling exceptions."""
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000056 pass
57
58class PicklingError(PickleError):
59 """This exception is raised when an unpicklable object is passed to the
60 dump() method.
61
62 """
63 pass
64
65class UnpicklingError(PickleError):
66 """This exception is raised when there is a problem unpickling an object,
67 such as a security violation.
68
69 Note that other exceptions may also be raised during unpickling, including
70 (but not necessarily limited to) AttributeError, EOFError, ImportError,
71 and IndexError.
72
73 """
74 pass
Guido van Rossum7849da81995-03-09 14:08:35 +000075
Tim Petersc0c12b52003-01-29 00:56:17 +000076# An instance of _Stop is raised by Unpickler.load_stop() in response to
77# the STOP opcode, passing the object that is the result of unpickling.
Guido van Rossumff871742000-12-13 18:11:56 +000078class _Stop(Exception):
79 def __init__(self, value):
80 self.value = value
81
Guido van Rossum533dbcf2003-01-28 17:55:05 +000082# Jython has PyStringMap; it's a dict subclass with string keys
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000083try:
84 from org.python.core import PyStringMap
85except ImportError:
86 PyStringMap = None
87
Guido van Rossum533dbcf2003-01-28 17:55:05 +000088# UnicodeType may or may not be exported (normally imported from types)
Guido van Rossumdbb718f2001-09-21 19:22:34 +000089try:
90 UnicodeType
91except NameError:
92 UnicodeType = None
93
Tim Peters22a449a2003-01-27 20:16:36 +000094# Pickle opcodes. See pickletools.py for extensive docs. The listing
95# here is in kind-of alphabetical order of 1-character pickle code.
96# pickletools groups them by purpose.
Guido van Rossumdbb718f2001-09-21 19:22:34 +000097
Tim Peters22a449a2003-01-27 20:16:36 +000098MARK = '(' # push special markobject on stack
99STOP = '.' # every pickle ends with STOP
100POP = '0' # discard topmost stack item
101POP_MARK = '1' # discard stack top through topmost markobject
102DUP = '2' # duplicate top stack item
103FLOAT = 'F' # push float object; decimal string argument
104INT = 'I' # push integer or bool; decimal string argument
105BININT = 'J' # push four-byte signed int
106BININT1 = 'K' # push 1-byte unsigned int
107LONG = 'L' # push long; decimal string argument
108BININT2 = 'M' # push 2-byte unsigned int
109NONE = 'N' # push None
110PERSID = 'P' # push persistent object; id is taken from string arg
111BINPERSID = 'Q' # " " " ; " " " " stack
112REDUCE = 'R' # apply callable to argtuple, both on stack
113STRING = 'S' # push string; NL-terminated string argument
114BINSTRING = 'T' # push string; counted binary string argument
115SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes
116UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument
117BINUNICODE = 'X' # " " " ; counted UTF-8 string argument
118APPEND = 'a' # append stack top to list below it
119BUILD = 'b' # call __setstate__ or __dict__.update()
120GLOBAL = 'c' # push self.find_class(modname, name); 2 string args
121DICT = 'd' # build a dict from stack items
122EMPTY_DICT = '}' # push empty dict
123APPENDS = 'e' # extend list on stack by topmost stack slice
124GET = 'g' # push item from memo on stack; index is string arg
125BINGET = 'h' # " " " " " " ; " " 1-byte arg
126INST = 'i' # build & push class instance
127LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg
128LIST = 'l' # build list from topmost stack items
129EMPTY_LIST = ']' # push empty list
130OBJ = 'o' # build & push class instance
131PUT = 'p' # store stack top in memo; index is string arg
132BINPUT = 'q' # " " " " " ; " " 1-byte arg
133LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg
134SETITEM = 's' # add key+value pair to dict
135TUPLE = 't' # build tuple from topmost stack items
136EMPTY_TUPLE = ')' # push empty tuple
137SETITEMS = 'u' # modify dict by adding topmost key+value pairs
138BINFLOAT = 'G' # push float; arg is 8-byte float encoding
139
140TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py
141FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py
Guido van Rossum77f6a652002-04-03 22:41:51 +0000142
Tim Petersc0c12b52003-01-29 00:56:17 +0000143# Protocol 2 (XXX not yet implemented).
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000144
Tim Peterse1054782003-01-28 00:22:12 +0000145PROTO = '\x80' # identify pickle protocol
146NEWOBJ = '\x81' # build object by applying cls.__new__ to argtuple
147EXT1 = '\x82' # push object from extension registry; 1-byte index
148EXT2 = '\x83' # ditto, but 2-byte index
149EXT4 = '\x84' # ditto, but 4-byte index
150TUPLE1 = '\x85' # build 1-tuple from stack top
151TUPLE2 = '\x86' # build 2-tuple from two topmost stack items
152TUPLE3 = '\x87' # build 3-tuple from three topmost stack items
153NEWTRUE = '\x88' # push True
154NEWFALSE = '\x89' # push False
155LONG1 = '\x8a' # push long from < 256 bytes
156LONG4 = '\x8b' # push really big long
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000157
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000158_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
159
Guido van Rossuma48061a1995-01-10 00:31:14 +0000160
Skip Montanaro23bafc62001-02-18 03:10:09 +0000161__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
Neal Norwitzd5ba4ae2002-02-11 18:12:06 +0000162del x
Skip Montanaro23bafc62001-02-18 03:10:09 +0000163
Guido van Rossum1be31752003-01-28 15:19:53 +0000164
165# Pickling machinery
166
Guido van Rossuma48061a1995-01-10 00:31:14 +0000167class Pickler:
168
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000169 def __init__(self, file, proto=1):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000170 """This takes a file-like object for writing a pickle data stream.
171
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000172 The optional proto argument tells the pickler to use the given
173 protocol; supported protocols are 0, 1, 2. The default
174 protocol is 1 (in previous Python versions the default was 0).
175
176 Protocol 1 is more efficient than protocol 0; protocol 2 is
177 more efficient than protocol 1. Protocol 2 is not the default
178 because it is not supported by older Python versions.
179
180 XXX Protocol 2 is not yet implemented.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000181
182 The file parameter must have a write() method that accepts a single
183 string argument. It can thus be an open file object, a StringIO
184 object, or any other custom object that meets this interface.
185
186 """
Guido van Rossum1be31752003-01-28 15:19:53 +0000187 if proto not in (0, 1, 2):
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000188 raise ValueError, "pickle protocol must be 0, 1 or 2"
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000189 self.write = file.write
190 self.memo = {}
Guido van Rossum1be31752003-01-28 15:19:53 +0000191 self.proto = int(proto)
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000192 self.bin = proto >= 1
Guido van Rossuma48061a1995-01-10 00:31:14 +0000193
Fred Drake7f781c92002-05-01 20:33:53 +0000194 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000195 """Clears the pickler's "memo".
196
197 The memo is the data structure that remembers which objects the
Tim Petersb377f8a2003-01-28 00:23:36 +0000198 pickler has already seen, so that shared or recursive objects are
199 pickled by reference and not by value. This method is useful when
200 re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000201
202 """
Fred Drake7f781c92002-05-01 20:33:53 +0000203 self.memo.clear()
204
Guido van Rossum3a41c612003-01-28 15:10:22 +0000205 def dump(self, obj):
206 """Write a pickled representation of obj to the open file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000207
208 Either the binary or ASCII format will be used, depending on the
209 value of the bin flag passed to the constructor.
210
211 """
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000212 if self.proto >= 2:
213 self.write(PROTO + chr(self.proto))
Guido van Rossum3a41c612003-01-28 15:10:22 +0000214 self.save(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000215 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000216
Jeremy Hylton3422c992003-01-24 19:29:52 +0000217 def memoize(self, obj):
218 """Store an object in the memo."""
219
Tim Peterse46b73f2003-01-27 21:22:10 +0000220 # The Pickler memo is a dictionary mapping object ids to 2-tuples
221 # that contain the Unpickler memo key and the object being memoized.
222 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000223 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000224 # Pickler memo so that transient objects are kept alive during
225 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000226
Tim Peterse46b73f2003-01-27 21:22:10 +0000227 # The use of the Unpickler memo length as the memo key is just a
228 # convention. The only requirement is that the memo values be unique.
229 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000230 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000231 # growable) array, indexed by memo key.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000232 memo_len = len(self.memo)
233 self.write(self.put(memo_len))
Tim Peters518df0d2003-01-28 01:00:38 +0000234 self.memo[id(obj)] = memo_len, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000235
Tim Petersbb38e302003-01-27 21:25:41 +0000236 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000237 def put(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000238 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000239 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000240 return BINPUT + chr(i)
241 else:
242 return LONG_BINPUT + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000243
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000244 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000245
Tim Petersbb38e302003-01-27 21:25:41 +0000246 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000247 def get(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000248 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000249 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000250 return BINGET + chr(i)
251 else:
252 return LONG_BINGET + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000253
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000254 return GET + `i` + '\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000255
Guido van Rossumac5b5d22003-01-28 22:01:16 +0000256 def save(self, obj):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000257 # Check for persistent id (defined by a subclass)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000258 pid = self.persistent_id(obj)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000259 if pid:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000260 self.save_pers(pid)
261 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000262
Guido van Rossumbc64e222003-01-28 16:34:19 +0000263 # Check the memo
264 x = self.memo.get(id(obj))
265 if x:
266 self.write(self.get(x[0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000267 return
268
Guido van Rossumbc64e222003-01-28 16:34:19 +0000269 # Check the type dispatch table
Guido van Rossum3a41c612003-01-28 15:10:22 +0000270 t = type(obj)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000271 f = self.dispatch.get(t)
272 if f:
273 f(self, obj) # Call unbound method with explicit self
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000274 return
275
Guido van Rossumbc64e222003-01-28 16:34:19 +0000276 # Check for a class with a custom metaclass; treat as regular class
Tim Petersb32a8312003-01-28 00:48:09 +0000277 try:
278 issc = issubclass(t, TypeType)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000279 except TypeError: # t is not a class (old Boost; see SF #502085)
Tim Petersb32a8312003-01-28 00:48:09 +0000280 issc = 0
281 if issc:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000282 self.save_global(obj)
Tim Petersb32a8312003-01-28 00:48:09 +0000283 return
284
Guido van Rossumbc64e222003-01-28 16:34:19 +0000285 # Check copy_reg.dispatch_table
286 reduce = dispatch_table.get(t)
Guido van Rossumac5b5d22003-01-28 22:01:16 +0000287 if not reduce:
288 # Check for a __reduce__ method.
289 # Subtle: get the unbound method from the class, so that
290 # protocol 2 can override the default __reduce__ that all
291 # classes inherit from object. This has the added
292 # advantage that the call always has the form reduce(obj)
293 reduce = getattr(t, "__reduce__", None)
294 if self.proto >= 2:
295 # Protocol 2 can do better than the default __reduce__
296 if reduce is object.__reduce__:
297 reduce = None
298 if not reduce:
Guido van Rossum54fb1922003-01-28 18:22:35 +0000299 self.save_newobj(obj)
300 return
Guido van Rossumac5b5d22003-01-28 22:01:16 +0000301 if not reduce:
Guido van Rossumbc64e222003-01-28 16:34:19 +0000302 raise PicklingError("Can't pickle %r object: %r" %
303 (t.__name__, obj))
Guido van Rossumac5b5d22003-01-28 22:01:16 +0000304 rv = reduce(obj)
Tim Petersb32a8312003-01-28 00:48:09 +0000305
Guido van Rossumbc64e222003-01-28 16:34:19 +0000306 # Check for string returned by reduce(), meaning "save as global"
307 if type(rv) is StringType:
308 self.save_global(obj, rv)
Tim Petersb32a8312003-01-28 00:48:09 +0000309 return
310
Guido van Rossumbc64e222003-01-28 16:34:19 +0000311 # Assert that reduce() returned a tuple
312 if type(rv) is not TupleType:
313 raise PicklingError("%s must return string or tuple" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000314
Guido van Rossumbc64e222003-01-28 16:34:19 +0000315 # Assert that it returned a 2-tuple or 3-tuple, and unpack it
316 l = len(rv)
317 if l == 2:
318 func, args = rv
Tim Petersb32a8312003-01-28 00:48:09 +0000319 state = None
Guido van Rossumbc64e222003-01-28 16:34:19 +0000320 elif l == 3:
321 func, args, state = rv
322 else:
323 raise PicklingError("Tuple returned by %s must have "
324 "exactly two or three elements" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000325
Guido van Rossumbc64e222003-01-28 16:34:19 +0000326 # Save the reduce() output and finally memoize the object
327 self.save_reduce(func, args, state)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000328 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000329
Guido van Rossum3a41c612003-01-28 15:10:22 +0000330 def persistent_id(self, obj):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000331 # This exists so a subclass can override it
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000332 return None
333
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000334 def save_pers(self, pid):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000335 # Save a persistent id reference
Tim Petersbd1cdb92003-01-28 01:03:10 +0000336 if self.bin:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000337 self.save(pid)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000338 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000339 else:
340 self.write(PERSID + str(pid) + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000341
Guido van Rossumbc64e222003-01-28 16:34:19 +0000342 def save_reduce(self, func, args, state=None):
343 # This API is be called by some subclasses
344
345 # Assert that args is a tuple or None
346 if not isinstance(args, TupleType):
347 if args is None:
348 # A hack for Jim Fulton's ExtensionClass, now deprecated.
349 # See load_reduce()
350 warnings.warn("__basicnew__ special case is deprecated",
351 DeprecationWarning)
352 else:
353 raise PicklingError(
354 "args from reduce() should be a tuple")
355
356 # Assert that func is callable
357 if not callable(func):
358 raise PicklingError("func from reduce should be callable")
359
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000360 save = self.save
Guido van Rossumbc64e222003-01-28 16:34:19 +0000361 write = self.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000362
Guido van Rossumbc64e222003-01-28 16:34:19 +0000363 save(func)
364 save(args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000365 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000366
Tim Petersc32d8242001-04-10 02:48:53 +0000367 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000368 save(state)
369 write(BUILD)
370
Guido van Rossum54fb1922003-01-28 18:22:35 +0000371 def save_newobj(self, obj):
372 # Save a new-style class instance, using protocol 2.
Guido van Rossum4e2491d2003-01-28 22:31:25 +0000373 # XXX This is still experimental.
Guido van Rossumac5b5d22003-01-28 22:01:16 +0000374 assert self.proto >= 2 # This only works for protocol 2
Guido van Rossum54fb1922003-01-28 18:22:35 +0000375 t = type(obj)
Guido van Rossum54fb1922003-01-28 18:22:35 +0000376 getnewargs = getattr(obj, "__getnewargs__", None)
377 if getnewargs:
Guido van Rossumac5b5d22003-01-28 22:01:16 +0000378 args = getnewargs() # This bette not reference obj
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000379 else:
Guido van Rossum4e2491d2003-01-28 22:31:25 +0000380 # XXX These types should each grow a __getnewargs__
381 # implementation so this special-casing is unnecessary.
Guido van Rossumb26a97a2003-01-28 22:29:13 +0000382 for cls in int, long, float, complex, str, UnicodeType, tuple:
383 if cls and isinstance(obj, cls):
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000384 args = (cls(obj),)
385 break
386 else:
387 args = ()
388
389 save = self.save
390 write = self.write
391
Guido van Rossum54fb1922003-01-28 18:22:35 +0000392 self.save_global(t)
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000393 save(args)
394 write(NEWOBJ)
Guido van Rossum54fb1922003-01-28 18:22:35 +0000395 self.memoize(obj)
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000396
397 if isinstance(obj, list):
398 write(MARK)
399 for x in obj:
400 save(x)
401 write(APPENDS)
402 elif isinstance(obj, dict):
403 write(MARK)
404 for k, v in obj.iteritems():
405 save(k)
406 save(v)
407 write(SETITEMS)
408
Guido van Rossum54fb1922003-01-28 18:22:35 +0000409 getstate = getattr(obj, "__getstate__", None)
410 if getstate:
Guido van Rossumac5b5d22003-01-28 22:01:16 +0000411 try:
412 state = getstate()
413 except TypeError, err:
414 # XXX Catch generic exception caused by __slots__
415 if str(err) != ("a class that defines __slots__ "
416 "without defining __getstate__ "
417 "cannot be pickled"):
418 print repr(str(err))
419 raise # Not that specific exception
420 getstate = None
421 if not getstate:
Guido van Rossum54fb1922003-01-28 18:22:35 +0000422 state = getattr(obj, "__dict__", None)
Guido van Rossumac5b5d22003-01-28 22:01:16 +0000423 # If there are slots, the state becomes a tuple of two
424 # items: the first item the regular __dict__ or None, and
425 # the second a dict mapping slot names to slot values
426 names = _slotnames(t)
427 if names:
428 slots = {}
429 nil = []
430 for name in names:
431 value = getattr(obj, name, nil)
432 if value is not nil:
433 slots[name] = value
434 if slots:
435 state = (state, slots)
436
Guido van Rossum54fb1922003-01-28 18:22:35 +0000437 if state is not None:
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000438 save(state)
439 write(BUILD)
Guido van Rossum54fb1922003-01-28 18:22:35 +0000440
Guido van Rossumbc64e222003-01-28 16:34:19 +0000441 # Methods below this point are dispatched through the dispatch table
442
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000443 dispatch = {}
444
Guido van Rossum3a41c612003-01-28 15:10:22 +0000445 def save_none(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000446 self.write(NONE)
447 dispatch[NoneType] = save_none
448
Guido van Rossum3a41c612003-01-28 15:10:22 +0000449 def save_bool(self, obj):
Guido van Rossum7d97d312003-01-28 04:25:27 +0000450 if self.proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000451 self.write(obj and NEWTRUE or NEWFALSE)
Guido van Rossum7d97d312003-01-28 04:25:27 +0000452 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000453 self.write(obj and TRUE or FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000454 dispatch[bool] = save_bool
455
Guido van Rossum3a41c612003-01-28 15:10:22 +0000456 def save_int(self, obj, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000457 if self.bin:
Tim Peters44714002001-04-10 05:02:52 +0000458 # If the int is small enough to fit in a signed 4-byte 2's-comp
459 # format, we can store it more efficiently than the general
460 # case.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000461 # First one- and two-byte unsigned ints:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000462 if obj >= 0:
463 if obj <= 0xff:
464 self.write(BININT1 + chr(obj))
Guido van Rossum5c938d02003-01-28 03:03:08 +0000465 return
Guido van Rossum3a41c612003-01-28 15:10:22 +0000466 if obj <= 0xffff:
467 self.write(BININT2 + chr(obj&0xff) + chr(obj>>8))
Guido van Rossum5c938d02003-01-28 03:03:08 +0000468 return
469 # Next check for 4-byte signed ints:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000470 high_bits = obj >> 31 # note that Python shift sign-extends
Tim Petersd95c2df2003-01-28 03:41:54 +0000471 if high_bits == 0 or high_bits == -1:
Tim Peters44714002001-04-10 05:02:52 +0000472 # All high bits are copies of bit 2**31, so the value
473 # fits in a 4-byte signed int.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000474 self.write(BININT + pack("<i", obj))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000475 return
Tim Peters44714002001-04-10 05:02:52 +0000476 # Text pickle, or int too big to fit in signed 4-byte format.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000477 self.write(INT + `obj` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000478 dispatch[IntType] = save_int
479
Guido van Rossum3a41c612003-01-28 15:10:22 +0000480 def save_long(self, obj, pack=struct.pack):
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000481 if self.proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000482 bytes = encode_long(obj)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000483 n = len(bytes)
484 if n < 256:
485 self.write(LONG1 + chr(n) + bytes)
486 else:
487 self.write(LONG4 + pack("<i", n) + bytes)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000488 self.write(LONG + `obj` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000489 dispatch[LongType] = save_long
490
Guido van Rossum3a41c612003-01-28 15:10:22 +0000491 def save_float(self, obj, pack=struct.pack):
Guido van Rossumd3703791998-10-22 20:15:36 +0000492 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000493 self.write(BINFLOAT + pack('>d', obj))
Guido van Rossumd3703791998-10-22 20:15:36 +0000494 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000495 self.write(FLOAT + `obj` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000496 dispatch[FloatType] = save_float
497
Guido van Rossum3a41c612003-01-28 15:10:22 +0000498 def save_string(self, obj, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000499 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000500 n = len(obj)
Tim Petersbbf63cd2003-01-27 21:15:36 +0000501 if n < 256:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000502 self.write(SHORT_BINSTRING + chr(n) + obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000503 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000504 self.write(BINSTRING + pack("<i", n) + obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000505 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000506 self.write(STRING + `obj` + '\n')
507 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000508 dispatch[StringType] = save_string
509
Guido van Rossum3a41c612003-01-28 15:10:22 +0000510 def save_unicode(self, obj, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000511 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000512 encoding = obj.encode('utf-8')
Tim Petersbbf63cd2003-01-27 21:15:36 +0000513 n = len(encoding)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000514 self.write(BINUNICODE + pack("<i", n) + encoding)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000515 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000516 obj = obj.replace("\\", "\\u005c")
517 obj = obj.replace("\n", "\\u000a")
518 self.write(UNICODE + obj.encode('raw-unicode-escape') + '\n')
519 self.memoize(obj)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000520 dispatch[UnicodeType] = save_unicode
521
Guido van Rossum31584cb2001-01-22 14:53:29 +0000522 if StringType == UnicodeType:
523 # This is true for Jython
Guido van Rossum3a41c612003-01-28 15:10:22 +0000524 def save_string(self, obj, pack=struct.pack):
525 unicode = obj.isunicode()
Guido van Rossum31584cb2001-01-22 14:53:29 +0000526
Tim Petersc32d8242001-04-10 02:48:53 +0000527 if self.bin:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000528 if unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000529 obj = obj.encode("utf-8")
530 l = len(obj)
Tim Petersc32d8242001-04-10 02:48:53 +0000531 if l < 256 and not unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000532 self.write(SHORT_BINSTRING + chr(l) + obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000533 else:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000534 s = pack("<i", l)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000535 if unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000536 self.write(BINUNICODE + s + obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000537 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000538 self.write(BINSTRING + s + obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000539 else:
Tim Peters658cba62001-02-09 20:06:00 +0000540 if unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000541 obj = obj.replace("\\", "\\u005c")
542 obj = obj.replace("\n", "\\u000a")
543 obj = obj.encode('raw-unicode-escape')
544 self.write(UNICODE + obj + '\n')
Guido van Rossum31584cb2001-01-22 14:53:29 +0000545 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000546 self.write(STRING + `obj` + '\n')
547 self.memoize(obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000548 dispatch[StringType] = save_string
Tim Peters658cba62001-02-09 20:06:00 +0000549
Guido van Rossum3a41c612003-01-28 15:10:22 +0000550 def save_tuple(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000551 write = self.write
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000552 proto = self.proto
553
Guido van Rossum3a41c612003-01-28 15:10:22 +0000554 n = len(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000555 if n == 0 and proto:
556 write(EMPTY_TUPLE)
557 return
558
559 save = self.save
560 memo = self.memo
561 if n <= 3 and proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000562 for element in obj:
Tim Petersd97da802003-01-28 05:48:29 +0000563 save(element)
564 # Subtle. Same as in the big comment below.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000565 if id(obj) in memo:
566 get = self.get(memo[id(obj)][0])
Tim Petersd97da802003-01-28 05:48:29 +0000567 write(POP * n + get)
568 else:
569 write(_tuplesize2code[n])
Guido van Rossum3a41c612003-01-28 15:10:22 +0000570 self.memoize(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000571 return
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000572
Tim Petersff57bff2003-01-28 05:34:53 +0000573 # proto 0, or proto 1 and tuple isn't empty, or proto > 1 and tuple
574 # has more than 3 elements.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000575 write(MARK)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000576 for element in obj:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000577 save(element)
578
Guido van Rossum3a41c612003-01-28 15:10:22 +0000579 if n and id(obj) in memo:
Tim Petersf558da02003-01-28 02:09:55 +0000580 # Subtle. d was not in memo when we entered save_tuple(), so
581 # the process of saving the tuple's elements must have saved
582 # the tuple itself: the tuple is recursive. The proper action
583 # now is to throw away everything we put on the stack, and
584 # simply GET the tuple (it's already constructed). This check
585 # could have been done in the "for element" loop instead, but
586 # recursive tuples are a rare thing.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000587 get = self.get(memo[id(obj)][0])
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000588 if proto:
Tim Petersf558da02003-01-28 02:09:55 +0000589 write(POP_MARK + get)
590 else: # proto 0 -- POP_MARK not available
Tim Petersd97da802003-01-28 05:48:29 +0000591 write(POP * (n+1) + get)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000592 return
593
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000594 # No recursion (including the empty-tuple case for protocol 0).
Tim Peters518df0d2003-01-28 01:00:38 +0000595 self.write(TUPLE)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000596 if obj: # No need to memoize empty tuple
597 self.memoize(obj)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000598
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000599 dispatch[TupleType] = save_tuple
600
Tim Petersa6ae9a22003-01-28 16:58:41 +0000601 # save_empty_tuple() isn't used by anything in Python 2.3. However, I
602 # found a Pickler subclass in Zope3 that calls it, so it's not harmless
603 # to remove it.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000604 def save_empty_tuple(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000605 self.write(EMPTY_TUPLE)
606
Guido van Rossum3a41c612003-01-28 15:10:22 +0000607 def save_list(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000608 write = self.write
609 save = self.save
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000610
Tim Petersc32d8242001-04-10 02:48:53 +0000611 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000612 write(EMPTY_LIST)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000613 self.memoize(obj)
614 n = len(obj)
Tim Peters21c18f02003-01-28 01:15:46 +0000615 if n > 1:
616 write(MARK)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000617 for element in obj:
Tim Peters21c18f02003-01-28 01:15:46 +0000618 save(element)
619 write(APPENDS)
620 elif n:
621 assert n == 1
Guido van Rossum3a41c612003-01-28 15:10:22 +0000622 save(obj[0])
Tim Peters21c18f02003-01-28 01:15:46 +0000623 write(APPEND)
624 # else the list is empty, and we're already done
625
626 else: # proto 0 -- can't use EMPTY_LIST or APPENDS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000627 write(MARK + LIST)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000628 self.memoize(obj)
629 for element in obj:
Tim Peters21c18f02003-01-28 01:15:46 +0000630 save(element)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000631 write(APPEND)
632
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000633 dispatch[ListType] = save_list
634
Guido van Rossum3a41c612003-01-28 15:10:22 +0000635 def save_dict(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000636 write = self.write
637 save = self.save
Guido van Rossum3a41c612003-01-28 15:10:22 +0000638 items = obj.iteritems()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000639
Tim Petersc32d8242001-04-10 02:48:53 +0000640 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000641 write(EMPTY_DICT)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000642 self.memoize(obj)
643 if len(obj) > 1:
Tim Peters064567e2003-01-28 01:34:43 +0000644 write(MARK)
645 for key, value in items:
646 save(key)
647 save(value)
648 write(SETITEMS)
649 return
Tim Peters82ca59e2003-01-28 16:47:59 +0000650 # else (dict is empty or a singleton), fall through to the
651 # SETITEM code at the end
Tim Peters064567e2003-01-28 01:34:43 +0000652 else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000653 write(MARK + DICT)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000654 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000655
Guido van Rossum3a41c612003-01-28 15:10:22 +0000656 # proto 0 or len(obj) < 2
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000657 for key, value in items:
658 save(key)
659 save(value)
Tim Peters064567e2003-01-28 01:34:43 +0000660 write(SETITEM)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000661
662 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000663 if not PyStringMap is None:
664 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000665
Guido van Rossum3a41c612003-01-28 15:10:22 +0000666 def save_inst(self, obj):
667 cls = obj.__class__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000668
669 memo = self.memo
670 write = self.write
671 save = self.save
672
Guido van Rossum3a41c612003-01-28 15:10:22 +0000673 if hasattr(obj, '__getinitargs__'):
674 args = obj.__getinitargs__()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000675 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000676 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000677 else:
678 args = ()
679
680 write(MARK)
681
Tim Petersc32d8242001-04-10 02:48:53 +0000682 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000683 save(cls)
Tim Peters3b769832003-01-28 03:51:36 +0000684 for arg in args:
685 save(arg)
686 write(OBJ)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000687 else:
Tim Peters3b769832003-01-28 03:51:36 +0000688 for arg in args:
689 save(arg)
690 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000691
Guido van Rossum3a41c612003-01-28 15:10:22 +0000692 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000693
694 try:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000695 getstate = obj.__getstate__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000696 except AttributeError:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000697 stuff = obj.__dict__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000698 else:
699 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000700 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000701 save(stuff)
702 write(BUILD)
Tim Peters3b769832003-01-28 03:51:36 +0000703
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000704 dispatch[InstanceType] = save_inst
705
Guido van Rossum3a41c612003-01-28 15:10:22 +0000706 def save_global(self, obj, name = None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000707 write = self.write
708 memo = self.memo
709
Tim Petersc32d8242001-04-10 02:48:53 +0000710 if name is None:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000711 name = obj.__name__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000712
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000713 try:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000714 module = obj.__module__
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000715 except AttributeError:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000716 module = whichmodule(obj, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000717
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000718 try:
719 __import__(module)
720 mod = sys.modules[module]
721 klass = getattr(mod, name)
722 except (ImportError, KeyError, AttributeError):
723 raise PicklingError(
724 "Can't pickle %r: it's not found as %s.%s" %
Guido van Rossum3a41c612003-01-28 15:10:22 +0000725 (obj, module, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000726 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000727 if klass is not obj:
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000728 raise PicklingError(
729 "Can't pickle %r: it's not the same object as %s.%s" %
Guido van Rossum3a41c612003-01-28 15:10:22 +0000730 (obj, module, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000731
Tim Peters518df0d2003-01-28 01:00:38 +0000732 write(GLOBAL + module + '\n' + name + '\n')
Guido van Rossum3a41c612003-01-28 15:10:22 +0000733 self.memoize(obj)
Tim Peters3b769832003-01-28 03:51:36 +0000734
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000735 dispatch[ClassType] = save_global
736 dispatch[FunctionType] = save_global
737 dispatch[BuiltinFunctionType] = save_global
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738 dispatch[TypeType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000739
Guido van Rossum1be31752003-01-28 15:19:53 +0000740# Pickling helpers
Guido van Rossuma48061a1995-01-10 00:31:14 +0000741
Guido van Rossumac5b5d22003-01-28 22:01:16 +0000742def _slotnames(cls):
743 """Return a list of slot names for a given class.
744
745 This needs to find slots defined by the class and its bases, so we
746 can't simply return the __slots__ attribute. We must walk down
747 the Method Resolution Order and concatenate the __slots__ of each
748 class found there. (This assumes classes don't modify their
749 __slots__ attribute to misrepresent their slots after the class is
750 defined.)
751 """
752 if not hasattr(cls, "__slots__"):
753 return []
754 names = []
755 for c in cls.__mro__:
756 if "__slots__" in c.__dict__:
757 names += list(c.__dict__["__slots__"])
758 return names
759
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000760def _keep_alive(x, memo):
761 """Keeps a reference to the object x in the memo.
762
763 Because we remember objects by their id, we have
764 to assure that possibly temporary objects are kept
765 alive by referencing them.
766 We store a reference at the id of the memo, which should
767 normally not be used unless someone tries to deepcopy
768 the memo itself...
769 """
770 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000771 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000772 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000773 # aha, this is the first one :-)
774 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000775
776
Tim Petersc0c12b52003-01-29 00:56:17 +0000777# A cache for whichmodule(), mapping a function object to the name of
778# the module in which the function was found.
779
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000780classmap = {} # called classmap for backwards compatibility
Guido van Rossuma48061a1995-01-10 00:31:14 +0000781
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000782def whichmodule(func, funcname):
783 """Figure out the module in which a function occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000784
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000785 Search sys.modules for the module.
786 Cache in classmap.
787 Return a module name.
Tim Petersc0c12b52003-01-29 00:56:17 +0000788 If the function cannot be found, return "__main__".
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000789 """
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000790 if func in classmap:
791 return classmap[func]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000792
793 for name, module in sys.modules.items():
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000794 if module is None:
Jeremy Hylton065a5ab2002-09-19 22:57:26 +0000795 continue # skip dummy package entries
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000796 if name != '__main__' and \
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000797 hasattr(module, funcname) and \
798 getattr(module, funcname) is func:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000799 break
800 else:
801 name = '__main__'
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000802 classmap[func] = name
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000803 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000804
805
Guido van Rossum1be31752003-01-28 15:19:53 +0000806# Unpickling machinery
807
Guido van Rossuma48061a1995-01-10 00:31:14 +0000808class Unpickler:
809
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000810 def __init__(self, file):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000811 """This takes a file-like object for reading a pickle data stream.
812
813 This class automatically determines whether the data stream was
814 written in binary mode or not, so it does not need a flag as in
815 the Pickler class factory.
816
817 The file-like object must have two methods, a read() method that
818 takes an integer argument, and a readline() method that requires no
819 arguments. Both methods should return a string. Thus file-like
820 object can be a file object opened for reading, a StringIO object,
821 or any other custom object that meets this interface.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000822 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000823 self.readline = file.readline
824 self.read = file.read
825 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000826
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000827 def load(self):
Guido van Rossum3a41c612003-01-28 15:10:22 +0000828 """Read a pickled object representation from the open file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000829
Guido van Rossum3a41c612003-01-28 15:10:22 +0000830 Return the reconstituted object hierarchy specified in the file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000831 """
Jeremy Hylton20747fa2001-11-09 16:15:04 +0000832 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000833 self.stack = []
834 self.append = self.stack.append
835 read = self.read
836 dispatch = self.dispatch
837 try:
838 while 1:
839 key = read(1)
840 dispatch[key](self)
Guido van Rossumff871742000-12-13 18:11:56 +0000841 except _Stop, stopinst:
842 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000843
Tim Petersc23d18a2003-01-28 01:41:51 +0000844 # Return largest index k such that self.stack[k] is self.mark.
845 # If the stack doesn't contain a mark, eventually raises IndexError.
846 # This could be sped by maintaining another stack, of indices at which
847 # the mark appears. For that matter, the latter stack would suffice,
848 # and we wouldn't need to push mark objects on self.stack at all.
849 # Doing so is probably a good thing, though, since if the pickle is
850 # corrupt (or hostile) we may get a clue from finding self.mark embedded
851 # in unpickled objects.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000852 def marker(self):
853 stack = self.stack
854 mark = self.mark
855 k = len(stack)-1
856 while stack[k] is not mark: k = k-1
857 return k
858
859 dispatch = {}
860
861 def load_eof(self):
862 raise EOFError
863 dispatch[''] = load_eof
864
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000865 def load_proto(self):
866 proto = ord(self.read(1))
867 if not 0 <= proto <= 2:
868 raise ValueError, "unsupported pickle protocol: %d" % proto
869 dispatch[PROTO] = load_proto
870
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000871 def load_persid(self):
872 pid = self.readline()[:-1]
873 self.append(self.persistent_load(pid))
874 dispatch[PERSID] = load_persid
875
876 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000877 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000878 self.append(self.persistent_load(pid))
879 dispatch[BINPERSID] = load_binpersid
880
881 def load_none(self):
882 self.append(None)
883 dispatch[NONE] = load_none
884
Guido van Rossum7d97d312003-01-28 04:25:27 +0000885 def load_false(self):
886 self.append(False)
887 dispatch[NEWFALSE] = load_false
888
889 def load_true(self):
890 self.append(True)
891 dispatch[NEWTRUE] = load_true
892
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000893 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000894 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +0000895 if data == FALSE[1:]:
896 val = False
897 elif data == TRUE[1:]:
898 val = True
899 else:
900 try:
901 val = int(data)
902 except ValueError:
903 val = long(data)
904 self.append(val)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000905 dispatch[INT] = load_int
906
907 def load_binint(self):
908 self.append(mloads('i' + self.read(4)))
909 dispatch[BININT] = load_binint
910
911 def load_binint1(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000912 self.append(ord(self.read(1)))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000913 dispatch[BININT1] = load_binint1
914
915 def load_binint2(self):
916 self.append(mloads('i' + self.read(2) + '\000\000'))
917 dispatch[BININT2] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +0000918
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000919 def load_long(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000920 self.append(long(self.readline()[:-1], 0))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000921 dispatch[LONG] = load_long
922
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000923 def load_long1(self):
924 n = ord(self.read(1))
925 bytes = self.read(n)
926 return decode_long(bytes)
927 dispatch[LONG1] = load_long1
928
929 def load_long4(self):
930 n = mloads('i' + self.read(4))
931 bytes = self.read(n)
932 return decode_long(bytes)
933 dispatch[LONG4] = load_long4
934
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000935 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000936 self.append(float(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000937 dispatch[FLOAT] = load_float
938
Guido van Rossumd3703791998-10-22 20:15:36 +0000939 def load_binfloat(self, unpack=struct.unpack):
940 self.append(unpack('>d', self.read(8))[0])
941 dispatch[BINFLOAT] = load_binfloat
942
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000943 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000944 rep = self.readline()[:-1]
Tim Petersad5a7712003-01-28 16:23:33 +0000945 for q in "\"'": # double or single quote
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000946 if rep.startswith(q):
947 if not rep.endswith(q):
948 raise ValueError, "insecure string pickle"
949 rep = rep[len(q):-len(q)]
950 break
951 else:
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000952 raise ValueError, "insecure string pickle"
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000953 self.append(rep.decode("string-escape"))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000954 dispatch[STRING] = load_string
955
956 def load_binstring(self):
957 len = mloads('i' + self.read(4))
958 self.append(self.read(len))
959 dispatch[BINSTRING] = load_binstring
960
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000961 def load_unicode(self):
962 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
963 dispatch[UNICODE] = load_unicode
964
965 def load_binunicode(self):
966 len = mloads('i' + self.read(4))
967 self.append(unicode(self.read(len),'utf-8'))
968 dispatch[BINUNICODE] = load_binunicode
969
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000970 def load_short_binstring(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000971 len = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000972 self.append(self.read(len))
973 dispatch[SHORT_BINSTRING] = load_short_binstring
974
975 def load_tuple(self):
976 k = self.marker()
977 self.stack[k:] = [tuple(self.stack[k+1:])]
978 dispatch[TUPLE] = load_tuple
979
980 def load_empty_tuple(self):
981 self.stack.append(())
982 dispatch[EMPTY_TUPLE] = load_empty_tuple
983
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000984 def load_tuple1(self):
985 self.stack[-1] = (self.stack[-1],)
986 dispatch[TUPLE1] = load_tuple1
987
988 def load_tuple2(self):
989 self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
990 dispatch[TUPLE2] = load_tuple2
991
992 def load_tuple3(self):
993 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
994 dispatch[TUPLE3] = load_tuple3
995
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000996 def load_empty_list(self):
997 self.stack.append([])
998 dispatch[EMPTY_LIST] = load_empty_list
999
1000 def load_empty_dictionary(self):
1001 self.stack.append({})
1002 dispatch[EMPTY_DICT] = load_empty_dictionary
1003
1004 def load_list(self):
1005 k = self.marker()
1006 self.stack[k:] = [self.stack[k+1:]]
1007 dispatch[LIST] = load_list
1008
1009 def load_dict(self):
1010 k = self.marker()
1011 d = {}
1012 items = self.stack[k+1:]
1013 for i in range(0, len(items), 2):
1014 key = items[i]
1015 value = items[i+1]
1016 d[key] = value
1017 self.stack[k:] = [d]
1018 dispatch[DICT] = load_dict
1019
1020 def load_inst(self):
1021 k = self.marker()
1022 args = tuple(self.stack[k+1:])
1023 del self.stack[k:]
1024 module = self.readline()[:-1]
1025 name = self.readline()[:-1]
1026 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001027 instantiated = 0
1028 if (not args and type(klass) is ClassType and
1029 not hasattr(klass, "__getinitargs__")):
1030 try:
1031 value = _EmptyClass()
1032 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +00001033 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001034 except RuntimeError:
1035 # In restricted execution, assignment to inst.__class__ is
1036 # prohibited
1037 pass
1038 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +00001039 try:
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001040 value = klass(*args)
Guido van Rossum743d17e1998-09-15 20:25:57 +00001041 except TypeError, err:
1042 raise TypeError, "in constructor for %s: %s" % (
1043 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001044 self.append(value)
1045 dispatch[INST] = load_inst
1046
1047 def load_obj(self):
1048 stack = self.stack
1049 k = self.marker()
1050 klass = stack[k + 1]
1051 del stack[k + 1]
Tim Peters2344fae2001-01-15 00:50:52 +00001052 args = tuple(stack[k + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001053 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001054 instantiated = 0
1055 if (not args and type(klass) is ClassType and
1056 not hasattr(klass, "__getinitargs__")):
1057 try:
1058 value = _EmptyClass()
1059 value.__class__ = klass
1060 instantiated = 1
1061 except RuntimeError:
1062 # In restricted execution, assignment to inst.__class__ is
1063 # prohibited
1064 pass
1065 if not instantiated:
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001066 value = klass(*args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001067 self.append(value)
Tim Peters2344fae2001-01-15 00:50:52 +00001068 dispatch[OBJ] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001069
Guido van Rossum3a41c612003-01-28 15:10:22 +00001070 def load_newobj(self):
1071 args = self.stack.pop()
1072 cls = self.stack[-1]
1073 obj = cls.__new__(cls, *args)
Guido van Rossum533dbcf2003-01-28 17:55:05 +00001074 self.stack[-1] = obj
Guido van Rossum3a41c612003-01-28 15:10:22 +00001075 dispatch[NEWOBJ] = load_newobj
1076
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001077 def load_global(self):
1078 module = self.readline()[:-1]
1079 name = self.readline()[:-1]
1080 klass = self.find_class(module, name)
1081 self.append(klass)
1082 dispatch[GLOBAL] = load_global
1083
1084 def find_class(self, module, name):
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001085 # Subclasses may override this
Barry Warsawbf4d9592001-11-15 23:42:58 +00001086 __import__(module)
1087 mod = sys.modules[module]
1088 klass = getattr(mod, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001089 return klass
1090
1091 def load_reduce(self):
1092 stack = self.stack
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001093 args = stack.pop()
1094 func = stack[-1]
1095 if args is None:
Guido van Rossumbc64e222003-01-28 16:34:19 +00001096 # A hack for Jim Fulton's ExtensionClass, now deprecated
1097 warnings.warn("__basicnew__ special case is deprecated",
Tim Peters8ac14952002-05-23 15:15:30 +00001098 DeprecationWarning)
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001099 value = func.__basicnew__()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001100 else:
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001101 value = func(*args)
1102 stack[-1] = value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001103 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +00001104
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001105 def load_pop(self):
1106 del self.stack[-1]
1107 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +00001108
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001109 def load_pop_mark(self):
1110 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001111 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001112 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001113
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001114 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +00001115 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001116 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +00001117
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001118 def load_get(self):
1119 self.append(self.memo[self.readline()[:-1]])
1120 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +00001121
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001122 def load_binget(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001123 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001124 self.append(self.memo[`i`])
1125 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001126
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001127 def load_long_binget(self):
1128 i = mloads('i' + self.read(4))
1129 self.append(self.memo[`i`])
1130 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001131
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001132 def load_put(self):
1133 self.memo[self.readline()[:-1]] = self.stack[-1]
1134 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001135
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001136 def load_binput(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001137 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001138 self.memo[`i`] = self.stack[-1]
1139 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001140
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001141 def load_long_binput(self):
1142 i = mloads('i' + self.read(4))
1143 self.memo[`i`] = self.stack[-1]
1144 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001145
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001146 def load_append(self):
1147 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001148 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001149 list = stack[-1]
1150 list.append(value)
1151 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001152
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001153 def load_appends(self):
1154 stack = self.stack
1155 mark = self.marker()
1156 list = stack[mark - 1]
Tim Peters209ad952003-01-28 01:44:45 +00001157 list.extend(stack[mark + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001158 del stack[mark:]
1159 dispatch[APPENDS] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001160
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001161 def load_setitem(self):
1162 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001163 value = stack.pop()
1164 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001165 dict = stack[-1]
1166 dict[key] = value
1167 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001168
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001169 def load_setitems(self):
1170 stack = self.stack
1171 mark = self.marker()
1172 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001173 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001174 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001175
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001176 del stack[mark:]
1177 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001178
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001179 def load_build(self):
1180 stack = self.stack
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001181 state = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001182 inst = stack[-1]
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001183 setstate = getattr(inst, "__setstate__", None)
1184 if setstate:
1185 setstate(state)
1186 return
1187 slotstate = None
1188 if isinstance(state, tuple) and len(state) == 2:
1189 state, slotstate = state
1190 if state:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001191 try:
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001192 inst.__dict__.update(state)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001193 except RuntimeError:
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001194 # XXX In restricted execution, the instance's __dict__
1195 # is not accessible. Use the old way of unpickling
1196 # the instance variables. This is a semantic
1197 # difference when unpickling in restricted
1198 # vs. unrestricted modes.
1199 for k, v in state.items():
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001200 setattr(inst, k, v)
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001201 if slotstate:
1202 for k, v in slotstate.items():
1203 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001204 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001205
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001206 def load_mark(self):
1207 self.append(self.mark)
1208 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001209
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001210 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001211 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001212 raise _Stop(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001213 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001214
Guido van Rossume467be61997-12-05 19:42:42 +00001215# Helper class for load_inst/load_obj
1216
1217class _EmptyClass:
1218 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +00001219
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001220# Encode/decode longs.
1221
1222def encode_long(x):
1223 r"""Encode a long to a two's complement little-ending binary string.
1224 >>> encode_long(255L)
1225 '\xff\x00'
1226 >>> encode_long(32767L)
1227 '\xff\x7f'
1228 >>> encode_long(-256L)
1229 '\x00\xff'
1230 >>> encode_long(-32768L)
1231 '\x00\x80'
1232 >>> encode_long(-128L)
1233 '\x80'
1234 >>> encode_long(127L)
1235 '\x7f'
1236 >>>
1237 """
Guido van Rossum3d8c01b2003-01-28 19:48:18 +00001238 # XXX This is still a quadratic algorithm.
1239 # Should use hex() to get started.
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001240 digits = []
1241 while not -128 <= x < 128:
1242 digits.append(x & 0xff)
1243 x >>= 8
1244 digits.append(x & 0xff)
1245 return "".join(map(chr, digits))
1246
1247def decode_long(data):
1248 r"""Decode a long from a two's complement little-endian binary string.
1249 >>> decode_long("\xff\x00")
1250 255L
1251 >>> decode_long("\xff\x7f")
1252 32767L
1253 >>> decode_long("\x00\xff")
1254 -256L
1255 >>> decode_long("\x00\x80")
1256 -32768L
1257 >>> decode_long("\x80")
1258 -128L
1259 >>> decode_long("\x7f")
1260 127L
1261 """
Guido van Rossum3d8c01b2003-01-28 19:48:18 +00001262 # XXX This is quadratic too.
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001263 x = 0L
1264 i = 0L
1265 for c in data:
1266 x |= long(ord(c)) << i
1267 i += 8L
1268 if data and ord(c) >= 0x80:
1269 x -= 1L << i
1270 return x
1271
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001272# Shorthands
1273
Jeremy Hyltonabe2c622001-10-15 21:29:28 +00001274try:
1275 from cStringIO import StringIO
1276except ImportError:
1277 from StringIO import StringIO
Guido van Rossumc7c5e691996-07-22 22:26:07 +00001278
Guido van Rossum3a41c612003-01-28 15:10:22 +00001279def dump(obj, file, proto=1):
1280 Pickler(file, proto).dump(obj)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001281
Guido van Rossum3a41c612003-01-28 15:10:22 +00001282def dumps(obj, proto=1):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001283 file = StringIO()
Guido van Rossum3a41c612003-01-28 15:10:22 +00001284 Pickler(file, proto).dump(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001285 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001286
1287def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001288 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001289
1290def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001291 file = StringIO(str)
1292 return Unpickler(file).load()
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001293
1294# Doctest
1295
1296def _test():
1297 import doctest
1298 return doctest.testmod()
1299
1300if __name__ == "__main__":
1301 _test()