fallback.py 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. """Fallback pure Python implementation of msgpack"""
  2. from datetime import datetime as _DateTime
  3. import sys
  4. import struct
  5. PY2 = sys.version_info[0] == 2
  6. if PY2:
  7. int_types = (int, long)
  8. def dict_iteritems(d):
  9. return d.iteritems()
  10. else:
  11. int_types = int
  12. unicode = str
  13. xrange = range
  14. def dict_iteritems(d):
  15. return d.items()
  16. if sys.version_info < (3, 5):
  17. # Ugly hack...
  18. RecursionError = RuntimeError
  19. def _is_recursionerror(e):
  20. return (
  21. len(e.args) == 1
  22. and isinstance(e.args[0], str)
  23. and e.args[0].startswith("maximum recursion depth exceeded")
  24. )
  25. else:
  26. def _is_recursionerror(e):
  27. return True
  28. if hasattr(sys, "pypy_version_info"):
  29. # StringIO is slow on PyPy, StringIO is faster. However: PyPy's own
  30. # StringBuilder is fastest.
  31. from __pypy__ import newlist_hint
  32. try:
  33. from __pypy__.builders import BytesBuilder as StringBuilder
  34. except ImportError:
  35. from __pypy__.builders import StringBuilder
  36. USING_STRINGBUILDER = True
  37. class StringIO(object):
  38. def __init__(self, s=b""):
  39. if s:
  40. self.builder = StringBuilder(len(s))
  41. self.builder.append(s)
  42. else:
  43. self.builder = StringBuilder()
  44. def write(self, s):
  45. if isinstance(s, memoryview):
  46. s = s.tobytes()
  47. elif isinstance(s, bytearray):
  48. s = bytes(s)
  49. self.builder.append(s)
  50. def getvalue(self):
  51. return self.builder.build()
  52. else:
  53. USING_STRINGBUILDER = False
  54. from io import BytesIO as StringIO
  55. newlist_hint = lambda size: []
  56. from .exceptions import BufferFull, OutOfData, ExtraData, FormatError, StackError
  57. from .ext import ExtType, Timestamp
  58. EX_SKIP = 0
  59. EX_CONSTRUCT = 1
  60. EX_READ_ARRAY_HEADER = 2
  61. EX_READ_MAP_HEADER = 3
  62. TYPE_IMMEDIATE = 0
  63. TYPE_ARRAY = 1
  64. TYPE_MAP = 2
  65. TYPE_RAW = 3
  66. TYPE_BIN = 4
  67. TYPE_EXT = 5
  68. DEFAULT_RECURSE_LIMIT = 511
  69. def _check_type_strict(obj, t, type=type, tuple=tuple):
  70. if type(t) is tuple:
  71. return type(obj) in t
  72. else:
  73. return type(obj) is t
  74. def _get_data_from_buffer(obj):
  75. view = memoryview(obj)
  76. if view.itemsize != 1:
  77. raise ValueError("cannot unpack from multi-byte object")
  78. return view
  79. def unpackb(packed, **kwargs):
  80. """
  81. Unpack an object from `packed`.
  82. Raises ``ExtraData`` when *packed* contains extra bytes.
  83. Raises ``ValueError`` when *packed* is incomplete.
  84. Raises ``FormatError`` when *packed* is not valid msgpack.
  85. Raises ``StackError`` when *packed* contains too nested.
  86. Other exceptions can be raised during unpacking.
  87. See :class:`Unpacker` for options.
  88. """
  89. unpacker = Unpacker(None, max_buffer_size=len(packed), **kwargs)
  90. unpacker.feed(packed)
  91. try:
  92. ret = unpacker._unpack()
  93. except OutOfData:
  94. raise ValueError("Unpack failed: incomplete input")
  95. except RecursionError as e:
  96. if _is_recursionerror(e):
  97. raise StackError
  98. raise
  99. if unpacker._got_extradata():
  100. raise ExtraData(ret, unpacker._get_extradata())
  101. return ret
  102. if sys.version_info < (2, 7, 6):
  103. def _unpack_from(f, b, o=0):
  104. """Explicit type cast for legacy struct.unpack_from"""
  105. return struct.unpack_from(f, bytes(b), o)
  106. else:
  107. _unpack_from = struct.unpack_from
  108. class Unpacker(object):
  109. """Streaming unpacker.
  110. Arguments:
  111. :param file_like:
  112. File-like object having `.read(n)` method.
  113. If specified, unpacker reads serialized data from it and :meth:`feed()` is not usable.
  114. :param int read_size:
  115. Used as `file_like.read(read_size)`. (default: `min(16*1024, max_buffer_size)`)
  116. :param bool use_list:
  117. If true, unpack msgpack array to Python list.
  118. Otherwise, unpack to Python tuple. (default: True)
  119. :param bool raw:
  120. If true, unpack msgpack raw to Python bytes.
  121. Otherwise, unpack to Python str by decoding with UTF-8 encoding (default).
  122. :param int timestamp:
  123. Control how timestamp type is unpacked:
  124. 0 - Timestamp
  125. 1 - float (Seconds from the EPOCH)
  126. 2 - int (Nanoseconds from the EPOCH)
  127. 3 - datetime.datetime (UTC). Python 2 is not supported.
  128. :param bool strict_map_key:
  129. If true (default), only str or bytes are accepted for map (dict) keys.
  130. :param callable object_hook:
  131. When specified, it should be callable.
  132. Unpacker calls it with a dict argument after unpacking msgpack map.
  133. (See also simplejson)
  134. :param callable object_pairs_hook:
  135. When specified, it should be callable.
  136. Unpacker calls it with a list of key-value pairs after unpacking msgpack map.
  137. (See also simplejson)
  138. :param str unicode_errors:
  139. The error handler for decoding unicode. (default: 'strict')
  140. This option should be used only when you have msgpack data which
  141. contains invalid UTF-8 string.
  142. :param int max_buffer_size:
  143. Limits size of data waiting unpacked. 0 means 2**32-1.
  144. The default value is 100*1024*1024 (100MiB).
  145. Raises `BufferFull` exception when it is insufficient.
  146. You should set this parameter when unpacking data from untrusted source.
  147. :param int max_str_len:
  148. Deprecated, use *max_buffer_size* instead.
  149. Limits max length of str. (default: max_buffer_size)
  150. :param int max_bin_len:
  151. Deprecated, use *max_buffer_size* instead.
  152. Limits max length of bin. (default: max_buffer_size)
  153. :param int max_array_len:
  154. Limits max length of array.
  155. (default: max_buffer_size)
  156. :param int max_map_len:
  157. Limits max length of map.
  158. (default: max_buffer_size//2)
  159. :param int max_ext_len:
  160. Deprecated, use *max_buffer_size* instead.
  161. Limits max size of ext type. (default: max_buffer_size)
  162. Example of streaming deserialize from file-like object::
  163. unpacker = Unpacker(file_like)
  164. for o in unpacker:
  165. process(o)
  166. Example of streaming deserialize from socket::
  167. unpacker = Unpacker(max_buffer_size)
  168. while True:
  169. buf = sock.recv(1024**2)
  170. if not buf:
  171. break
  172. unpacker.feed(buf)
  173. for o in unpacker:
  174. process(o)
  175. Raises ``ExtraData`` when *packed* contains extra bytes.
  176. Raises ``OutOfData`` when *packed* is incomplete.
  177. Raises ``FormatError`` when *packed* is not valid msgpack.
  178. Raises ``StackError`` when *packed* contains too nested.
  179. Other exceptions can be raised during unpacking.
  180. """
  181. def __init__(
  182. self,
  183. file_like=None,
  184. read_size=0,
  185. use_list=True,
  186. raw=False,
  187. timestamp=0,
  188. strict_map_key=True,
  189. object_hook=None,
  190. object_pairs_hook=None,
  191. list_hook=None,
  192. unicode_errors=None,
  193. max_buffer_size=100 * 1024 * 1024,
  194. ext_hook=ExtType,
  195. max_str_len=-1,
  196. max_bin_len=-1,
  197. max_array_len=-1,
  198. max_map_len=-1,
  199. max_ext_len=-1,
  200. ):
  201. if unicode_errors is None:
  202. unicode_errors = "strict"
  203. if file_like is None:
  204. self._feeding = True
  205. else:
  206. if not callable(file_like.read):
  207. raise TypeError("`file_like.read` must be callable")
  208. self.file_like = file_like
  209. self._feeding = False
  210. #: array of bytes fed.
  211. self._buffer = bytearray()
  212. #: Which position we currently reads
  213. self._buff_i = 0
  214. # When Unpacker is used as an iterable, between the calls to next(),
  215. # the buffer is not "consumed" completely, for efficiency sake.
  216. # Instead, it is done sloppily. To make sure we raise BufferFull at
  217. # the correct moments, we have to keep track of how sloppy we were.
  218. # Furthermore, when the buffer is incomplete (that is: in the case
  219. # we raise an OutOfData) we need to rollback the buffer to the correct
  220. # state, which _buf_checkpoint records.
  221. self._buf_checkpoint = 0
  222. if not max_buffer_size:
  223. max_buffer_size = 2 ** 31 - 1
  224. if max_str_len == -1:
  225. max_str_len = max_buffer_size
  226. if max_bin_len == -1:
  227. max_bin_len = max_buffer_size
  228. if max_array_len == -1:
  229. max_array_len = max_buffer_size
  230. if max_map_len == -1:
  231. max_map_len = max_buffer_size // 2
  232. if max_ext_len == -1:
  233. max_ext_len = max_buffer_size
  234. self._max_buffer_size = max_buffer_size
  235. if read_size > self._max_buffer_size:
  236. raise ValueError("read_size must be smaller than max_buffer_size")
  237. self._read_size = read_size or min(self._max_buffer_size, 16 * 1024)
  238. self._raw = bool(raw)
  239. self._strict_map_key = bool(strict_map_key)
  240. self._unicode_errors = unicode_errors
  241. self._use_list = use_list
  242. if not (0 <= timestamp <= 3):
  243. raise ValueError("timestamp must be 0..3")
  244. self._timestamp = timestamp
  245. self._list_hook = list_hook
  246. self._object_hook = object_hook
  247. self._object_pairs_hook = object_pairs_hook
  248. self._ext_hook = ext_hook
  249. self._max_str_len = max_str_len
  250. self._max_bin_len = max_bin_len
  251. self._max_array_len = max_array_len
  252. self._max_map_len = max_map_len
  253. self._max_ext_len = max_ext_len
  254. self._stream_offset = 0
  255. if list_hook is not None and not callable(list_hook):
  256. raise TypeError("`list_hook` is not callable")
  257. if object_hook is not None and not callable(object_hook):
  258. raise TypeError("`object_hook` is not callable")
  259. if object_pairs_hook is not None and not callable(object_pairs_hook):
  260. raise TypeError("`object_pairs_hook` is not callable")
  261. if object_hook is not None and object_pairs_hook is not None:
  262. raise TypeError(
  263. "object_pairs_hook and object_hook are mutually " "exclusive"
  264. )
  265. if not callable(ext_hook):
  266. raise TypeError("`ext_hook` is not callable")
  267. def feed(self, next_bytes):
  268. assert self._feeding
  269. view = _get_data_from_buffer(next_bytes)
  270. if len(self._buffer) - self._buff_i + len(view) > self._max_buffer_size:
  271. raise BufferFull
  272. # Strip buffer before checkpoint before reading file.
  273. if self._buf_checkpoint > 0:
  274. del self._buffer[: self._buf_checkpoint]
  275. self._buff_i -= self._buf_checkpoint
  276. self._buf_checkpoint = 0
  277. # Use extend here: INPLACE_ADD += doesn't reliably typecast memoryview in jython
  278. self._buffer.extend(view)
  279. def _consume(self):
  280. """ Gets rid of the used parts of the buffer. """
  281. self._stream_offset += self._buff_i - self._buf_checkpoint
  282. self._buf_checkpoint = self._buff_i
  283. def _got_extradata(self):
  284. return self._buff_i < len(self._buffer)
  285. def _get_extradata(self):
  286. return self._buffer[self._buff_i :]
  287. def read_bytes(self, n):
  288. ret = self._read(n)
  289. self._consume()
  290. return ret
  291. def _read(self, n):
  292. # (int) -> bytearray
  293. self._reserve(n)
  294. i = self._buff_i
  295. self._buff_i = i + n
  296. return self._buffer[i : i + n]
  297. def _reserve(self, n):
  298. remain_bytes = len(self._buffer) - self._buff_i - n
  299. # Fast path: buffer has n bytes already
  300. if remain_bytes >= 0:
  301. return
  302. if self._feeding:
  303. self._buff_i = self._buf_checkpoint
  304. raise OutOfData
  305. # Strip buffer before checkpoint before reading file.
  306. if self._buf_checkpoint > 0:
  307. del self._buffer[: self._buf_checkpoint]
  308. self._buff_i -= self._buf_checkpoint
  309. self._buf_checkpoint = 0
  310. # Read from file
  311. remain_bytes = -remain_bytes
  312. while remain_bytes > 0:
  313. to_read_bytes = max(self._read_size, remain_bytes)
  314. read_data = self.file_like.read(to_read_bytes)
  315. if not read_data:
  316. break
  317. assert isinstance(read_data, bytes)
  318. self._buffer += read_data
  319. remain_bytes -= len(read_data)
  320. if len(self._buffer) < n + self._buff_i:
  321. self._buff_i = 0 # rollback
  322. raise OutOfData
  323. def _read_header(self, execute=EX_CONSTRUCT):
  324. typ = TYPE_IMMEDIATE
  325. n = 0
  326. obj = None
  327. self._reserve(1)
  328. b = self._buffer[self._buff_i]
  329. self._buff_i += 1
  330. if b & 0b10000000 == 0:
  331. obj = b
  332. elif b & 0b11100000 == 0b11100000:
  333. obj = -1 - (b ^ 0xFF)
  334. elif b & 0b11100000 == 0b10100000:
  335. n = b & 0b00011111
  336. typ = TYPE_RAW
  337. if n > self._max_str_len:
  338. raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
  339. obj = self._read(n)
  340. elif b & 0b11110000 == 0b10010000:
  341. n = b & 0b00001111
  342. typ = TYPE_ARRAY
  343. if n > self._max_array_len:
  344. raise ValueError("%s exceeds max_array_len(%s)", n, self._max_array_len)
  345. elif b & 0b11110000 == 0b10000000:
  346. n = b & 0b00001111
  347. typ = TYPE_MAP
  348. if n > self._max_map_len:
  349. raise ValueError("%s exceeds max_map_len(%s)", n, self._max_map_len)
  350. elif b == 0xC0:
  351. obj = None
  352. elif b == 0xC2:
  353. obj = False
  354. elif b == 0xC3:
  355. obj = True
  356. elif b == 0xC4:
  357. typ = TYPE_BIN
  358. self._reserve(1)
  359. n = self._buffer[self._buff_i]
  360. self._buff_i += 1
  361. if n > self._max_bin_len:
  362. raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
  363. obj = self._read(n)
  364. elif b == 0xC5:
  365. typ = TYPE_BIN
  366. self._reserve(2)
  367. n = _unpack_from(">H", self._buffer, self._buff_i)[0]
  368. self._buff_i += 2
  369. if n > self._max_bin_len:
  370. raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
  371. obj = self._read(n)
  372. elif b == 0xC6:
  373. typ = TYPE_BIN
  374. self._reserve(4)
  375. n = _unpack_from(">I", self._buffer, self._buff_i)[0]
  376. self._buff_i += 4
  377. if n > self._max_bin_len:
  378. raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
  379. obj = self._read(n)
  380. elif b == 0xC7: # ext 8
  381. typ = TYPE_EXT
  382. self._reserve(2)
  383. L, n = _unpack_from("Bb", self._buffer, self._buff_i)
  384. self._buff_i += 2
  385. if L > self._max_ext_len:
  386. raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
  387. obj = self._read(L)
  388. elif b == 0xC8: # ext 16
  389. typ = TYPE_EXT
  390. self._reserve(3)
  391. L, n = _unpack_from(">Hb", self._buffer, self._buff_i)
  392. self._buff_i += 3
  393. if L > self._max_ext_len:
  394. raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
  395. obj = self._read(L)
  396. elif b == 0xC9: # ext 32
  397. typ = TYPE_EXT
  398. self._reserve(5)
  399. L, n = _unpack_from(">Ib", self._buffer, self._buff_i)
  400. self._buff_i += 5
  401. if L > self._max_ext_len:
  402. raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
  403. obj = self._read(L)
  404. elif b == 0xCA:
  405. self._reserve(4)
  406. obj = _unpack_from(">f", self._buffer, self._buff_i)[0]
  407. self._buff_i += 4
  408. elif b == 0xCB:
  409. self._reserve(8)
  410. obj = _unpack_from(">d", self._buffer, self._buff_i)[0]
  411. self._buff_i += 8
  412. elif b == 0xCC:
  413. self._reserve(1)
  414. obj = self._buffer[self._buff_i]
  415. self._buff_i += 1
  416. elif b == 0xCD:
  417. self._reserve(2)
  418. obj = _unpack_from(">H", self._buffer, self._buff_i)[0]
  419. self._buff_i += 2
  420. elif b == 0xCE:
  421. self._reserve(4)
  422. obj = _unpack_from(">I", self._buffer, self._buff_i)[0]
  423. self._buff_i += 4
  424. elif b == 0xCF:
  425. self._reserve(8)
  426. obj = _unpack_from(">Q", self._buffer, self._buff_i)[0]
  427. self._buff_i += 8
  428. elif b == 0xD0:
  429. self._reserve(1)
  430. obj = _unpack_from("b", self._buffer, self._buff_i)[0]
  431. self._buff_i += 1
  432. elif b == 0xD1:
  433. self._reserve(2)
  434. obj = _unpack_from(">h", self._buffer, self._buff_i)[0]
  435. self._buff_i += 2
  436. elif b == 0xD2:
  437. self._reserve(4)
  438. obj = _unpack_from(">i", self._buffer, self._buff_i)[0]
  439. self._buff_i += 4
  440. elif b == 0xD3:
  441. self._reserve(8)
  442. obj = _unpack_from(">q", self._buffer, self._buff_i)[0]
  443. self._buff_i += 8
  444. elif b == 0xD4: # fixext 1
  445. typ = TYPE_EXT
  446. if self._max_ext_len < 1:
  447. raise ValueError("%s exceeds max_ext_len(%s)" % (1, self._max_ext_len))
  448. self._reserve(2)
  449. n, obj = _unpack_from("b1s", self._buffer, self._buff_i)
  450. self._buff_i += 2
  451. elif b == 0xD5: # fixext 2
  452. typ = TYPE_EXT
  453. if self._max_ext_len < 2:
  454. raise ValueError("%s exceeds max_ext_len(%s)" % (2, self._max_ext_len))
  455. self._reserve(3)
  456. n, obj = _unpack_from("b2s", self._buffer, self._buff_i)
  457. self._buff_i += 3
  458. elif b == 0xD6: # fixext 4
  459. typ = TYPE_EXT
  460. if self._max_ext_len < 4:
  461. raise ValueError("%s exceeds max_ext_len(%s)" % (4, self._max_ext_len))
  462. self._reserve(5)
  463. n, obj = _unpack_from("b4s", self._buffer, self._buff_i)
  464. self._buff_i += 5
  465. elif b == 0xD7: # fixext 8
  466. typ = TYPE_EXT
  467. if self._max_ext_len < 8:
  468. raise ValueError("%s exceeds max_ext_len(%s)" % (8, self._max_ext_len))
  469. self._reserve(9)
  470. n, obj = _unpack_from("b8s", self._buffer, self._buff_i)
  471. self._buff_i += 9
  472. elif b == 0xD8: # fixext 16
  473. typ = TYPE_EXT
  474. if self._max_ext_len < 16:
  475. raise ValueError("%s exceeds max_ext_len(%s)" % (16, self._max_ext_len))
  476. self._reserve(17)
  477. n, obj = _unpack_from("b16s", self._buffer, self._buff_i)
  478. self._buff_i += 17
  479. elif b == 0xD9:
  480. typ = TYPE_RAW
  481. self._reserve(1)
  482. n = self._buffer[self._buff_i]
  483. self._buff_i += 1
  484. if n > self._max_str_len:
  485. raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
  486. obj = self._read(n)
  487. elif b == 0xDA:
  488. typ = TYPE_RAW
  489. self._reserve(2)
  490. (n,) = _unpack_from(">H", self._buffer, self._buff_i)
  491. self._buff_i += 2
  492. if n > self._max_str_len:
  493. raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
  494. obj = self._read(n)
  495. elif b == 0xDB:
  496. typ = TYPE_RAW
  497. self._reserve(4)
  498. (n,) = _unpack_from(">I", self._buffer, self._buff_i)
  499. self._buff_i += 4
  500. if n > self._max_str_len:
  501. raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
  502. obj = self._read(n)
  503. elif b == 0xDC:
  504. typ = TYPE_ARRAY
  505. self._reserve(2)
  506. (n,) = _unpack_from(">H", self._buffer, self._buff_i)
  507. self._buff_i += 2
  508. if n > self._max_array_len:
  509. raise ValueError("%s exceeds max_array_len(%s)", n, self._max_array_len)
  510. elif b == 0xDD:
  511. typ = TYPE_ARRAY
  512. self._reserve(4)
  513. (n,) = _unpack_from(">I", self._buffer, self._buff_i)
  514. self._buff_i += 4
  515. if n > self._max_array_len:
  516. raise ValueError("%s exceeds max_array_len(%s)", n, self._max_array_len)
  517. elif b == 0xDE:
  518. self._reserve(2)
  519. (n,) = _unpack_from(">H", self._buffer, self._buff_i)
  520. self._buff_i += 2
  521. if n > self._max_map_len:
  522. raise ValueError("%s exceeds max_map_len(%s)", n, self._max_map_len)
  523. typ = TYPE_MAP
  524. elif b == 0xDF:
  525. self._reserve(4)
  526. (n,) = _unpack_from(">I", self._buffer, self._buff_i)
  527. self._buff_i += 4
  528. if n > self._max_map_len:
  529. raise ValueError("%s exceeds max_map_len(%s)", n, self._max_map_len)
  530. typ = TYPE_MAP
  531. else:
  532. raise FormatError("Unknown header: 0x%x" % b)
  533. return typ, n, obj
  534. def _unpack(self, execute=EX_CONSTRUCT):
  535. typ, n, obj = self._read_header(execute)
  536. if execute == EX_READ_ARRAY_HEADER:
  537. if typ != TYPE_ARRAY:
  538. raise ValueError("Expected array")
  539. return n
  540. if execute == EX_READ_MAP_HEADER:
  541. if typ != TYPE_MAP:
  542. raise ValueError("Expected map")
  543. return n
  544. # TODO should we eliminate the recursion?
  545. if typ == TYPE_ARRAY:
  546. if execute == EX_SKIP:
  547. for i in xrange(n):
  548. # TODO check whether we need to call `list_hook`
  549. self._unpack(EX_SKIP)
  550. return
  551. ret = newlist_hint(n)
  552. for i in xrange(n):
  553. ret.append(self._unpack(EX_CONSTRUCT))
  554. if self._list_hook is not None:
  555. ret = self._list_hook(ret)
  556. # TODO is the interaction between `list_hook` and `use_list` ok?
  557. return ret if self._use_list else tuple(ret)
  558. if typ == TYPE_MAP:
  559. if execute == EX_SKIP:
  560. for i in xrange(n):
  561. # TODO check whether we need to call hooks
  562. self._unpack(EX_SKIP)
  563. self._unpack(EX_SKIP)
  564. return
  565. if self._object_pairs_hook is not None:
  566. ret = self._object_pairs_hook(
  567. (self._unpack(EX_CONSTRUCT), self._unpack(EX_CONSTRUCT))
  568. for _ in xrange(n)
  569. )
  570. else:
  571. ret = {}
  572. for _ in xrange(n):
  573. key = self._unpack(EX_CONSTRUCT)
  574. if self._strict_map_key and type(key) not in (unicode, bytes):
  575. raise ValueError(
  576. "%s is not allowed for map key" % str(type(key))
  577. )
  578. if not PY2 and type(key) is str:
  579. key = sys.intern(key)
  580. ret[key] = self._unpack(EX_CONSTRUCT)
  581. if self._object_hook is not None:
  582. ret = self._object_hook(ret)
  583. return ret
  584. if execute == EX_SKIP:
  585. return
  586. if typ == TYPE_RAW:
  587. if self._raw:
  588. obj = bytes(obj)
  589. else:
  590. obj = obj.decode("utf_8", self._unicode_errors)
  591. return obj
  592. if typ == TYPE_BIN:
  593. return bytes(obj)
  594. if typ == TYPE_EXT:
  595. if n == -1: # timestamp
  596. ts = Timestamp.from_bytes(bytes(obj))
  597. if self._timestamp == 1:
  598. return ts.to_unix()
  599. elif self._timestamp == 2:
  600. return ts.to_unix_nano()
  601. elif self._timestamp == 3:
  602. return ts.to_datetime()
  603. else:
  604. return ts
  605. else:
  606. return self._ext_hook(n, bytes(obj))
  607. assert typ == TYPE_IMMEDIATE
  608. return obj
  609. def __iter__(self):
  610. return self
  611. def __next__(self):
  612. try:
  613. ret = self._unpack(EX_CONSTRUCT)
  614. self._consume()
  615. return ret
  616. except OutOfData:
  617. self._consume()
  618. raise StopIteration
  619. except RecursionError:
  620. raise StackError
  621. next = __next__
  622. def skip(self):
  623. self._unpack(EX_SKIP)
  624. self._consume()
  625. def unpack(self):
  626. try:
  627. ret = self._unpack(EX_CONSTRUCT)
  628. except RecursionError:
  629. raise StackError
  630. self._consume()
  631. return ret
  632. def read_array_header(self):
  633. ret = self._unpack(EX_READ_ARRAY_HEADER)
  634. self._consume()
  635. return ret
  636. def read_map_header(self):
  637. ret = self._unpack(EX_READ_MAP_HEADER)
  638. self._consume()
  639. return ret
  640. def tell(self):
  641. return self._stream_offset
  642. class Packer(object):
  643. """
  644. MessagePack Packer
  645. Usage:
  646. packer = Packer()
  647. astream.write(packer.pack(a))
  648. astream.write(packer.pack(b))
  649. Packer's constructor has some keyword arguments:
  650. :param callable default:
  651. Convert user type to builtin type that Packer supports.
  652. See also simplejson's document.
  653. :param bool use_single_float:
  654. Use single precision float type for float. (default: False)
  655. :param bool autoreset:
  656. Reset buffer after each pack and return its content as `bytes`. (default: True).
  657. If set this to false, use `bytes()` to get content and `.reset()` to clear buffer.
  658. :param bool use_bin_type:
  659. Use bin type introduced in msgpack spec 2.0 for bytes.
  660. It also enables str8 type for unicode. (default: True)
  661. :param bool strict_types:
  662. If set to true, types will be checked to be exact. Derived classes
  663. from serializable types will not be serialized and will be
  664. treated as unsupported type and forwarded to default.
  665. Additionally tuples will not be serialized as lists.
  666. This is useful when trying to implement accurate serialization
  667. for python types.
  668. :param bool datetime:
  669. If set to true, datetime with tzinfo is packed into Timestamp type.
  670. Note that the tzinfo is stripped in the timestamp.
  671. You can get UTC datetime with `timestamp=3` option of the Unpacker.
  672. (Python 2 is not supported).
  673. :param str unicode_errors:
  674. The error handler for encoding unicode. (default: 'strict')
  675. DO NOT USE THIS!! This option is kept for very specific usage.
  676. """
  677. def __init__(
  678. self,
  679. default=None,
  680. use_single_float=False,
  681. autoreset=True,
  682. use_bin_type=True,
  683. strict_types=False,
  684. datetime=False,
  685. unicode_errors=None,
  686. ):
  687. self._strict_types = strict_types
  688. self._use_float = use_single_float
  689. self._autoreset = autoreset
  690. self._use_bin_type = use_bin_type
  691. self._buffer = StringIO()
  692. if PY2 and datetime:
  693. raise ValueError("datetime is not supported in Python 2")
  694. self._datetime = bool(datetime)
  695. self._unicode_errors = unicode_errors or "strict"
  696. if default is not None:
  697. if not callable(default):
  698. raise TypeError("default must be callable")
  699. self._default = default
  700. def _pack(
  701. self,
  702. obj,
  703. nest_limit=DEFAULT_RECURSE_LIMIT,
  704. check=isinstance,
  705. check_type_strict=_check_type_strict,
  706. ):
  707. default_used = False
  708. if self._strict_types:
  709. check = check_type_strict
  710. list_types = list
  711. else:
  712. list_types = (list, tuple)
  713. while True:
  714. if nest_limit < 0:
  715. raise ValueError("recursion limit exceeded")
  716. if obj is None:
  717. return self._buffer.write(b"\xc0")
  718. if check(obj, bool):
  719. if obj:
  720. return self._buffer.write(b"\xc3")
  721. return self._buffer.write(b"\xc2")
  722. if check(obj, int_types):
  723. if 0 <= obj < 0x80:
  724. return self._buffer.write(struct.pack("B", obj))
  725. if -0x20 <= obj < 0:
  726. return self._buffer.write(struct.pack("b", obj))
  727. if 0x80 <= obj <= 0xFF:
  728. return self._buffer.write(struct.pack("BB", 0xCC, obj))
  729. if -0x80 <= obj < 0:
  730. return self._buffer.write(struct.pack(">Bb", 0xD0, obj))
  731. if 0xFF < obj <= 0xFFFF:
  732. return self._buffer.write(struct.pack(">BH", 0xCD, obj))
  733. if -0x8000 <= obj < -0x80:
  734. return self._buffer.write(struct.pack(">Bh", 0xD1, obj))
  735. if 0xFFFF < obj <= 0xFFFFFFFF:
  736. return self._buffer.write(struct.pack(">BI", 0xCE, obj))
  737. if -0x80000000 <= obj < -0x8000:
  738. return self._buffer.write(struct.pack(">Bi", 0xD2, obj))
  739. if 0xFFFFFFFF < obj <= 0xFFFFFFFFFFFFFFFF:
  740. return self._buffer.write(struct.pack(">BQ", 0xCF, obj))
  741. if -0x8000000000000000 <= obj < -0x80000000:
  742. return self._buffer.write(struct.pack(">Bq", 0xD3, obj))
  743. if not default_used and self._default is not None:
  744. obj = self._default(obj)
  745. default_used = True
  746. continue
  747. raise OverflowError("Integer value out of range")
  748. if check(obj, (bytes, bytearray)):
  749. n = len(obj)
  750. if n >= 2 ** 32:
  751. raise ValueError("%s is too large" % type(obj).__name__)
  752. self._pack_bin_header(n)
  753. return self._buffer.write(obj)
  754. if check(obj, unicode):
  755. obj = obj.encode("utf-8", self._unicode_errors)
  756. n = len(obj)
  757. if n >= 2 ** 32:
  758. raise ValueError("String is too large")
  759. self._pack_raw_header(n)
  760. return self._buffer.write(obj)
  761. if check(obj, memoryview):
  762. n = len(obj) * obj.itemsize
  763. if n >= 2 ** 32:
  764. raise ValueError("Memoryview is too large")
  765. self._pack_bin_header(n)
  766. return self._buffer.write(obj)
  767. if check(obj, float):
  768. if self._use_float:
  769. return self._buffer.write(struct.pack(">Bf", 0xCA, obj))
  770. return self._buffer.write(struct.pack(">Bd", 0xCB, obj))
  771. if check(obj, (ExtType, Timestamp)):
  772. if check(obj, Timestamp):
  773. code = -1
  774. data = obj.to_bytes()
  775. else:
  776. code = obj.code
  777. data = obj.data
  778. assert isinstance(code, int)
  779. assert isinstance(data, bytes)
  780. L = len(data)
  781. if L == 1:
  782. self._buffer.write(b"\xd4")
  783. elif L == 2:
  784. self._buffer.write(b"\xd5")
  785. elif L == 4:
  786. self._buffer.write(b"\xd6")
  787. elif L == 8:
  788. self._buffer.write(b"\xd7")
  789. elif L == 16:
  790. self._buffer.write(b"\xd8")
  791. elif L <= 0xFF:
  792. self._buffer.write(struct.pack(">BB", 0xC7, L))
  793. elif L <= 0xFFFF:
  794. self._buffer.write(struct.pack(">BH", 0xC8, L))
  795. else:
  796. self._buffer.write(struct.pack(">BI", 0xC9, L))
  797. self._buffer.write(struct.pack("b", code))
  798. self._buffer.write(data)
  799. return
  800. if check(obj, list_types):
  801. n = len(obj)
  802. self._pack_array_header(n)
  803. for i in xrange(n):
  804. self._pack(obj[i], nest_limit - 1)
  805. return
  806. if check(obj, dict):
  807. return self._pack_map_pairs(
  808. len(obj), dict_iteritems(obj), nest_limit - 1
  809. )
  810. if self._datetime and check(obj, _DateTime):
  811. obj = Timestamp.from_datetime(obj)
  812. default_used = 1
  813. continue
  814. if not default_used and self._default is not None:
  815. obj = self._default(obj)
  816. default_used = 1
  817. continue
  818. raise TypeError("Cannot serialize %r" % (obj,))
  819. def pack(self, obj):
  820. try:
  821. self._pack(obj)
  822. except:
  823. self._buffer = StringIO() # force reset
  824. raise
  825. if self._autoreset:
  826. ret = self._buffer.getvalue()
  827. self._buffer = StringIO()
  828. return ret
  829. def pack_map_pairs(self, pairs):
  830. self._pack_map_pairs(len(pairs), pairs)
  831. if self._autoreset:
  832. ret = self._buffer.getvalue()
  833. self._buffer = StringIO()
  834. return ret
  835. def pack_array_header(self, n):
  836. if n >= 2 ** 32:
  837. raise ValueError
  838. self._pack_array_header(n)
  839. if self._autoreset:
  840. ret = self._buffer.getvalue()
  841. self._buffer = StringIO()
  842. return ret
  843. def pack_map_header(self, n):
  844. if n >= 2 ** 32:
  845. raise ValueError
  846. self._pack_map_header(n)
  847. if self._autoreset:
  848. ret = self._buffer.getvalue()
  849. self._buffer = StringIO()
  850. return ret
  851. def pack_ext_type(self, typecode, data):
  852. if not isinstance(typecode, int):
  853. raise TypeError("typecode must have int type.")
  854. if not 0 <= typecode <= 127:
  855. raise ValueError("typecode should be 0-127")
  856. if not isinstance(data, bytes):
  857. raise TypeError("data must have bytes type")
  858. L = len(data)
  859. if L > 0xFFFFFFFF:
  860. raise ValueError("Too large data")
  861. if L == 1:
  862. self._buffer.write(b"\xd4")
  863. elif L == 2:
  864. self._buffer.write(b"\xd5")
  865. elif L == 4:
  866. self._buffer.write(b"\xd6")
  867. elif L == 8:
  868. self._buffer.write(b"\xd7")
  869. elif L == 16:
  870. self._buffer.write(b"\xd8")
  871. elif L <= 0xFF:
  872. self._buffer.write(b"\xc7" + struct.pack("B", L))
  873. elif L <= 0xFFFF:
  874. self._buffer.write(b"\xc8" + struct.pack(">H", L))
  875. else:
  876. self._buffer.write(b"\xc9" + struct.pack(">I", L))
  877. self._buffer.write(struct.pack("B", typecode))
  878. self._buffer.write(data)
  879. def _pack_array_header(self, n):
  880. if n <= 0x0F:
  881. return self._buffer.write(struct.pack("B", 0x90 + n))
  882. if n <= 0xFFFF:
  883. return self._buffer.write(struct.pack(">BH", 0xDC, n))
  884. if n <= 0xFFFFFFFF:
  885. return self._buffer.write(struct.pack(">BI", 0xDD, n))
  886. raise ValueError("Array is too large")
  887. def _pack_map_header(self, n):
  888. if n <= 0x0F:
  889. return self._buffer.write(struct.pack("B", 0x80 + n))
  890. if n <= 0xFFFF:
  891. return self._buffer.write(struct.pack(">BH", 0xDE, n))
  892. if n <= 0xFFFFFFFF:
  893. return self._buffer.write(struct.pack(">BI", 0xDF, n))
  894. raise ValueError("Dict is too large")
  895. def _pack_map_pairs(self, n, pairs, nest_limit=DEFAULT_RECURSE_LIMIT):
  896. self._pack_map_header(n)
  897. for (k, v) in pairs:
  898. self._pack(k, nest_limit - 1)
  899. self._pack(v, nest_limit - 1)
  900. def _pack_raw_header(self, n):
  901. if n <= 0x1F:
  902. self._buffer.write(struct.pack("B", 0xA0 + n))
  903. elif self._use_bin_type and n <= 0xFF:
  904. self._buffer.write(struct.pack(">BB", 0xD9, n))
  905. elif n <= 0xFFFF:
  906. self._buffer.write(struct.pack(">BH", 0xDA, n))
  907. elif n <= 0xFFFFFFFF:
  908. self._buffer.write(struct.pack(">BI", 0xDB, n))
  909. else:
  910. raise ValueError("Raw is too large")
  911. def _pack_bin_header(self, n):
  912. if not self._use_bin_type:
  913. return self._pack_raw_header(n)
  914. elif n <= 0xFF:
  915. return self._buffer.write(struct.pack(">BB", 0xC4, n))
  916. elif n <= 0xFFFF:
  917. return self._buffer.write(struct.pack(">BH", 0xC5, n))
  918. elif n <= 0xFFFFFFFF:
  919. return self._buffer.write(struct.pack(">BI", 0xC6, n))
  920. else:
  921. raise ValueError("Bin is too large")
  922. def bytes(self):
  923. """Return internal buffer contents as bytes object"""
  924. return self._buffer.getvalue()
  925. def reset(self):
  926. """Reset internal buffer.
  927. This method is useful only when autoreset=False.
  928. """
  929. self._buffer = StringIO()
  930. def getbuffer(self):
  931. """Return view of internal buffer."""
  932. if USING_STRINGBUILDER or PY2:
  933. return memoryview(self.bytes())
  934. else:
  935. return self._buffer.getbuffer()