pickle.py 46 KB

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