blob: 1cee4d53fa43872580a241160cf791dab9e52b43 [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 Rossum3d8c01b2003-01-28 19:48:18 +000030from copy_reg import dispatch_table, safe_constructors, _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
Guido van Rossumf29d3d62003-01-27 22:47:53 +000040# These are purely informational; no code usues these
41format_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
50# unpickling? struct.pack() is 40% faster than marshal.loads(), but
51# 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
Guido van Rossumff871742000-12-13 18:11:56 +000076class _Stop(Exception):
77 def __init__(self, value):
78 self.value = value
79
Guido van Rossum533dbcf2003-01-28 17:55:05 +000080# Jython has PyStringMap; it's a dict subclass with string keys
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000081try:
82 from org.python.core import PyStringMap
83except ImportError:
84 PyStringMap = None
85
Guido van Rossum533dbcf2003-01-28 17:55:05 +000086# UnicodeType may or may not be exported (normally imported from types)
Guido van Rossumdbb718f2001-09-21 19:22:34 +000087try:
88 UnicodeType
89except NameError:
90 UnicodeType = None
91
Tim Peters22a449a2003-01-27 20:16:36 +000092# Pickle opcodes. See pickletools.py for extensive docs. The listing
93# here is in kind-of alphabetical order of 1-character pickle code.
94# pickletools groups them by purpose.
Guido van Rossumdbb718f2001-09-21 19:22:34 +000095
Tim Peters22a449a2003-01-27 20:16:36 +000096MARK = '(' # push special markobject on stack
97STOP = '.' # every pickle ends with STOP
98POP = '0' # discard topmost stack item
99POP_MARK = '1' # discard stack top through topmost markobject
100DUP = '2' # duplicate top stack item
101FLOAT = 'F' # push float object; decimal string argument
102INT = 'I' # push integer or bool; decimal string argument
103BININT = 'J' # push four-byte signed int
104BININT1 = 'K' # push 1-byte unsigned int
105LONG = 'L' # push long; decimal string argument
106BININT2 = 'M' # push 2-byte unsigned int
107NONE = 'N' # push None
108PERSID = 'P' # push persistent object; id is taken from string arg
109BINPERSID = 'Q' # " " " ; " " " " stack
110REDUCE = 'R' # apply callable to argtuple, both on stack
111STRING = 'S' # push string; NL-terminated string argument
112BINSTRING = 'T' # push string; counted binary string argument
113SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes
114UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument
115BINUNICODE = 'X' # " " " ; counted UTF-8 string argument
116APPEND = 'a' # append stack top to list below it
117BUILD = 'b' # call __setstate__ or __dict__.update()
118GLOBAL = 'c' # push self.find_class(modname, name); 2 string args
119DICT = 'd' # build a dict from stack items
120EMPTY_DICT = '}' # push empty dict
121APPENDS = 'e' # extend list on stack by topmost stack slice
122GET = 'g' # push item from memo on stack; index is string arg
123BINGET = 'h' # " " " " " " ; " " 1-byte arg
124INST = 'i' # build & push class instance
125LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg
126LIST = 'l' # build list from topmost stack items
127EMPTY_LIST = ']' # push empty list
128OBJ = 'o' # build & push class instance
129PUT = 'p' # store stack top in memo; index is string arg
130BINPUT = 'q' # " " " " " ; " " 1-byte arg
131LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg
132SETITEM = 's' # add key+value pair to dict
133TUPLE = 't' # build tuple from topmost stack items
134EMPTY_TUPLE = ')' # push empty tuple
135SETITEMS = 'u' # modify dict by adding topmost key+value pairs
136BINFLOAT = 'G' # push float; arg is 8-byte float encoding
137
138TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py
139FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py
Guido van Rossum77f6a652002-04-03 22:41:51 +0000140
Tim Peterse1054782003-01-28 00:22:12 +0000141# Protocol 2 (not yet implemented).
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000142
Tim Peterse1054782003-01-28 00:22:12 +0000143PROTO = '\x80' # identify pickle protocol
144NEWOBJ = '\x81' # build object by applying cls.__new__ to argtuple
145EXT1 = '\x82' # push object from extension registry; 1-byte index
146EXT2 = '\x83' # ditto, but 2-byte index
147EXT4 = '\x84' # ditto, but 4-byte index
148TUPLE1 = '\x85' # build 1-tuple from stack top
149TUPLE2 = '\x86' # build 2-tuple from two topmost stack items
150TUPLE3 = '\x87' # build 3-tuple from three topmost stack items
151NEWTRUE = '\x88' # push True
152NEWFALSE = '\x89' # push False
153LONG1 = '\x8a' # push long from < 256 bytes
154LONG4 = '\x8b' # push really big long
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000155
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000156_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
157
Guido van Rossuma48061a1995-01-10 00:31:14 +0000158
Skip Montanaro23bafc62001-02-18 03:10:09 +0000159__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
Neal Norwitzd5ba4ae2002-02-11 18:12:06 +0000160del x
Skip Montanaro23bafc62001-02-18 03:10:09 +0000161
Guido van Rossum1be31752003-01-28 15:19:53 +0000162
163# Pickling machinery
164
Guido van Rossuma48061a1995-01-10 00:31:14 +0000165class Pickler:
166
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000167 def __init__(self, file, proto=1):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000168 """This takes a file-like object for writing a pickle data stream.
169
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000170 The optional proto argument tells the pickler to use the given
171 protocol; supported protocols are 0, 1, 2. The default
172 protocol is 1 (in previous Python versions the default was 0).
173
174 Protocol 1 is more efficient than protocol 0; protocol 2 is
175 more efficient than protocol 1. Protocol 2 is not the default
176 because it is not supported by older Python versions.
177
178 XXX Protocol 2 is not yet implemented.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000179
180 The file parameter must have a write() method that accepts a single
181 string argument. It can thus be an open file object, a StringIO
182 object, or any other custom object that meets this interface.
183
184 """
Guido van Rossum1be31752003-01-28 15:19:53 +0000185 if proto not in (0, 1, 2):
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000186 raise ValueError, "pickle protocol must be 0, 1 or 2"
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000187 self.write = file.write
188 self.memo = {}
Guido van Rossum1be31752003-01-28 15:19:53 +0000189 self.proto = int(proto)
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000190 self.bin = proto >= 1
Guido van Rossuma48061a1995-01-10 00:31:14 +0000191
Fred Drake7f781c92002-05-01 20:33:53 +0000192 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000193 """Clears the pickler's "memo".
194
195 The memo is the data structure that remembers which objects the
Tim Petersb377f8a2003-01-28 00:23:36 +0000196 pickler has already seen, so that shared or recursive objects are
197 pickled by reference and not by value. This method is useful when
198 re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000199
200 """
Fred Drake7f781c92002-05-01 20:33:53 +0000201 self.memo.clear()
202
Guido van Rossum3a41c612003-01-28 15:10:22 +0000203 def dump(self, obj):
204 """Write a pickled representation of obj to the open file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000205
206 Either the binary or ASCII format will be used, depending on the
207 value of the bin flag passed to the constructor.
208
209 """
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000210 if self.proto >= 2:
211 self.write(PROTO + chr(self.proto))
Guido van Rossum3a41c612003-01-28 15:10:22 +0000212 self.save(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000213 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000214
Jeremy Hylton3422c992003-01-24 19:29:52 +0000215 def memoize(self, obj):
216 """Store an object in the memo."""
217
Tim Peterse46b73f2003-01-27 21:22:10 +0000218 # The Pickler memo is a dictionary mapping object ids to 2-tuples
219 # that contain the Unpickler memo key and the object being memoized.
220 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000221 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000222 # Pickler memo so that transient objects are kept alive during
223 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000224
Tim Peterse46b73f2003-01-27 21:22:10 +0000225 # The use of the Unpickler memo length as the memo key is just a
226 # convention. The only requirement is that the memo values be unique.
227 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000228 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000229 # growable) array, indexed by memo key.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000230 memo_len = len(self.memo)
231 self.write(self.put(memo_len))
Tim Peters518df0d2003-01-28 01:00:38 +0000232 self.memo[id(obj)] = memo_len, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000233
Tim Petersbb38e302003-01-27 21:25:41 +0000234 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000235 def put(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000236 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000237 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000238 return BINPUT + chr(i)
239 else:
240 return LONG_BINPUT + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000241
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000242 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000243
Tim Petersbb38e302003-01-27 21:25:41 +0000244 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000245 def get(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000246 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000247 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000248 return BINGET + chr(i)
249 else:
250 return LONG_BINGET + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000251
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000252 return GET + `i` + '\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000253
Guido van Rossum533dbcf2003-01-28 17:55:05 +0000254 def save(self, obj,
255 _builtin_type = (int, long, float, complex, str, unicode,
256 tuple, list, dict),
257 ):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000258 # Check for persistent id (defined by a subclass)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000259 pid = self.persistent_id(obj)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000260 if pid:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000261 self.save_pers(pid)
262 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000263
Guido van Rossumbc64e222003-01-28 16:34:19 +0000264 # Check the memo
265 x = self.memo.get(id(obj))
266 if x:
267 self.write(self.get(x[0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000268 return
269
Guido van Rossumbc64e222003-01-28 16:34:19 +0000270 # Check the type dispatch table
Guido van Rossum3a41c612003-01-28 15:10:22 +0000271 t = type(obj)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000272 f = self.dispatch.get(t)
273 if f:
274 f(self, obj) # Call unbound method with explicit self
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000275 return
276
Guido van Rossumbc64e222003-01-28 16:34:19 +0000277 # Check for a class with a custom metaclass; treat as regular class
Tim Petersb32a8312003-01-28 00:48:09 +0000278 try:
279 issc = issubclass(t, TypeType)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000280 except TypeError: # t is not a class (old Boost; see SF #502085)
Tim Petersb32a8312003-01-28 00:48:09 +0000281 issc = 0
282 if issc:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000283 self.save_global(obj)
Tim Petersb32a8312003-01-28 00:48:09 +0000284 return
285
Guido van Rossumbc64e222003-01-28 16:34:19 +0000286 # Check copy_reg.dispatch_table
287 reduce = dispatch_table.get(t)
288 if reduce:
289 rv = reduce(obj)
Tim Petersb32a8312003-01-28 00:48:09 +0000290 else:
Guido van Rossumbc64e222003-01-28 16:34:19 +0000291 # Check for __reduce__ method
292 reduce = getattr(obj, "__reduce__", None)
293 if not reduce:
Guido van Rossum54fb1922003-01-28 18:22:35 +0000294 # Check for instance of subclass of common built-in types
295 if self.proto >= 2 and isinstance(obj, _builtin_type):
296 assert t not in _builtin_type # Proper subclass
297 self.save_newobj(obj)
298 return
Guido van Rossumbc64e222003-01-28 16:34:19 +0000299 raise PicklingError("Can't pickle %r object: %r" %
300 (t.__name__, obj))
301 rv = reduce()
Tim Petersb32a8312003-01-28 00:48:09 +0000302
Guido van Rossumbc64e222003-01-28 16:34:19 +0000303 # Check for string returned by reduce(), meaning "save as global"
304 if type(rv) is StringType:
305 self.save_global(obj, rv)
Tim Petersb32a8312003-01-28 00:48:09 +0000306 return
307
Guido van Rossumbc64e222003-01-28 16:34:19 +0000308 # Assert that reduce() returned a tuple
309 if type(rv) is not TupleType:
310 raise PicklingError("%s must return string or tuple" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000311
Guido van Rossumbc64e222003-01-28 16:34:19 +0000312 # Assert that it returned a 2-tuple or 3-tuple, and unpack it
313 l = len(rv)
314 if l == 2:
315 func, args = rv
Tim Petersb32a8312003-01-28 00:48:09 +0000316 state = None
Guido van Rossumbc64e222003-01-28 16:34:19 +0000317 elif l == 3:
318 func, args, state = rv
319 else:
320 raise PicklingError("Tuple returned by %s must have "
321 "exactly two or three elements" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000322
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000323 # XXX Temporary hack XXX
324 # Override the default __reduce__ for new-style class instances
325 if self.proto >= 2:
326 if func is _reconstructor:
327 self.save_newobj(obj)
328 return
329
Guido van Rossumbc64e222003-01-28 16:34:19 +0000330 # Save the reduce() output and finally memoize the object
331 self.save_reduce(func, args, state)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000332 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000333
Guido van Rossum3a41c612003-01-28 15:10:22 +0000334 def persistent_id(self, obj):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000335 # This exists so a subclass can override it
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000336 return None
337
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000338 def save_pers(self, pid):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000339 # Save a persistent id reference
Tim Petersbd1cdb92003-01-28 01:03:10 +0000340 if self.bin:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000341 self.save(pid)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000342 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000343 else:
344 self.write(PERSID + str(pid) + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000345
Guido van Rossumbc64e222003-01-28 16:34:19 +0000346 def save_reduce(self, func, args, state=None):
347 # This API is be called by some subclasses
348
349 # Assert that args is a tuple or None
350 if not isinstance(args, TupleType):
351 if args is None:
352 # A hack for Jim Fulton's ExtensionClass, now deprecated.
353 # See load_reduce()
354 warnings.warn("__basicnew__ special case is deprecated",
355 DeprecationWarning)
356 else:
357 raise PicklingError(
358 "args from reduce() should be a tuple")
359
360 # Assert that func is callable
361 if not callable(func):
362 raise PicklingError("func from reduce should be callable")
363
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000364 save = self.save
Guido van Rossumbc64e222003-01-28 16:34:19 +0000365 write = self.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000366
Guido van Rossumbc64e222003-01-28 16:34:19 +0000367 save(func)
368 save(args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000369 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000370
Tim Petersc32d8242001-04-10 02:48:53 +0000371 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000372 save(state)
373 write(BUILD)
374
Guido van Rossum54fb1922003-01-28 18:22:35 +0000375 def save_newobj(self, obj):
376 # Save a new-style class instance, using protocol 2.
377 # XXX Much of this is still experimental.
378 t = type(obj)
Guido van Rossum54fb1922003-01-28 18:22:35 +0000379 getnewargs = getattr(obj, "__getnewargs__", None)
380 if getnewargs:
381 args = getnewargs() # This better not reference obj
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000382 else:
383 for cls in int, long, float, complex, str, unicode, tuple:
384 if isinstance(obj, cls):
385 args = (cls(obj),)
386 break
387 else:
388 args = ()
389
390 save = self.save
391 write = self.write
392
Guido van Rossum54fb1922003-01-28 18:22:35 +0000393 self.save_global(t)
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000394 save(args)
395 write(NEWOBJ)
Guido van Rossum54fb1922003-01-28 18:22:35 +0000396 self.memoize(obj)
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000397
398 if isinstance(obj, list):
399 write(MARK)
400 for x in obj:
401 save(x)
402 write(APPENDS)
403 elif isinstance(obj, dict):
404 write(MARK)
405 for k, v in obj.iteritems():
406 save(k)
407 save(v)
408 write(SETITEMS)
409
Guido van Rossum54fb1922003-01-28 18:22:35 +0000410 getstate = getattr(obj, "__getstate__", None)
411 if getstate:
412 state = getstate()
413 else:
414 state = getattr(obj, "__dict__", None)
415 # XXX What about __slots__?
416 if state is not None:
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000417 save(state)
418 write(BUILD)
Guido van Rossum54fb1922003-01-28 18:22:35 +0000419
Guido van Rossumbc64e222003-01-28 16:34:19 +0000420 # Methods below this point are dispatched through the dispatch table
421
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000422 dispatch = {}
423
Guido van Rossum3a41c612003-01-28 15:10:22 +0000424 def save_none(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000425 self.write(NONE)
426 dispatch[NoneType] = save_none
427
Guido van Rossum3a41c612003-01-28 15:10:22 +0000428 def save_bool(self, obj):
Guido van Rossum7d97d312003-01-28 04:25:27 +0000429 if self.proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000430 self.write(obj and NEWTRUE or NEWFALSE)
Guido van Rossum7d97d312003-01-28 04:25:27 +0000431 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000432 self.write(obj and TRUE or FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000433 dispatch[bool] = save_bool
434
Guido van Rossum3a41c612003-01-28 15:10:22 +0000435 def save_int(self, obj, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000436 if self.bin:
Tim Peters44714002001-04-10 05:02:52 +0000437 # If the int is small enough to fit in a signed 4-byte 2's-comp
438 # format, we can store it more efficiently than the general
439 # case.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000440 # First one- and two-byte unsigned ints:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000441 if obj >= 0:
442 if obj <= 0xff:
443 self.write(BININT1 + chr(obj))
Guido van Rossum5c938d02003-01-28 03:03:08 +0000444 return
Guido van Rossum3a41c612003-01-28 15:10:22 +0000445 if obj <= 0xffff:
446 self.write(BININT2 + chr(obj&0xff) + chr(obj>>8))
Guido van Rossum5c938d02003-01-28 03:03:08 +0000447 return
448 # Next check for 4-byte signed ints:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000449 high_bits = obj >> 31 # note that Python shift sign-extends
Tim Petersd95c2df2003-01-28 03:41:54 +0000450 if high_bits == 0 or high_bits == -1:
Tim Peters44714002001-04-10 05:02:52 +0000451 # All high bits are copies of bit 2**31, so the value
452 # fits in a 4-byte signed int.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000453 self.write(BININT + pack("<i", obj))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000454 return
Tim Peters44714002001-04-10 05:02:52 +0000455 # Text pickle, or int too big to fit in signed 4-byte format.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000456 self.write(INT + `obj` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000457 dispatch[IntType] = save_int
458
Guido van Rossum3a41c612003-01-28 15:10:22 +0000459 def save_long(self, obj, pack=struct.pack):
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000460 if self.proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000461 bytes = encode_long(obj)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000462 n = len(bytes)
463 if n < 256:
464 self.write(LONG1 + chr(n) + bytes)
465 else:
466 self.write(LONG4 + pack("<i", n) + bytes)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000467 self.write(LONG + `obj` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000468 dispatch[LongType] = save_long
469
Guido van Rossum3a41c612003-01-28 15:10:22 +0000470 def save_float(self, obj, pack=struct.pack):
Guido van Rossumd3703791998-10-22 20:15:36 +0000471 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000472 self.write(BINFLOAT + pack('>d', obj))
Guido van Rossumd3703791998-10-22 20:15:36 +0000473 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000474 self.write(FLOAT + `obj` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000475 dispatch[FloatType] = save_float
476
Guido van Rossum3a41c612003-01-28 15:10:22 +0000477 def save_string(self, obj, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000478 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000479 n = len(obj)
Tim Petersbbf63cd2003-01-27 21:15:36 +0000480 if n < 256:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000481 self.write(SHORT_BINSTRING + chr(n) + obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000482 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000483 self.write(BINSTRING + pack("<i", n) + obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000484 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000485 self.write(STRING + `obj` + '\n')
486 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000487 dispatch[StringType] = save_string
488
Guido van Rossum3a41c612003-01-28 15:10:22 +0000489 def save_unicode(self, obj, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000490 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000491 encoding = obj.encode('utf-8')
Tim Petersbbf63cd2003-01-27 21:15:36 +0000492 n = len(encoding)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000493 self.write(BINUNICODE + pack("<i", n) + encoding)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000494 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000495 obj = obj.replace("\\", "\\u005c")
496 obj = obj.replace("\n", "\\u000a")
497 self.write(UNICODE + obj.encode('raw-unicode-escape') + '\n')
498 self.memoize(obj)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000499 dispatch[UnicodeType] = save_unicode
500
Guido van Rossum31584cb2001-01-22 14:53:29 +0000501 if StringType == UnicodeType:
502 # This is true for Jython
Guido van Rossum3a41c612003-01-28 15:10:22 +0000503 def save_string(self, obj, pack=struct.pack):
504 unicode = obj.isunicode()
Guido van Rossum31584cb2001-01-22 14:53:29 +0000505
Tim Petersc32d8242001-04-10 02:48:53 +0000506 if self.bin:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000507 if unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000508 obj = obj.encode("utf-8")
509 l = len(obj)
Tim Petersc32d8242001-04-10 02:48:53 +0000510 if l < 256 and not unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000511 self.write(SHORT_BINSTRING + chr(l) + obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000512 else:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000513 s = pack("<i", l)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000514 if unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000515 self.write(BINUNICODE + s + obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000516 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000517 self.write(BINSTRING + s + obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000518 else:
Tim Peters658cba62001-02-09 20:06:00 +0000519 if unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000520 obj = obj.replace("\\", "\\u005c")
521 obj = obj.replace("\n", "\\u000a")
522 obj = obj.encode('raw-unicode-escape')
523 self.write(UNICODE + obj + '\n')
Guido van Rossum31584cb2001-01-22 14:53:29 +0000524 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000525 self.write(STRING + `obj` + '\n')
526 self.memoize(obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000527 dispatch[StringType] = save_string
Tim Peters658cba62001-02-09 20:06:00 +0000528
Guido van Rossum3a41c612003-01-28 15:10:22 +0000529 def save_tuple(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000530 write = self.write
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000531 proto = self.proto
532
Guido van Rossum3a41c612003-01-28 15:10:22 +0000533 n = len(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000534 if n == 0 and proto:
535 write(EMPTY_TUPLE)
536 return
537
538 save = self.save
539 memo = self.memo
540 if n <= 3 and proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000541 for element in obj:
Tim Petersd97da802003-01-28 05:48:29 +0000542 save(element)
543 # Subtle. Same as in the big comment below.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000544 if id(obj) in memo:
545 get = self.get(memo[id(obj)][0])
Tim Petersd97da802003-01-28 05:48:29 +0000546 write(POP * n + get)
547 else:
548 write(_tuplesize2code[n])
Guido van Rossum3a41c612003-01-28 15:10:22 +0000549 self.memoize(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000550 return
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000551
Tim Petersff57bff2003-01-28 05:34:53 +0000552 # proto 0, or proto 1 and tuple isn't empty, or proto > 1 and tuple
553 # has more than 3 elements.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000554 write(MARK)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000555 for element in obj:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000556 save(element)
557
Guido van Rossum3a41c612003-01-28 15:10:22 +0000558 if n and id(obj) in memo:
Tim Petersf558da02003-01-28 02:09:55 +0000559 # Subtle. d was not in memo when we entered save_tuple(), so
560 # the process of saving the tuple's elements must have saved
561 # the tuple itself: the tuple is recursive. The proper action
562 # now is to throw away everything we put on the stack, and
563 # simply GET the tuple (it's already constructed). This check
564 # could have been done in the "for element" loop instead, but
565 # recursive tuples are a rare thing.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000566 get = self.get(memo[id(obj)][0])
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000567 if proto:
Tim Petersf558da02003-01-28 02:09:55 +0000568 write(POP_MARK + get)
569 else: # proto 0 -- POP_MARK not available
Tim Petersd97da802003-01-28 05:48:29 +0000570 write(POP * (n+1) + get)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000571 return
572
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000573 # No recursion (including the empty-tuple case for protocol 0).
Tim Peters518df0d2003-01-28 01:00:38 +0000574 self.write(TUPLE)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000575 if obj: # No need to memoize empty tuple
576 self.memoize(obj)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000577
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000578 dispatch[TupleType] = save_tuple
579
Tim Petersa6ae9a22003-01-28 16:58:41 +0000580 # save_empty_tuple() isn't used by anything in Python 2.3. However, I
581 # found a Pickler subclass in Zope3 that calls it, so it's not harmless
582 # to remove it.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000583 def save_empty_tuple(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000584 self.write(EMPTY_TUPLE)
585
Guido van Rossum3a41c612003-01-28 15:10:22 +0000586 def save_list(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000587 write = self.write
588 save = self.save
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000589
Tim Petersc32d8242001-04-10 02:48:53 +0000590 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000591 write(EMPTY_LIST)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000592 self.memoize(obj)
593 n = len(obj)
Tim Peters21c18f02003-01-28 01:15:46 +0000594 if n > 1:
595 write(MARK)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000596 for element in obj:
Tim Peters21c18f02003-01-28 01:15:46 +0000597 save(element)
598 write(APPENDS)
599 elif n:
600 assert n == 1
Guido van Rossum3a41c612003-01-28 15:10:22 +0000601 save(obj[0])
Tim Peters21c18f02003-01-28 01:15:46 +0000602 write(APPEND)
603 # else the list is empty, and we're already done
604
605 else: # proto 0 -- can't use EMPTY_LIST or APPENDS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000606 write(MARK + LIST)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000607 self.memoize(obj)
608 for element in obj:
Tim Peters21c18f02003-01-28 01:15:46 +0000609 save(element)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000610 write(APPEND)
611
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000612 dispatch[ListType] = save_list
613
Guido van Rossum3a41c612003-01-28 15:10:22 +0000614 def save_dict(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000615 write = self.write
616 save = self.save
Guido van Rossum3a41c612003-01-28 15:10:22 +0000617 items = obj.iteritems()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000618
Tim Petersc32d8242001-04-10 02:48:53 +0000619 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000620 write(EMPTY_DICT)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000621 self.memoize(obj)
622 if len(obj) > 1:
Tim Peters064567e2003-01-28 01:34:43 +0000623 write(MARK)
624 for key, value in items:
625 save(key)
626 save(value)
627 write(SETITEMS)
628 return
Tim Peters82ca59e2003-01-28 16:47:59 +0000629 # else (dict is empty or a singleton), fall through to the
630 # SETITEM code at the end
Tim Peters064567e2003-01-28 01:34:43 +0000631 else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000632 write(MARK + DICT)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000633 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000634
Guido van Rossum3a41c612003-01-28 15:10:22 +0000635 # proto 0 or len(obj) < 2
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000636 for key, value in items:
637 save(key)
638 save(value)
Tim Peters064567e2003-01-28 01:34:43 +0000639 write(SETITEM)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000640
641 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000642 if not PyStringMap is None:
643 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000644
Guido van Rossum3a41c612003-01-28 15:10:22 +0000645 def save_inst(self, obj):
646 cls = obj.__class__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000647
648 memo = self.memo
649 write = self.write
650 save = self.save
651
Guido van Rossum3a41c612003-01-28 15:10:22 +0000652 if hasattr(obj, '__getinitargs__'):
653 args = obj.__getinitargs__()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000654 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000655 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000656 else:
657 args = ()
658
659 write(MARK)
660
Tim Petersc32d8242001-04-10 02:48:53 +0000661 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000662 save(cls)
Tim Peters3b769832003-01-28 03:51:36 +0000663 for arg in args:
664 save(arg)
665 write(OBJ)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000666 else:
Tim Peters3b769832003-01-28 03:51:36 +0000667 for arg in args:
668 save(arg)
669 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000670
Guido van Rossum3a41c612003-01-28 15:10:22 +0000671 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000672
673 try:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000674 getstate = obj.__getstate__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000675 except AttributeError:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000676 stuff = obj.__dict__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000677 else:
678 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000679 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000680 save(stuff)
681 write(BUILD)
Tim Peters3b769832003-01-28 03:51:36 +0000682
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000683 dispatch[InstanceType] = save_inst
684
Guido van Rossum3a41c612003-01-28 15:10:22 +0000685 def save_global(self, obj, name = None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000686 write = self.write
687 memo = self.memo
688
Tim Petersc32d8242001-04-10 02:48:53 +0000689 if name is None:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000690 name = obj.__name__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000691
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000692 try:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000693 module = obj.__module__
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000694 except AttributeError:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000695 module = whichmodule(obj, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000696
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000697 try:
698 __import__(module)
699 mod = sys.modules[module]
700 klass = getattr(mod, name)
701 except (ImportError, KeyError, AttributeError):
702 raise PicklingError(
703 "Can't pickle %r: it's not found as %s.%s" %
Guido van Rossum3a41c612003-01-28 15:10:22 +0000704 (obj, module, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000705 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000706 if klass is not obj:
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000707 raise PicklingError(
708 "Can't pickle %r: it's not the same object as %s.%s" %
Guido van Rossum3a41c612003-01-28 15:10:22 +0000709 (obj, module, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000710
Tim Peters518df0d2003-01-28 01:00:38 +0000711 write(GLOBAL + module + '\n' + name + '\n')
Guido van Rossum3a41c612003-01-28 15:10:22 +0000712 self.memoize(obj)
Tim Peters3b769832003-01-28 03:51:36 +0000713
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000714 dispatch[ClassType] = save_global
715 dispatch[FunctionType] = save_global
716 dispatch[BuiltinFunctionType] = save_global
Tim Peters6d6c1a32001-08-02 04:15:00 +0000717 dispatch[TypeType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000718
Guido van Rossum1be31752003-01-28 15:19:53 +0000719# Pickling helpers
Guido van Rossuma48061a1995-01-10 00:31:14 +0000720
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000721def _keep_alive(x, memo):
722 """Keeps a reference to the object x in the memo.
723
724 Because we remember objects by their id, we have
725 to assure that possibly temporary objects are kept
726 alive by referencing them.
727 We store a reference at the id of the memo, which should
728 normally not be used unless someone tries to deepcopy
729 the memo itself...
730 """
731 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000732 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000733 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000734 # aha, this is the first one :-)
735 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000736
737
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000738classmap = {} # called classmap for backwards compatibility
Guido van Rossuma48061a1995-01-10 00:31:14 +0000739
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000740def whichmodule(func, funcname):
741 """Figure out the module in which a function occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000742
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000743 Search sys.modules for the module.
744 Cache in classmap.
745 Return a module name.
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000746 If the function cannot be found, return __main__.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000747 """
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000748 if func in classmap:
749 return classmap[func]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000750
751 for name, module in sys.modules.items():
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000752 if module is None:
Jeremy Hylton065a5ab2002-09-19 22:57:26 +0000753 continue # skip dummy package entries
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000754 if name != '__main__' and \
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000755 hasattr(module, funcname) and \
756 getattr(module, funcname) is func:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000757 break
758 else:
759 name = '__main__'
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000760 classmap[func] = name
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000761 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000762
763
Guido van Rossum1be31752003-01-28 15:19:53 +0000764# Unpickling machinery
765
Guido van Rossuma48061a1995-01-10 00:31:14 +0000766class Unpickler:
767
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000768 def __init__(self, file):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000769 """This takes a file-like object for reading a pickle data stream.
770
771 This class automatically determines whether the data stream was
772 written in binary mode or not, so it does not need a flag as in
773 the Pickler class factory.
774
775 The file-like object must have two methods, a read() method that
776 takes an integer argument, and a readline() method that requires no
777 arguments. Both methods should return a string. Thus file-like
778 object can be a file object opened for reading, a StringIO object,
779 or any other custom object that meets this interface.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000780 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000781 self.readline = file.readline
782 self.read = file.read
783 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000784
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000785 def load(self):
Guido van Rossum3a41c612003-01-28 15:10:22 +0000786 """Read a pickled object representation from the open file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000787
Guido van Rossum3a41c612003-01-28 15:10:22 +0000788 Return the reconstituted object hierarchy specified in the file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000789 """
Jeremy Hylton20747fa2001-11-09 16:15:04 +0000790 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000791 self.stack = []
792 self.append = self.stack.append
793 read = self.read
794 dispatch = self.dispatch
795 try:
796 while 1:
797 key = read(1)
798 dispatch[key](self)
Guido van Rossumff871742000-12-13 18:11:56 +0000799 except _Stop, stopinst:
800 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000801
Tim Petersc23d18a2003-01-28 01:41:51 +0000802 # Return largest index k such that self.stack[k] is self.mark.
803 # If the stack doesn't contain a mark, eventually raises IndexError.
804 # This could be sped by maintaining another stack, of indices at which
805 # the mark appears. For that matter, the latter stack would suffice,
806 # and we wouldn't need to push mark objects on self.stack at all.
807 # Doing so is probably a good thing, though, since if the pickle is
808 # corrupt (or hostile) we may get a clue from finding self.mark embedded
809 # in unpickled objects.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000810 def marker(self):
811 stack = self.stack
812 mark = self.mark
813 k = len(stack)-1
814 while stack[k] is not mark: k = k-1
815 return k
816
817 dispatch = {}
818
819 def load_eof(self):
820 raise EOFError
821 dispatch[''] = load_eof
822
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000823 def load_proto(self):
824 proto = ord(self.read(1))
825 if not 0 <= proto <= 2:
826 raise ValueError, "unsupported pickle protocol: %d" % proto
827 dispatch[PROTO] = load_proto
828
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000829 def load_persid(self):
830 pid = self.readline()[:-1]
831 self.append(self.persistent_load(pid))
832 dispatch[PERSID] = load_persid
833
834 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000835 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000836 self.append(self.persistent_load(pid))
837 dispatch[BINPERSID] = load_binpersid
838
839 def load_none(self):
840 self.append(None)
841 dispatch[NONE] = load_none
842
Guido van Rossum7d97d312003-01-28 04:25:27 +0000843 def load_false(self):
844 self.append(False)
845 dispatch[NEWFALSE] = load_false
846
847 def load_true(self):
848 self.append(True)
849 dispatch[NEWTRUE] = load_true
850
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000851 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000852 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +0000853 if data == FALSE[1:]:
854 val = False
855 elif data == TRUE[1:]:
856 val = True
857 else:
858 try:
859 val = int(data)
860 except ValueError:
861 val = long(data)
862 self.append(val)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000863 dispatch[INT] = load_int
864
865 def load_binint(self):
866 self.append(mloads('i' + self.read(4)))
867 dispatch[BININT] = load_binint
868
869 def load_binint1(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000870 self.append(ord(self.read(1)))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000871 dispatch[BININT1] = load_binint1
872
873 def load_binint2(self):
874 self.append(mloads('i' + self.read(2) + '\000\000'))
875 dispatch[BININT2] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +0000876
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000877 def load_long(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000878 self.append(long(self.readline()[:-1], 0))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000879 dispatch[LONG] = load_long
880
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000881 def load_long1(self):
882 n = ord(self.read(1))
883 bytes = self.read(n)
884 return decode_long(bytes)
885 dispatch[LONG1] = load_long1
886
887 def load_long4(self):
888 n = mloads('i' + self.read(4))
889 bytes = self.read(n)
890 return decode_long(bytes)
891 dispatch[LONG4] = load_long4
892
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000893 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000894 self.append(float(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000895 dispatch[FLOAT] = load_float
896
Guido van Rossumd3703791998-10-22 20:15:36 +0000897 def load_binfloat(self, unpack=struct.unpack):
898 self.append(unpack('>d', self.read(8))[0])
899 dispatch[BINFLOAT] = load_binfloat
900
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000901 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000902 rep = self.readline()[:-1]
Tim Petersad5a7712003-01-28 16:23:33 +0000903 for q in "\"'": # double or single quote
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000904 if rep.startswith(q):
905 if not rep.endswith(q):
906 raise ValueError, "insecure string pickle"
907 rep = rep[len(q):-len(q)]
908 break
909 else:
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000910 raise ValueError, "insecure string pickle"
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000911 self.append(rep.decode("string-escape"))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000912 dispatch[STRING] = load_string
913
914 def load_binstring(self):
915 len = mloads('i' + self.read(4))
916 self.append(self.read(len))
917 dispatch[BINSTRING] = load_binstring
918
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000919 def load_unicode(self):
920 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
921 dispatch[UNICODE] = load_unicode
922
923 def load_binunicode(self):
924 len = mloads('i' + self.read(4))
925 self.append(unicode(self.read(len),'utf-8'))
926 dispatch[BINUNICODE] = load_binunicode
927
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000928 def load_short_binstring(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000929 len = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000930 self.append(self.read(len))
931 dispatch[SHORT_BINSTRING] = load_short_binstring
932
933 def load_tuple(self):
934 k = self.marker()
935 self.stack[k:] = [tuple(self.stack[k+1:])]
936 dispatch[TUPLE] = load_tuple
937
938 def load_empty_tuple(self):
939 self.stack.append(())
940 dispatch[EMPTY_TUPLE] = load_empty_tuple
941
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000942 def load_tuple1(self):
943 self.stack[-1] = (self.stack[-1],)
944 dispatch[TUPLE1] = load_tuple1
945
946 def load_tuple2(self):
947 self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
948 dispatch[TUPLE2] = load_tuple2
949
950 def load_tuple3(self):
951 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
952 dispatch[TUPLE3] = load_tuple3
953
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000954 def load_empty_list(self):
955 self.stack.append([])
956 dispatch[EMPTY_LIST] = load_empty_list
957
958 def load_empty_dictionary(self):
959 self.stack.append({})
960 dispatch[EMPTY_DICT] = load_empty_dictionary
961
962 def load_list(self):
963 k = self.marker()
964 self.stack[k:] = [self.stack[k+1:]]
965 dispatch[LIST] = load_list
966
967 def load_dict(self):
968 k = self.marker()
969 d = {}
970 items = self.stack[k+1:]
971 for i in range(0, len(items), 2):
972 key = items[i]
973 value = items[i+1]
974 d[key] = value
975 self.stack[k:] = [d]
976 dispatch[DICT] = load_dict
977
978 def load_inst(self):
979 k = self.marker()
980 args = tuple(self.stack[k+1:])
981 del self.stack[k:]
982 module = self.readline()[:-1]
983 name = self.readline()[:-1]
984 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000985 instantiated = 0
986 if (not args and type(klass) is ClassType and
987 not hasattr(klass, "__getinitargs__")):
988 try:
989 value = _EmptyClass()
990 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000991 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000992 except RuntimeError:
993 # In restricted execution, assignment to inst.__class__ is
994 # prohibited
995 pass
996 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000997 try:
Barry Warsawbf4d9592001-11-15 23:42:58 +0000998 if not hasattr(klass, '__safe_for_unpickling__'):
999 raise UnpicklingError('%s is not safe for unpickling' %
1000 klass)
Guido van Rossum743d17e1998-09-15 20:25:57 +00001001 value = apply(klass, args)
1002 except TypeError, err:
1003 raise TypeError, "in constructor for %s: %s" % (
1004 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001005 self.append(value)
1006 dispatch[INST] = load_inst
1007
1008 def load_obj(self):
1009 stack = self.stack
1010 k = self.marker()
1011 klass = stack[k + 1]
1012 del stack[k + 1]
Tim Peters2344fae2001-01-15 00:50:52 +00001013 args = tuple(stack[k + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001014 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001015 instantiated = 0
1016 if (not args and type(klass) is ClassType and
1017 not hasattr(klass, "__getinitargs__")):
1018 try:
1019 value = _EmptyClass()
1020 value.__class__ = klass
1021 instantiated = 1
1022 except RuntimeError:
1023 # In restricted execution, assignment to inst.__class__ is
1024 # prohibited
1025 pass
1026 if not instantiated:
1027 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001028 self.append(value)
Tim Peters2344fae2001-01-15 00:50:52 +00001029 dispatch[OBJ] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001030
Guido van Rossum3a41c612003-01-28 15:10:22 +00001031 def load_newobj(self):
1032 args = self.stack.pop()
1033 cls = self.stack[-1]
1034 obj = cls.__new__(cls, *args)
Guido van Rossum533dbcf2003-01-28 17:55:05 +00001035 self.stack[-1] = obj
Guido van Rossum3a41c612003-01-28 15:10:22 +00001036 dispatch[NEWOBJ] = load_newobj
1037
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001038 def load_global(self):
1039 module = self.readline()[:-1]
1040 name = self.readline()[:-1]
1041 klass = self.find_class(module, name)
1042 self.append(klass)
1043 dispatch[GLOBAL] = load_global
1044
1045 def find_class(self, module, name):
Barry Warsawbf4d9592001-11-15 23:42:58 +00001046 __import__(module)
1047 mod = sys.modules[module]
1048 klass = getattr(mod, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001049 return klass
1050
1051 def load_reduce(self):
1052 stack = self.stack
1053
1054 callable = stack[-2]
1055 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001056 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001057
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001058 if type(callable) is not ClassType:
Raymond Hettinger54f02222002-06-01 14:18:47 +00001059 if not callable in safe_constructors:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001060 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001061 safe = callable.__safe_for_unpickling__
1062 except AttributeError:
1063 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +00001064
Tim Petersc32d8242001-04-10 02:48:53 +00001065 if not safe:
Tim Peters2344fae2001-01-15 00:50:52 +00001066 raise UnpicklingError, "%s is not safe for " \
1067 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +00001068
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001069 if arg_tup is None:
Guido van Rossumbc64e222003-01-28 16:34:19 +00001070 # A hack for Jim Fulton's ExtensionClass, now deprecated
1071 warnings.warn("__basicnew__ special case is deprecated",
Tim Peters8ac14952002-05-23 15:15:30 +00001072 DeprecationWarning)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001073 value = callable.__basicnew__()
1074 else:
1075 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001076 self.append(value)
1077 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +00001078
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001079 def load_pop(self):
1080 del self.stack[-1]
1081 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +00001082
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001083 def load_pop_mark(self):
1084 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001085 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001086 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001087
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001088 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +00001089 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001090 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +00001091
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001092 def load_get(self):
1093 self.append(self.memo[self.readline()[:-1]])
1094 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +00001095
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001096 def load_binget(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001097 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001098 self.append(self.memo[`i`])
1099 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001100
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001101 def load_long_binget(self):
1102 i = mloads('i' + self.read(4))
1103 self.append(self.memo[`i`])
1104 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001105
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001106 def load_put(self):
1107 self.memo[self.readline()[:-1]] = self.stack[-1]
1108 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001109
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001110 def load_binput(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001111 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001112 self.memo[`i`] = self.stack[-1]
1113 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001114
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001115 def load_long_binput(self):
1116 i = mloads('i' + self.read(4))
1117 self.memo[`i`] = self.stack[-1]
1118 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001119
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001120 def load_append(self):
1121 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001122 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001123 list = stack[-1]
1124 list.append(value)
1125 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001126
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001127 def load_appends(self):
1128 stack = self.stack
1129 mark = self.marker()
1130 list = stack[mark - 1]
Tim Peters209ad952003-01-28 01:44:45 +00001131 list.extend(stack[mark + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001132 del stack[mark:]
1133 dispatch[APPENDS] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001134
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001135 def load_setitem(self):
1136 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001137 value = stack.pop()
1138 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001139 dict = stack[-1]
1140 dict[key] = value
1141 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001142
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001143 def load_setitems(self):
1144 stack = self.stack
1145 mark = self.marker()
1146 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001147 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001148 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001149
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001150 del stack[mark:]
1151 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001152
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001153 def load_build(self):
1154 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001155 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001156 inst = stack[-1]
1157 try:
1158 setstate = inst.__setstate__
1159 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001160 try:
1161 inst.__dict__.update(value)
1162 except RuntimeError:
1163 # XXX In restricted execution, the instance's __dict__ is not
1164 # accessible. Use the old way of unpickling the instance
1165 # variables. This is a semantic different when unpickling in
1166 # restricted vs. unrestricted modes.
1167 for k, v in value.items():
1168 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001169 else:
1170 setstate(value)
1171 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001172
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001173 def load_mark(self):
1174 self.append(self.mark)
1175 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001176
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001177 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001178 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001179 raise _Stop(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001180 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001181
Guido van Rossume467be61997-12-05 19:42:42 +00001182# Helper class for load_inst/load_obj
1183
1184class _EmptyClass:
1185 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +00001186
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001187# Encode/decode longs.
1188
1189def encode_long(x):
1190 r"""Encode a long to a two's complement little-ending binary string.
1191 >>> encode_long(255L)
1192 '\xff\x00'
1193 >>> encode_long(32767L)
1194 '\xff\x7f'
1195 >>> encode_long(-256L)
1196 '\x00\xff'
1197 >>> encode_long(-32768L)
1198 '\x00\x80'
1199 >>> encode_long(-128L)
1200 '\x80'
1201 >>> encode_long(127L)
1202 '\x7f'
1203 >>>
1204 """
Guido van Rossum3d8c01b2003-01-28 19:48:18 +00001205 # XXX This is still a quadratic algorithm.
1206 # Should use hex() to get started.
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001207 digits = []
1208 while not -128 <= x < 128:
1209 digits.append(x & 0xff)
1210 x >>= 8
1211 digits.append(x & 0xff)
1212 return "".join(map(chr, digits))
1213
1214def decode_long(data):
1215 r"""Decode a long from a two's complement little-endian binary string.
1216 >>> decode_long("\xff\x00")
1217 255L
1218 >>> decode_long("\xff\x7f")
1219 32767L
1220 >>> decode_long("\x00\xff")
1221 -256L
1222 >>> decode_long("\x00\x80")
1223 -32768L
1224 >>> decode_long("\x80")
1225 -128L
1226 >>> decode_long("\x7f")
1227 127L
1228 """
Guido van Rossum3d8c01b2003-01-28 19:48:18 +00001229 # XXX This is quadratic too.
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001230 x = 0L
1231 i = 0L
1232 for c in data:
1233 x |= long(ord(c)) << i
1234 i += 8L
1235 if data and ord(c) >= 0x80:
1236 x -= 1L << i
1237 return x
1238
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001239# Shorthands
1240
Jeremy Hyltonabe2c622001-10-15 21:29:28 +00001241try:
1242 from cStringIO import StringIO
1243except ImportError:
1244 from StringIO import StringIO
Guido van Rossumc7c5e691996-07-22 22:26:07 +00001245
Guido van Rossum3a41c612003-01-28 15:10:22 +00001246def dump(obj, file, proto=1):
1247 Pickler(file, proto).dump(obj)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001248
Guido van Rossum3a41c612003-01-28 15:10:22 +00001249def dumps(obj, proto=1):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001250 file = StringIO()
Guido van Rossum3a41c612003-01-28 15:10:22 +00001251 Pickler(file, proto).dump(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001252 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001253
1254def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001255 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001256
1257def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001258 file = StringIO(str)
1259 return Unpickler(file).load()
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001260
1261# Doctest
1262
1263def _test():
1264 import doctest
1265 return doctest.testmod()
1266
1267if __name__ == "__main__":
1268 _test()