serpent.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. """
  2. ast.literal_eval() compatible object tree serialization.
  3. Serpent serializes an object tree into bytes (utf-8 encoded string) that can
  4. be decoded and then passed as-is to ast.literal_eval() to rebuild it as the
  5. original object tree. As such it is safe to send serpent data to other
  6. machines over the network for instance (because only 'safe' literals are
  7. encoded).
  8. Compatible with Python 2.7+ (including 3.x), IronPython 2.7+, Jython 2.7+.
  9. Serpent handles several special Python types to make life easier:
  10. - str --> promoted to unicode (see below why this is)
  11. - bytes, bytearrays, memoryview, buffer --> string, base-64
  12. (you'll have to manually un-base64 them though)
  13. - uuid.UUID, datetime.{datetime, date, time, timespan} --> appropriate string/number
  14. - decimal.Decimal --> string (to not lose precision)
  15. - array.array typecode 'c'/'u' --> string/unicode
  16. - array.array other typecode --> list
  17. - Exception --> dict with some fields of the exception (message, args)
  18. - all other types --> dict with __getstate__ or vars() of the object
  19. Notes:
  20. All str will be promoted to unicode. This is done because it is the
  21. default anyway for Python 3.x, and it solves the problem of the str/unicode
  22. difference between different Python versions. Also it means the serialized
  23. output doesn't have those problematic 'u' prefixes on strings.
  24. The serializer is not thread-safe. Make sure you're not making changes
  25. to the object tree that is being serialized, and don't use the same
  26. serializer in different threads.
  27. Because the serialized format is just valid Python source code, it can
  28. contain comments.
  29. Set literals are not supported on python <3.2 (ast.literal_eval
  30. limitation). If you need Python < 3.2 compatibility, you'll have to use
  31. set_literals=False when serializing. Since version 1.6 serpent chooses
  32. this wisely for you by default, but you can still override it if needed.
  33. Floats +inf and -inf are handled via a trick, Float 'nan' cannot be handled
  34. and is represented by the special value: {'__class__':'float','value':'nan'}
  35. We chose not to encode it as just the string 'NaN' because that could cause
  36. memory issues when used in multiplications.
  37. Copyright by Irmen de Jong (irmen@razorvine.net)
  38. Software license: "MIT software license". See http://opensource.org/licenses/MIT
  39. """
  40. from __future__ import print_function, division
  41. import __future__
  42. import ast
  43. import base64
  44. import sys
  45. import types
  46. import os
  47. import gc
  48. import collections
  49. import decimal
  50. import datetime
  51. import uuid
  52. import array
  53. import math
  54. __version__ = "1.15"
  55. __all__ = ["dump", "dumps", "load", "loads", "register_class", "unregister_class", "tobytes"]
  56. can_use_set_literals = sys.version_info >= (3, 2) # check if we can use set literals
  57. def dumps(obj, indent=False, set_literals=can_use_set_literals, module_in_classname=False):
  58. """Serialize object tree to bytes"""
  59. return Serializer(indent, set_literals, module_in_classname).serialize(obj)
  60. def dump(obj, file, indent=False, set_literals=can_use_set_literals, module_in_classname=False):
  61. """Serialize object tree to a file"""
  62. file.write(dumps(obj, indent=indent, set_literals=set_literals, module_in_classname=module_in_classname))
  63. def loads(serialized_bytes):
  64. """Deserialize bytes back to object tree. Uses ast.literal_eval (safe)."""
  65. serialized = serialized_bytes.decode("utf-8")
  66. if sys.version_info < (3, 0) and sys.platform != "cli":
  67. if os.name == "java":
  68. # Because of a bug in Jython we have to manually convert all Str nodes to unicode. See http://bugs.jython.org/issue2008
  69. serialized = ast.parse(serialized, "<serpent>", mode="eval")
  70. for node in ast.walk(serialized):
  71. if isinstance(node, ast.Str) and type(node.s) is str:
  72. node.s = node.s.decode("utf-8")
  73. else:
  74. # python 2.x: parse with unicode_literals (promotes all strings to unicode)
  75. serialized = compile(serialized, "<serpent>", mode="eval", flags=ast.PyCF_ONLY_AST | __future__.unicode_literals.compiler_flag)
  76. try:
  77. if os.name != "java" and sys.platform != "cli":
  78. gc.disable()
  79. return ast.literal_eval(serialized)
  80. finally:
  81. gc.enable()
  82. def load(file):
  83. """Deserialize bytes from a file back to object tree. Uses ast.literal_eval (safe)."""
  84. data = file.read()
  85. return loads(data)
  86. def _ser_OrderedDict(obj, serializer, outputstream, indentlevel):
  87. obj = {
  88. "__class__": "collections.OrderedDict" if serializer.module_in_classname else "OrderedDict",
  89. "items": list(obj.items())
  90. }
  91. serializer._serialize(obj, outputstream, indentlevel)
  92. def _ser_DictView(obj, serializer, outputstream, indentlevel):
  93. serializer.ser_builtins_list(obj, outputstream, indentlevel)
  94. _special_classes_registry = {
  95. collections.KeysView: _ser_DictView,
  96. collections.ValuesView: _ser_DictView,
  97. collections.ItemsView: _ser_DictView
  98. }
  99. if sys.version_info >= (2, 7):
  100. _special_classes_registry[collections.OrderedDict] = _ser_OrderedDict
  101. def unregister_class(clazz):
  102. """Unregister the specialcase serializer for the given class."""
  103. if clazz in _special_classes_registry:
  104. del _special_classes_registry[clazz]
  105. def register_class(clazz, serializer):
  106. """
  107. Register a special serializer function for objects of the given class.
  108. The function will be called with (object, serpent_serializer, outputstream, indentlevel) arguments.
  109. The function must write the serialized data to outputstream. It doesn't return a value.
  110. """
  111. _special_classes_registry[clazz] = serializer
  112. class BytesWrapper(object):
  113. """
  114. Wrapper for bytes, bytearray etc. to make them appear as base-64 encoded data.
  115. You can use the tobytes utility function to decode this back into the actual bytes (or do it manually)
  116. """
  117. def __init__(self, data):
  118. self.data = data
  119. def __getstate__(self):
  120. if sys.platform == "cli":
  121. b64 = base64.b64encode(str(self.data)) # weird IronPython bug?
  122. elif (os.name == "java" or sys.version_info < (2, 7)) and type(self.data) is bytearray:
  123. b64 = base64.b64encode(bytes(self.data)) # Jython bug http://bugs.jython.org/issue2011
  124. else:
  125. b64 = base64.b64encode(self.data)
  126. return {
  127. "data": b64 if type(b64) is str else b64.decode("ascii"),
  128. "encoding": "base64"
  129. }
  130. @staticmethod
  131. def from_bytes(data):
  132. return BytesWrapper(data)
  133. @staticmethod
  134. def from_bytearray(data):
  135. return BytesWrapper(data)
  136. @staticmethod
  137. def from_memoryview(data):
  138. return BytesWrapper(data.tobytes())
  139. @staticmethod
  140. def from_buffer(data):
  141. return BytesWrapper(data)
  142. if sys.version_info < (3, 0):
  143. _repr = repr # python <3.0 won't need explicit encoding to utf-8, so we optimize this
  144. else:
  145. def _repr(obj):
  146. return repr(obj).encode("utf-8")
  147. _repr_types = set([
  148. str,
  149. int,
  150. bool,
  151. type(None)
  152. ])
  153. _translate_types = {
  154. bytes: BytesWrapper.from_bytes,
  155. bytearray: BytesWrapper.from_bytearray,
  156. collections.deque: list,
  157. }
  158. if sys.version_info >= (3, 0):
  159. _translate_types.update({
  160. collections.UserDict: dict,
  161. collections.UserList: list,
  162. collections.UserString: str
  163. })
  164. _bytes_types = [bytes, bytearray, memoryview]
  165. # do some dynamic changes to the types configuration if needed
  166. if bytes is str:
  167. del _translate_types[bytes]
  168. if hasattr(types, "BufferType"):
  169. _translate_types[types.BufferType] = BytesWrapper.from_buffer
  170. _bytes_types.append(buffer)
  171. try:
  172. _translate_types[memoryview] = BytesWrapper.from_memoryview
  173. except NameError:
  174. pass
  175. if sys.platform == "cli":
  176. _repr_types.remove(str) # IronPython needs special str treatment, otherwise it treats unicode wrong
  177. _bytes_types = tuple(_bytes_types)
  178. def tobytes(obj):
  179. """
  180. Utility function to convert obj back to actual bytes if it is a serpent-encoded bytes dictionary
  181. (a dict with base-64 encoded 'data' in it and 'encoding'='base64').
  182. If obj is already bytes or a byte-like type, return obj unmodified.
  183. Will raise TypeError if obj is none of the above.
  184. """
  185. if isinstance(obj, _bytes_types):
  186. return obj
  187. if isinstance(obj, dict) and "data" in obj and obj.get("encoding") == "base64":
  188. return base64.b64decode(obj["data"])
  189. raise TypeError("argument is neither bytes nor serpent base64 encoded bytes dict")
  190. class Serializer(object):
  191. """
  192. Serialize an object tree to a byte stream.
  193. It is not thread-safe: make sure you're not making changes to the
  194. object tree that is being serialized, and don't use the same serializer
  195. across different threads.
  196. """
  197. dispatch = {}
  198. def __init__(self, indent=False, set_literals=can_use_set_literals, module_in_classname=False):
  199. """
  200. Initialize the serializer.
  201. indent=indent the output over multiple lines (default=false)
  202. setLiterals=use set-literals or not (set to False if you need compatibility with Python < 3.2). Serpent chooses a sensible default for you.
  203. module_in_classname = include module prefix for class names or only use the class name itself
  204. """
  205. self.indent = indent
  206. self.set_literals = set_literals
  207. self.module_in_classname = module_in_classname
  208. self.serialized_obj_ids = set()
  209. self.special_classes_registry_copy = None
  210. def serialize(self, obj):
  211. """Serialize the object tree to bytes."""
  212. self.special_classes_registry_copy = _special_classes_registry.copy() # make it thread safe
  213. header = "# serpent utf-8 "
  214. if self.set_literals:
  215. header += "python3.2\n" # set-literals require python 3.2+ to deserialize (ast.literal_eval limitation)
  216. else:
  217. header += "python2.6\n" # don't change this even though we don't support 2.6 any longer, otherwise we can't read older serpent strings
  218. out = [header.encode("utf-8")]
  219. try:
  220. if os.name != "java" and sys.platform != "cli":
  221. gc.disable()
  222. self.serialized_obj_ids = set()
  223. self._serialize(obj, out, 0)
  224. finally:
  225. gc.enable()
  226. self.special_classes_registry_copy = None
  227. del self.serialized_obj_ids
  228. if sys.platform == "cli":
  229. return "".join(out)
  230. return b"".join(out)
  231. _shortcut_dispatch_types = frozenset([float, complex, tuple, list, dict, set, frozenset])
  232. def _serialize(self, obj, out, level):
  233. t = type(obj)
  234. if t in _translate_types:
  235. obj = _translate_types[t](obj)
  236. t = type(obj)
  237. if t in _repr_types:
  238. out.append(_repr(obj)) # just a simple repr() is enough for these objects
  239. return
  240. if t in self._shortcut_dispatch_types:
  241. # we shortcut these builtins directly to the dispatch function to avoid type lookup overhead below
  242. return self.dispatch[t](self, obj, out, level)
  243. # check special registered types:
  244. special_classes = self.special_classes_registry_copy
  245. for clazz in special_classes:
  246. if isinstance(obj, clazz):
  247. special_classes[clazz](obj, self, out, level)
  248. return
  249. # serialize dispatch
  250. try:
  251. func = self.dispatch[t]
  252. except KeyError:
  253. # walk the MRO until we find a base class we recognise
  254. for type_ in t.__mro__:
  255. if type_ in self.dispatch:
  256. func = self.dispatch[type_]
  257. break
  258. else:
  259. # fall back to the default class serializer
  260. func = Serializer.ser_default_class
  261. func(self, obj, out, level)
  262. def ser_builtins_str(self, str_obj, out, level):
  263. # special case str, for IronPython where str==unicode and repr() yields undesired result
  264. self.ser_builtins_unicode(str_obj, out, level)
  265. dispatch[str] = ser_builtins_str
  266. def ser_builtins_float(self, float_obj, out, level):
  267. if math.isnan(float_obj):
  268. # there's no literal expression for a float NaN...
  269. out.append(b"{'__class__':'float','value':'nan'}")
  270. elif math.isinf(float_obj):
  271. # output a literal expression that overflows the float and results in +/-INF
  272. if float_obj > 0:
  273. out.append(b"1e30000")
  274. else:
  275. out.append(b"-1e30000")
  276. else:
  277. out.append(str(float_obj).encode("ascii"))
  278. dispatch[float] = ser_builtins_float
  279. def ser_builtins_complex(self, complex_obj, out, level):
  280. out.append(b"(")
  281. self.ser_builtins_float(complex_obj.real, out, level)
  282. if complex_obj.imag >= 0:
  283. out.append(b"+")
  284. self.ser_builtins_float(complex_obj.imag, out, level)
  285. out.append(b"j)")
  286. dispatch[complex] = ser_builtins_complex
  287. if sys.version_info < (3, 0):
  288. def ser_builtins_unicode(self, unicode_obj, out, level):
  289. # this method is used for python 2.x unicode (python 3.x doesn't use this)
  290. z = unicode_obj.encode("utf-8")
  291. # double-escape existing backslashes:
  292. z = z.replace("\\", "\\\\")
  293. # backslash-escape control characters:
  294. z = z.replace("\a", "\\a")
  295. z = z.replace("\b", "\\b")
  296. z = z.replace("\f", "\\f")
  297. z = z.replace("\n", "\\n")
  298. z = z.replace("\r", "\\r")
  299. z = z.replace("\t", "\\t")
  300. z = z.replace("\v", "\\v")
  301. if "'" not in z:
  302. z = "'" + z + "'"
  303. elif '"' not in z:
  304. z = '"' + z + '"'
  305. else:
  306. z = z.replace("'", "\\'")
  307. z = "'" + z + "'"
  308. out.append(z)
  309. dispatch[unicode] = ser_builtins_unicode
  310. if sys.version_info < (3, 0):
  311. def ser_builtins_long(self, long_obj, out, level):
  312. # used with python 2.x
  313. out.append(str(long_obj))
  314. dispatch[long] = ser_builtins_long
  315. def ser_builtins_tuple(self, tuple_obj, out, level):
  316. append = out.append
  317. serialize = self._serialize
  318. if self.indent and tuple_obj:
  319. indent_chars = b" " * level
  320. indent_chars_inside = indent_chars + b" "
  321. append(b"(\n")
  322. for elt in tuple_obj:
  323. append(indent_chars_inside)
  324. serialize(elt, out, level + 1)
  325. append(b",\n")
  326. out[-1] = out[-1].rstrip() # remove the last \n
  327. if len(tuple_obj) > 1:
  328. del out[-1] # undo the last ,
  329. append(b"\n" + indent_chars + b")")
  330. else:
  331. append(b"(")
  332. for elt in tuple_obj:
  333. serialize(elt, out, level + 1)
  334. append(b",")
  335. if len(tuple_obj) > 1:
  336. del out[-1] # undo the last ,
  337. append(b")")
  338. dispatch[tuple] = ser_builtins_tuple
  339. def ser_builtins_list(self, list_obj, out, level):
  340. if id(list_obj) in self.serialized_obj_ids:
  341. raise ValueError("Circular reference detected (list)")
  342. self.serialized_obj_ids.add(id(list_obj))
  343. append = out.append
  344. serialize = self._serialize
  345. if self.indent and list_obj:
  346. indent_chars = b" " * level
  347. indent_chars_inside = indent_chars + b" "
  348. append(b"[\n")
  349. for elt in list_obj:
  350. append(indent_chars_inside)
  351. serialize(elt, out, level + 1)
  352. append(b",\n")
  353. del out[-1] # remove the last ,\n
  354. append(b"\n" + indent_chars + b"]")
  355. else:
  356. append(b"[")
  357. for elt in list_obj:
  358. serialize(elt, out, level + 1)
  359. append(b",")
  360. if list_obj:
  361. del out[-1] # remove the last ,
  362. append(b"]")
  363. self.serialized_obj_ids.discard(id(list_obj))
  364. dispatch[list] = ser_builtins_list
  365. def ser_builtins_dict(self, dict_obj, out, level):
  366. if id(dict_obj) in self.serialized_obj_ids:
  367. raise ValueError("Circular reference detected (dict)")
  368. self.serialized_obj_ids.add(id(dict_obj))
  369. append = out.append
  370. serialize = self._serialize
  371. if self.indent and dict_obj:
  372. indent_chars = b" " * level
  373. indent_chars_inside = indent_chars + b" "
  374. append(b"{\n")
  375. dict_items = dict_obj.items()
  376. try:
  377. sorted_items = sorted(dict_items)
  378. except TypeError: # can occur when elements can't be ordered (Python 3.x)
  379. sorted_items = dict_items
  380. for key, value in sorted_items:
  381. append(indent_chars_inside)
  382. serialize(key, out, level + 1)
  383. append(b": ")
  384. serialize(value, out, level + 1)
  385. append(b",\n")
  386. del out[-1] # remove last ,\n
  387. append(b"\n" + indent_chars + b"}")
  388. else:
  389. append(b"{")
  390. for key, value in dict_obj.items():
  391. serialize(key, out, level + 1)
  392. append(b":")
  393. serialize(value, out, level + 1)
  394. append(b",")
  395. if dict_obj:
  396. del out[-1] # remove the last ,
  397. append(b"}")
  398. self.serialized_obj_ids.discard(id(dict_obj))
  399. dispatch[dict] = ser_builtins_dict
  400. def ser_builtins_set(self, set_obj, out, level):
  401. if not self.set_literals:
  402. if self.indent:
  403. set_obj = sorted(set_obj)
  404. self._serialize(tuple(set_obj), out, level) # use a tuple instead of a set literal
  405. return
  406. append = out.append
  407. serialize = self._serialize
  408. if self.indent and set_obj:
  409. indent_chars = b" " * level
  410. indent_chars_inside = indent_chars + b" "
  411. append(b"{\n")
  412. try:
  413. sorted_elts = sorted(set_obj)
  414. except TypeError: # can occur when elements can't be ordered (Python 3.x)
  415. sorted_elts = set_obj
  416. for elt in sorted_elts:
  417. append(indent_chars_inside)
  418. serialize(elt, out, level + 1)
  419. append(b",\n")
  420. del out[-1] # remove the last ,\n
  421. append(b"\n" + indent_chars + b"}")
  422. elif set_obj:
  423. append(b"{")
  424. for elt in set_obj:
  425. serialize(elt, out, level + 1)
  426. append(b",")
  427. del out[-1] # remove the last ,
  428. append(b"}")
  429. else:
  430. # empty set literal doesn't exist unfortunately, replace with empty tuple
  431. self.ser_builtins_tuple((), out, level)
  432. dispatch[set] = ser_builtins_set
  433. def ser_builtins_frozenset(self, set_obj, out, level):
  434. self.ser_builtins_set(set_obj, out, level)
  435. dispatch[frozenset] = ser_builtins_set
  436. def ser_decimal_Decimal(self, decimal_obj, out, level):
  437. # decimal is serialized as a string to avoid losing precision
  438. self._serialize(str(decimal_obj), out, level)
  439. dispatch[decimal.Decimal] = ser_decimal_Decimal
  440. def ser_datetime_datetime(self, datetime_obj, out, level):
  441. self._serialize(datetime_obj.isoformat(), out, level)
  442. dispatch[datetime.datetime] = ser_datetime_datetime
  443. def ser_datetime_date(self, date_obj, out, level):
  444. self._serialize(date_obj.isoformat(), out, level)
  445. dispatch[datetime.date] = ser_datetime_date
  446. if os.name == "java" or sys.version_info < (2, 7): # jython bug http://bugs.jython.org/issue2010
  447. def ser_datetime_timedelta(self, timedelta_obj, out, level):
  448. secs = ((timedelta_obj.days * 86400 + timedelta_obj.seconds) * 10 ** 6 + timedelta_obj.microseconds) / 10 ** 6
  449. self._serialize(secs, out, level)
  450. else:
  451. def ser_datetime_timedelta(self, timedelta_obj, out, level):
  452. secs = timedelta_obj.total_seconds()
  453. self._serialize(secs, out, level)
  454. dispatch[datetime.timedelta] = ser_datetime_timedelta
  455. def ser_datetime_time(self, time_obj, out, level):
  456. self._serialize(str(time_obj), out, level)
  457. dispatch[datetime.time] = ser_datetime_time
  458. def ser_uuid_UUID(self, uuid_obj, out, level):
  459. self._serialize(str(uuid_obj), out, level)
  460. dispatch[uuid.UUID] = ser_uuid_UUID
  461. def ser_exception_class(self, exc_obj, out, level):
  462. value = {
  463. "__class__": self.get_class_name(exc_obj),
  464. "__exception__": True,
  465. "args": exc_obj.args,
  466. "attributes": vars(exc_obj) # add any custom attributes
  467. }
  468. self._serialize(value, out, level)
  469. dispatch[BaseException] = ser_exception_class
  470. def ser_array_array(self, array_obj, out, level):
  471. if array_obj.typecode == 'c':
  472. self._serialize(array_obj.tostring(), out, level)
  473. elif array_obj.typecode == 'u':
  474. self._serialize(array_obj.tounicode(), out, level)
  475. else:
  476. self._serialize(array_obj.tolist(), out, level)
  477. dispatch[array.array] = ser_array_array
  478. def ser_default_class(self, obj, out, level):
  479. if id(obj) in self.serialized_obj_ids:
  480. raise ValueError("Circular reference detected (class)")
  481. self.serialized_obj_ids.add(id(obj))
  482. try:
  483. try:
  484. value = obj.__getstate__()
  485. if value is None and isinstance(obj, tuple):
  486. # collections.namedtuple specialcase (if it is not handled by the tuple serializer)
  487. value = {
  488. "__class__": self.get_class_name(obj),
  489. "items": list(obj._asdict().items())
  490. }
  491. if isinstance(value, dict):
  492. self.ser_builtins_dict(value, out, level)
  493. return
  494. except AttributeError:
  495. try:
  496. value = dict(vars(obj)) # make sure we can serialize anything that resembles a dict
  497. value["__class__"] = self.get_class_name(obj)
  498. except TypeError:
  499. if hasattr(obj, "__slots__"):
  500. # use the __slots__ instead of the vars dict
  501. value = {}
  502. for slot in obj.__slots__:
  503. value[slot] = getattr(obj, slot)
  504. value["__class__"] = self.get_class_name(obj)
  505. else:
  506. raise TypeError("don't know how to serialize class " + str(obj.__class__) + ". Give it vars() or an appropriate __getstate__")
  507. self._serialize(value, out, level)
  508. finally:
  509. self.serialized_obj_ids.discard(id(obj))
  510. def get_class_name(self, obj):
  511. if self.module_in_classname:
  512. return "%s.%s" % (obj.__class__.__module__, obj.__class__.__name__)
  513. else:
  514. return obj.__class__.__name__