json模块

json模块是实现序列化和反序列化的,主要用户不同程序之间的数据交换,首先来看一下:

dumps()序列化

  1. import json
  2. '''json模块是实现序列化和反序列话功能的'''
  3. users = ["alex","tom","wupeiqi","sb","耿长学"]
  4. mes = json.dumps(users) #实例化,并打印
  5. print(mes)
    运行结果如下:
    ["alex", "tom", "wupeiqi", "sb", "\u803f\u957f\u5b66"]

从上面可以看出,dumps其实是生成一个序列化的实例,这个后面会和dump进行区分,而且汉字非英文转化成的是字节码。

loads()反序列化

  1. import json
  2. '''json模块是实现序列化和反序列话功能的'''
  3. users = ["alex","tom","wupeiqi","sb","耿长学"]
  4. mes = json.dumps(users) #实例化,并打印
  5. print("序列化:",mes)
  6.  
  7. data = json.loads(mes)
  8. print("反序列化:",data)
    运行结果如下:
    序列化: ["alex", "tom", "wupeiqi", "sb", "\u803f\u957f\u5b66"]
    反序列化: ['alex', 'tom', 'wupeiqi', 'sb', '耿长学']

上面dumps()和loads()序列化和反序列化是在程序之间交互,即没有通过文件,只是以json.dumps()序列化一个实例,然后接收之后json.loads()进行反序列化,依次实现程序之间的交互。那么如何在文件中交互呢?

文件中的交互:

  1. import json
  2. '''json模块是实现序列化和反序列话功能的'''
  3. users = ["alex","tom","wupeiqi","sb","耿长学"]
  4. mes = json.dumps(users) #实例化,并打印
  5. '''把mes序列化实例写入文件'''
  6. with open("users",'w+') as fw:
  7. fw.write(mes)

上面代码中,我们把序列化的实例写入到文件中,使用json.dumps()生成实例,fw.write()写入文件,需要通过文件的write()写入。

文件反序列化:

  1. import json
  2. with open('users','r+') as fr:
  3. mess = json.loads(fr.read())
  4. print(mess)
  5. 运行结果如下:
  6. ['alex', 'tom', 'wupeiqi', 'sb', '耿长学']

上面文件反序列化过程中,首先要把文件中的内容读取出来,因为是使用write()写进去的,因此要read()出来,然后再进行反序列化,生成一个实例。

下面我们来看看load()和dump()实现序列化和反序列化:

    dump()序列化

  1. import json
  2. users = {"alex":"sb","wupeiqi":,"耿长学":}
  3. with open("users","w+") as fw:
  4. json.dump(users,fw)

从上面可以看出,json.dump()是直接把实例化的信息序列化到指定文件中,不需要write()来写入,直接可以自己来写入,而dumps()只是序列化成一个实例,还需要自己write()到文件中。

    load()反序列化

  1. import json
  2. with open('users','r+') as fr:
  3. users = json.load(fr)
  4. print(users)
  5.  
  6. 运行结果如下:
  7. {'alex': 'sb', '耿长学': , 'wupeiqi': }

load()反序列化不需要读取之后才反序列化,直接可以从文件反序列化,因为是dump()进去的。

    总结:

从上面dumps()、loads()和dump()、load()序列化和反序列化可以看出,dumps()是序列化生成一个实例,loads()是读取写进去的实例,dumps()和loads()主要用于不同程序或者接口之间的数据交换传输,而dump()和load()更适合于文件级别之间的读取和写入,两者之间还是有侧重点的。

这个不是说两者功能一样,用法不同,其实设计的时候侧重方向就是不一样的。

    json源代码

  1. r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of
  2. JavaScript syntax (ECMA- 3rd edition) used as a lightweight data
  3. interchange format.
  4.  
  5. :mod:`json` exposes an API familiar to users of the standard library
  6. :mod:`marshal` and :mod:`pickle` modules. It is derived from a
  7. version of the externally maintained simplejson library.
  8.  
  9. Encoding basic Python object hierarchies::
  10.  
  11. >>> import json
  12. >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, )}])
  13. '["foo", {"bar": ["baz", null, 1.0, 2]}]'
  14. >>> print(json.dumps("\"foo\bar"))
  15. "\"foo\bar"
  16. >>> print(json.dumps('\u1234'))
  17. "\u1234"
  18. >>> print(json.dumps('\\'))
  19. "\\"
  20. >>> print(json.dumps({"c": , "b": , "a": }, sort_keys=True))
  21. {"a": , "b": , "c": }
  22. >>> from io import StringIO
  23. >>> io = StringIO()
  24. >>> json.dump(['streaming API'], io)
  25. >>> io.getvalue()
  26. '["streaming API"]'
  27.  
  28. Compact encoding::
  29.  
  30. >>> import json
  31. >>> from collections import OrderedDict
  32. >>> mydict = OrderedDict([('', ), ('', )])
  33. >>> json.dumps([,,,mydict], separators=(',', ':'))
  34. '[1,2,3,{"4":5,"6":7}]'
  35.  
  36. Pretty printing::
  37.  
  38. >>> import json
  39. >>> print(json.dumps({'': , '': }, sort_keys=True, indent=))
  40. {
  41. "": ,
  42. "":
  43. }
  44.  
  45. Decoding JSON::
  46.  
  47. >>> import json
  48. >>> obj = ['foo', {'bar': ['baz', None, 1.0, ]}]
  49. >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
  50. True
  51. >>> json.loads('"\\"foo\\bar"') == '"foo\x08ar'
  52. True
  53. >>> from io import StringIO
  54. >>> io = StringIO('["streaming API"]')
  55. >>> json.load(io)[] == 'streaming API'
  56. True
  57.  
  58. Specializing JSON object decoding::
  59.  
  60. >>> import json
  61. >>> def as_complex(dct):
  62. ... if '__complex__' in dct:
  63. ... return complex(dct['real'], dct['imag'])
  64. ... return dct
  65. ...
  66. >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
  67. ... object_hook=as_complex)
  68. (+2j)
  69. >>> from decimal import Decimal
  70. >>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1')
  71. True
  72.  
  73. Specializing JSON object encoding::
  74.  
  75. >>> import json
  76. >>> def encode_complex(obj):
  77. ... if isinstance(obj, complex):
  78. ... return [obj.real, obj.imag]
  79. ... raise TypeError(repr(o) + " is not JSON serializable")
  80. ...
  81. >>> json.dumps( + 1j, default=encode_complex)
  82. '[2.0, 1.0]'
  83. >>> json.JSONEncoder(default=encode_complex).encode( + 1j)
  84. '[2.0, 1.0]'
  85. >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode( + 1j))
  86. '[2.0, 1.0]'
  87.  
  88. Using json.tool from the shell to validate and pretty-print::
  89.  
  90. $ echo '{"json":"obj"}' | python -m json.tool
  91. {
  92. "json": "obj"
  93. }
  94. $ echo '{ 1.2:3.4}' | python -m json.tool
  95. Expecting property name enclosed in double quotes: line column (char )
  96. """
  97. __version__ = '2.0.9'
  98. __all__ = [
  99. 'dump', 'dumps', 'load', 'loads',
  100. 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
  101. ]
  102.  
  103. __author__ = 'Bob Ippolito <bob@redivi.com>'
  104.  
  105. from .decoder import JSONDecoder, JSONDecodeError
  106. from .encoder import JSONEncoder
  107.  
  108. _default_encoder = JSONEncoder(
  109. skipkeys=False,
  110. ensure_ascii=True,
  111. check_circular=True,
  112. allow_nan=True,
  113. indent=None,
  114. separators=None,
  115. default=None,
  116. )
  117.  
  118. def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
  119. allow_nan=True, cls=None, indent=None, separators=None,
  120. default=None, sort_keys=False, **kw):
  121. """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
  122. ``.write()``-supporting file-like object).
  123.  
  124. If ``skipkeys`` is true then ``dict`` keys that are not basic types
  125. (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
  126. instead of raising a ``TypeError``.
  127.  
  128. If ``ensure_ascii`` is false, then the strings written to ``fp`` can
  129. contain non-ASCII characters if they appear in strings contained in
  130. ``obj``. Otherwise, all such characters are escaped in JSON strings.
  131.  
  132. If ``check_circular`` is false, then the circular reference check
  133. for container types will be skipped and a circular reference will
  134. result in an ``OverflowError`` (or worse).
  135.  
  136. If ``allow_nan`` is false, then it will be a ``ValueError`` to
  137. serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
  138. in strict compliance of the JSON specification, instead of using the
  139. JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
  140.  
  141. If ``indent`` is a non-negative integer, then JSON array elements and
  142. object members will be pretty-printed with that indent level. An indent
  143. level of will only insert newlines. ``None`` is the most compact
  144. representation.
  145.  
  146. If specified, ``separators`` should be an ``(item_separator, key_separator)``
  147. tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
  148. ``(',', ': ')`` otherwise. To get the most compact JSON representation,
  149. you should specify ``(',', ':')`` to eliminate whitespace.
  150.  
  151. ``default(obj)`` is a function that should return a serializable version
  152. of obj or raise TypeError. The default simply raises TypeError.
  153.  
  154. If *sort_keys* is ``True`` (default: ``False``), then the output of
  155. dictionaries will be sorted by key.
  156.  
  157. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
  158. ``.default()`` method to serialize additional types), specify it with
  159. the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
  160.  
  161. """
  162. # cached encoder
  163. if (not skipkeys and ensure_ascii and
  164. check_circular and allow_nan and
  165. cls is None and indent is None and separators is None and
  166. default is None and not sort_keys and not kw):
  167. iterable = _default_encoder.iterencode(obj)
  168. else:
  169. if cls is None:
  170. cls = JSONEncoder
  171. iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
  172. check_circular=check_circular, allow_nan=allow_nan, indent=indent,
  173. separators=separators,
  174. default=default, sort_keys=sort_keys, **kw).iterencode(obj)
  175. # could accelerate with writelines in some versions of Python, at
  176. # a debuggability cost
  177. for chunk in iterable:
  178. fp.write(chunk)
  179.  
  180. def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
  181. allow_nan=True, cls=None, indent=None, separators=None,
  182. default=None, sort_keys=False, **kw):
  183. """Serialize ``obj`` to a JSON formatted ``str``.
  184.  
  185. If ``skipkeys`` is true then ``dict`` keys that are not basic types
  186. (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
  187. instead of raising a ``TypeError``.
  188.  
  189. If ``ensure_ascii`` is false, then the return value can contain non-ASCII
  190. characters if they appear in strings contained in ``obj``. Otherwise, all
  191. such characters are escaped in JSON strings.
  192.  
  193. If ``check_circular`` is false, then the circular reference check
  194. for container types will be skipped and a circular reference will
  195. result in an ``OverflowError`` (or worse).
  196.  
  197. If ``allow_nan`` is false, then it will be a ``ValueError`` to
  198. serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
  199. strict compliance of the JSON specification, instead of using the
  200. JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
  201.  
  202. If ``indent`` is a non-negative integer, then JSON array elements and
  203. object members will be pretty-printed with that indent level. An indent
  204. level of will only insert newlines. ``None`` is the most compact
  205. representation.
  206.  
  207. If specified, ``separators`` should be an ``(item_separator, key_separator)``
  208. tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
  209. ``(',', ': ')`` otherwise. To get the most compact JSON representation,
  210. you should specify ``(',', ':')`` to eliminate whitespace.
  211.  
  212. ``default(obj)`` is a function that should return a serializable version
  213. of obj or raise TypeError. The default simply raises TypeError.
  214.  
  215. If *sort_keys* is ``True`` (default: ``False``), then the output of
  216. dictionaries will be sorted by key.
  217.  
  218. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
  219. ``.default()`` method to serialize additional types), specify it with
  220. the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
  221.  
  222. """
  223. # cached encoder
  224. if (not skipkeys and ensure_ascii and
  225. check_circular and allow_nan and
  226. cls is None and indent is None and separators is None and
  227. default is None and not sort_keys and not kw):
  228. return _default_encoder.encode(obj)
  229. if cls is None:
  230. cls = JSONEncoder
  231. return cls(
  232. skipkeys=skipkeys, ensure_ascii=ensure_ascii,
  233. check_circular=check_circular, allow_nan=allow_nan, indent=indent,
  234. separators=separators, default=default, sort_keys=sort_keys,
  235. **kw).encode(obj)
  236.  
  237. _default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None)
  238.  
  239. def load(fp, cls=None, object_hook=None, parse_float=None,
  240. parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
  241. """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
  242. a JSON document) to a Python object.
  243.  
  244. ``object_hook`` is an optional function that will be called with the
  245. result of any object literal decode (a ``dict``). The return value of
  246. ``object_hook`` will be used instead of the ``dict``. This feature
  247. can be used to implement custom decoders (e.g. JSON-RPC class hinting).
  248.  
  249. ``object_pairs_hook`` is an optional function that will be called with the
  250. result of any object literal decoded with an ordered list of pairs. The
  251. return value of ``object_pairs_hook`` will be used instead of the ``dict``.
  252. This feature can be used to implement custom decoders that rely on the
  253. order that the key and value pairs are decoded (for example,
  254. collections.OrderedDict will remember the order of insertion). If
  255. ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
  256.  
  257. To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
  258. kwarg; otherwise ``JSONDecoder`` is used.
  259.  
  260. """
  261. return loads(fp.read(),
  262. cls=cls, object_hook=object_hook,
  263. parse_float=parse_float, parse_int=parse_int,
  264. parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  265.  
  266. def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
  267. parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
  268. """Deserialize ``s`` (a ``str`` instance containing a JSON
  269. document) to a Python object.
  270.  
  271. ``object_hook`` is an optional function that will be called with the
  272. result of any object literal decode (a ``dict``). The return value of
  273. ``object_hook`` will be used instead of the ``dict``. This feature
  274. can be used to implement custom decoders (e.g. JSON-RPC class hinting).
  275.  
  276. ``object_pairs_hook`` is an optional function that will be called with the
  277. result of any object literal decoded with an ordered list of pairs. The
  278. return value of ``object_pairs_hook`` will be used instead of the ``dict``.
  279. This feature can be used to implement custom decoders that rely on the
  280. order that the key and value pairs are decoded (for example,
  281. collections.OrderedDict will remember the order of insertion). If
  282. ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
  283.  
  284. ``parse_float``, if specified, will be called with the string
  285. of every JSON float to be decoded. By default this is equivalent to
  286. float(num_str). This can be used to use another datatype or parser
  287. for JSON floats (e.g. decimal.Decimal).
  288.  
  289. ``parse_int``, if specified, will be called with the string
  290. of every JSON int to be decoded. By default this is equivalent to
  291. int(num_str). This can be used to use another datatype or parser
  292. for JSON integers (e.g. float).
  293.  
  294. ``parse_constant``, if specified, will be called with one of the
  295. following strings: -Infinity, Infinity, NaN, null, true, false.
  296. This can be used to raise an exception if invalid JSON numbers
  297. are encountered.
  298.  
  299. To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
  300. kwarg; otherwise ``JSONDecoder`` is used.
  301.  
  302. The ``encoding`` argument is ignored and deprecated.
  303.  
  304. """
  305. if not isinstance(s, str):
  306. raise TypeError('the JSON object must be str, not {!r}'.format(
  307. s.__class__.__name__))
  308. if s.startswith(u'\ufeff'):
  309. raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
  310. s, )
  311. if (cls is None and object_hook is None and
  312. parse_int is None and parse_float is None and
  313. parse_constant is None and object_pairs_hook is None and not kw):
  314. return _default_decoder.decode(s)
  315. if cls is None:
  316. cls = JSONDecoder
  317. if object_hook is not None:
  318. kw['object_hook'] = object_hook
  319. if object_pairs_hook is not None:
  320. kw['object_pairs_hook'] = object_pairs_hook
  321. if parse_float is not None:
  322. kw['parse_float'] = parse_float
  323. if parse_int is not None:
  324. kw['parse_int'] = parse_int
  325. if parse_constant is not None:
  326. kw['parse_constant'] = parse_constant
  327. return cls(**kw).decode(s)

    pickle源代码

  1. """Create portable serialized representations of Python objects.
  2.  
  3. See module copyreg for a mechanism for registering custom picklers.
  4. See module pickletools source for extensive comments.
  5.  
  6. Classes:
  7.  
  8. Pickler
  9. Unpickler
  10.  
  11. Functions:
  12.  
  13. dump(object, file)
  14. dumps(object) -> string
  15. load(file) -> object
  16. loads(string) -> object
  17.  
  18. Misc variables:
  19.  
  20. __version__
  21. format_version
  22. compatible_formats
  23.  
  24. """
  25.  
  26. from types import FunctionType
  27. from copyreg import dispatch_table
  28. from copyreg import _extension_registry, _inverted_registry, _extension_cache
  29. from itertools import islice
  30. import sys
  31. from sys import maxsize
  32. from struct import pack, unpack
  33. import re
  34. import io
  35. import codecs
  36. import _compat_pickle
  37.  
  38. __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
  39. "Unpickler", "dump", "dumps", "load", "loads"]
  40.  
  41. # Shortcut for use in isinstance testing
  42. bytes_types = (bytes, bytearray)
  43.  
  44. # These are purely informational; no code uses these.
  45. format_version = "4.0" # File format version we write
  46. compatible_formats = ["1.0", # Original protocol
  47. "1.1", # Protocol with INST added
  48. "1.2", # Original protocol
  49. "1.3", # Protocol with BINFLOAT added
  50. "2.0", # Protocol
  51. "3.0", # Protocol
  52. "4.0", # Protocol
  53. ] # Old format versions we can read
  54.  
  55. # This is the highest protocol number we know how to read.
  56. HIGHEST_PROTOCOL =
  57.  
  58. # The protocol we write by default. May be less than HIGHEST_PROTOCOL.
  59. # We intentionally write a protocol that Python .x cannot read;
  60. # there are too many issues with that.
  61. DEFAULT_PROTOCOL =
  62.  
  63. class PickleError(Exception):
  64. """A common base class for the other pickling exceptions."""
  65. pass
  66.  
  67. class PicklingError(PickleError):
  68. """This exception is raised when an unpicklable object is passed to the
  69. dump() method.
  70.  
  71. """
  72. pass
  73.  
  74. class UnpicklingError(PickleError):
  75. """This exception is raised when there is a problem unpickling an object,
  76. such as a security violation.
  77.  
  78. Note that other exceptions may also be raised during unpickling, including
  79. (but not necessarily limited to) AttributeError, EOFError, ImportError,
  80. and IndexError.
  81.  
  82. """
  83. pass
  84.  
  85. # An instance of _Stop is raised by Unpickler.load_stop() in response to
  86. # the STOP opcode, passing the object that is the result of unpickling.
  87. class _Stop(Exception):
  88. def __init__(self, value):
  89. self.value = value
  90.  
  91. # Jython has PyStringMap; it's a dict subclass with string keys
  92. try:
  93. from org.python.core import PyStringMap
  94. except ImportError:
  95. PyStringMap = None
  96.  
  97. # Pickle opcodes. See pickletools.py for extensive docs. The listing
  98. # here is in kind-of alphabetical order of -character pickle code.
  99. # pickletools groups them by purpose.
  100.  
  101. MARK = b'(' # push special markobject on stack
  102. STOP = b'.' # every pickle ends with STOP
  103. POP = b'' # discard topmost stack item
  104. POP_MARK = b'' # discard stack top through topmost markobject
  105. DUP = b'' # duplicate top stack item
  106. FLOAT = b'F' # push float object; decimal string argument
  107. INT = b'I' # push integer or bool; decimal string argument
  108. BININT = b'J' # push four-byte signed int
  109. BININT1 = b'K' # push -byte unsigned int
  110. LONG = b'L' # push long; decimal string argument
  111. BININT2 = b'M' # push -byte unsigned int
  112. NONE = b'N' # push None
  113. PERSID = b'P' # push persistent object; id is taken from string arg
  114. BINPERSID = b'Q' # " " " ; " " " " stack
  115. REDUCE = b'R' # apply callable to argtuple, both on stack
  116. STRING = b'S' # push string; NL-terminated string argument
  117. BINSTRING = b'T' # push string; counted binary string argument
  118. SHORT_BINSTRING= b'U' # " " ; " " " " < bytes
  119. UNICODE = b'V' # push Unicode string; raw-unicode-escaped'd argument
  120. BINUNICODE = b'X' # " " " ; counted UTF-8 string argument
  121. APPEND = b'a' # append stack top to list below it
  122. BUILD = b'b' # call __setstate__ or __dict__.update()
  123. GLOBAL = b'c' # push self.find_class(modname, name); string args
  124. DICT = b'd' # build a dict from stack items
  125. EMPTY_DICT = b'}' # push empty dict
  126. APPENDS = b'e' # extend list on stack by topmost stack slice
  127. GET = b'g' # push item from memo on stack; index is string arg
  128. BINGET = b'h' # " " " " " " ; " " -byte arg
  129. INST = b'i' # build & push class instance
  130. LONG_BINGET = b'j' # push item from memo on stack; index is -byte arg
  131. LIST = b'l' # build list from topmost stack items
  132. EMPTY_LIST = b']' # push empty list
  133. OBJ = b'o' # build & push class instance
  134. PUT = b'p' # store stack top in memo; index is string arg
  135. BINPUT = b'q' # " " " " " ; " " 1-byte arg
  136. LONG_BINPUT = b'r' # " " " " " ; " " 4-byte arg
  137. SETITEM = b's' # add key+value pair to dict
  138. TUPLE = b't' # build tuple from topmost stack items
  139. EMPTY_TUPLE = b')' # push empty tuple
  140. SETITEMS = b'u' # modify dict by adding topmost key+value pairs
  141. BINFLOAT = b'G' # push float; arg is -byte float encoding
  142.  
  143. TRUE = b'I01\n' # not an opcode; see INT docs in pickletools.py
  144. FALSE = b'I00\n' # not an opcode; see INT docs in pickletools.py
  145.  
  146. # Protocol
  147.  
  148. PROTO = b'\x80' # identify pickle protocol
  149. NEWOBJ = b'\x81' # build object by applying cls.__new__ to argtuple
  150. EXT1 = b'\x82' # push object from extension registry; -byte index
  151. EXT2 = b'\x83' # ditto, but -byte index
  152. EXT4 = b'\x84' # ditto, but -byte index
  153. TUPLE1 = b'\x85' # build -tuple from stack top
  154. TUPLE2 = b'\x86' # build -tuple from two topmost stack items
  155. TUPLE3 = b'\x87' # build -tuple from three topmost stack items
  156. NEWTRUE = b'\x88' # push True
  157. NEWFALSE = b'\x89' # push False
  158. LONG1 = b'\x8a' # push long from < bytes
  159. LONG4 = b'\x8b' # push really big long
  160.  
  161. _tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
  162.  
  163. # Protocol (Python .x)
  164.  
  165. BINBYTES = b'B' # push bytes; counted binary string argument
  166. SHORT_BINBYTES = b'C' # " " ; " " " " < bytes
  167.  
  168. # Protocol
  169. SHORT_BINUNICODE = b'\x8c' # push short string; UTF- length < bytes
  170. BINUNICODE8 = b'\x8d' # push very long string
  171. BINBYTES8 = b'\x8e' # push very long bytes string
  172. EMPTY_SET = b'\x8f' # push empty set on the stack
  173. ADDITEMS = b'\x90' # modify set by adding topmost stack items
  174. FROZENSET = b'\x91' # build frozenset from topmost stack items
  175. NEWOBJ_EX = b'\x92' # like NEWOBJ but work with keyword only arguments
  176. STACK_GLOBAL = b'\x93' # same as GLOBAL but using names on the stacks
  177. MEMOIZE = b'\x94' # store top of the stack in memo
  178. FRAME = b'\x95' # indicate the beginning of a new frame
  179.  
  180. __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$", x)])
  181.  
  182. class _Framer:
  183.  
  184. _FRAME_SIZE_TARGET = *
  185.  
  186. def __init__(self, file_write):
  187. self.file_write = file_write
  188. self.current_frame = None
  189.  
  190. def start_framing(self):
  191. self.current_frame = io.BytesIO()
  192.  
  193. def end_framing(self):
  194. if self.current_frame and self.current_frame.tell() > :
  195. self.commit_frame(force=True)
  196. self.current_frame = None
  197.  
  198. def commit_frame(self, force=False):
  199. if self.current_frame:
  200. f = self.current_frame
  201. if f.tell() >= self._FRAME_SIZE_TARGET or force:
  202. with f.getbuffer() as data:
  203. n = len(data)
  204. write = self.file_write
  205. write(FRAME)
  206. write(pack("<Q", n))
  207. write(data)
  208. f.seek()
  209. f.truncate()
  210.  
  211. def write(self, data):
  212. if self.current_frame:
  213. return self.current_frame.write(data)
  214. else:
  215. return self.file_write(data)
  216.  
  217. class _Unframer:
  218.  
  219. def __init__(self, file_read, file_readline, file_tell=None):
  220. self.file_read = file_read
  221. self.file_readline = file_readline
  222. self.current_frame = None
  223.  
  224. def read(self, n):
  225. if self.current_frame:
  226. data = self.current_frame.read(n)
  227. if not data and n != :
  228. self.current_frame = None
  229. return self.file_read(n)
  230. if len(data) < n:
  231. raise UnpicklingError(
  232. "pickle exhausted before end of frame")
  233. return data
  234. else:
  235. return self.file_read(n)
  236.  
  237. def readline(self):
  238. if self.current_frame:
  239. data = self.current_frame.readline()
  240. if not data:
  241. self.current_frame = None
  242. return self.file_readline()
  243. if data[-] != b'\n'[]:
  244. raise UnpicklingError(
  245. "pickle exhausted before end of frame")
  246. return data
  247. else:
  248. return self.file_readline()
  249.  
  250. def load_frame(self, frame_size):
  251. if self.current_frame and self.current_frame.read() != b'':
  252. raise UnpicklingError(
  253. "beginning of a new frame before end of current frame")
  254. self.current_frame = io.BytesIO(self.file_read(frame_size))
  255.  
  256. # Tools used for pickling.
  257.  
  258. def _getattribute(obj, name):
  259. for subpath in name.split('.'):
  260. if subpath == '<locals>':
  261. raise AttributeError("Can't get local attribute {!r} on {!r}"
  262. .format(name, obj))
  263. try:
  264. parent = obj
  265. obj = getattr(obj, subpath)
  266. except AttributeError:
  267. raise AttributeError("Can't get attribute {!r} on {!r}"
  268. .format(name, obj))
  269. return obj, parent
  270.  
  271. def whichmodule(obj, name):
  272. """Find the module an object belong to."""
  273. module_name = getattr(obj, '__module__', None)
  274. if module_name is not None:
  275. return module_name
  276. # Protect the iteration by using a list copy of sys.modules against dynamic
  277. # modules that trigger imports of other modules upon calls to getattr.
  278. for module_name, module in list(sys.modules.items()):
  279. if module_name == '__main__' or module is None:
  280. continue
  281. try:
  282. if _getattribute(module, name)[] is obj:
  283. return module_name
  284. except AttributeError:
  285. pass
  286. return '__main__'
  287.  
  288. def encode_long(x):
  289. r"""Encode a long to a two's complement little-endian binary string.
  290. Note that is a special case, returning an empty string, to save a
  291. byte in the LONG1 pickling context.
  292.  
  293. >>> encode_long()
  294. b''
  295. >>> encode_long()
  296. b'\xff\x00'
  297. >>> encode_long()
  298. b'\xff\x7f'
  299. >>> encode_long(-)
  300. b'\x00\xff'
  301. >>> encode_long(-)
  302. b'\x00\x80'
  303. >>> encode_long(-)
  304. b'\x80'
  305. >>> encode_long()
  306. b'\x7f'
  307. >>>
  308. """
  309. if x == :
  310. return b''
  311. nbytes = (x.bit_length() >> ) +
  312. result = x.to_bytes(nbytes, byteorder='little', signed=True)
  313. if x < and nbytes > :
  314. if result[-] == 0xff and (result[-] & 0x80) != :
  315. result = result[:-]
  316. return result
  317.  
  318. def decode_long(data):
  319. r"""Decode a long from a two's complement little-endian binary string.
  320.  
  321. >>> decode_long(b'')
  322.  
  323. >>> decode_long(b"\xff\x00")
  324.  
  325. >>> decode_long(b"\xff\x7f")
  326.  
  327. >>> decode_long(b"\x00\xff")
  328. -
  329. >>> decode_long(b"\x00\x80")
  330. -
  331. >>> decode_long(b"\x80")
  332. -
  333. >>> decode_long(b"\x7f")
  334.  
  335. """
  336. return int.from_bytes(data, byteorder='little', signed=True)
  337.  
  338. # Pickling machinery
  339.  
  340. class _Pickler:
  341.  
  342. def __init__(self, file, protocol=None, *, fix_imports=True):
  343. """This takes a binary file for writing a pickle data stream.
  344.  
  345. The optional *protocol* argument tells the pickler to use the
  346. given protocol; supported protocols are , , , and . The
  347. default protocol is ; a backward-incompatible protocol designed
  348. for Python .
  349.  
  350. Specifying a negative protocol version selects the highest
  351. protocol version supported. The higher the protocol used, the
  352. more recent the version of Python needed to read the pickle
  353. produced.
  354.  
  355. The *file* argument must have a write() method that accepts a
  356. single bytes argument. It can thus be a file object opened for
  357. binary writing, an io.BytesIO instance, or any other custom
  358. object that meets this interface.
  359.  
  360. If *fix_imports* is True and *protocol* is less than , pickle
  361. will try to map the new Python names to the old module names
  362. used in Python , so that the pickle data stream is readable
  363. with Python .
  364. """
  365. if protocol is None:
  366. protocol = DEFAULT_PROTOCOL
  367. if protocol < :
  368. protocol = HIGHEST_PROTOCOL
  369. elif not <= protocol <= HIGHEST_PROTOCOL:
  370. raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
  371. try:
  372. self._file_write = file.write
  373. except AttributeError:
  374. raise TypeError("file must have a 'write' attribute")
  375. self.framer = _Framer(self._file_write)
  376. self.write = self.framer.write
  377. self.memo = {}
  378. self.proto = int(protocol)
  379. self.bin = protocol >=
  380. self.fast =
  381. self.fix_imports = fix_imports and protocol <
  382.  
  383. def clear_memo(self):
  384. """Clears the pickler's "memo".
  385.  
  386. The memo is the data structure that remembers which objects the
  387. pickler has already seen, so that shared or recursive objects
  388. are pickled by reference and not by value. This method is
  389. useful when re-using picklers.
  390. """
  391. self.memo.clear()
  392.  
  393. def dump(self, obj):
  394. """Write a pickled representation of obj to the open file."""
  395. # Check whether Pickler was initialized correctly. This is
  396. # only needed to mimic the behavior of _pickle.Pickler.dump().
  397. if not hasattr(self, "_file_write"):
  398. raise PicklingError("Pickler.__init__() was not called by "
  399. "%s.__init__()" % (self.__class__.__name__,))
  400. if self.proto >= :
  401. self.write(PROTO + pack("<B", self.proto))
  402. if self.proto >= :
  403. self.framer.start_framing()
  404. self.save(obj)
  405. self.write(STOP)
  406. self.framer.end_framing()
  407.  
  408. def memoize(self, obj):
  409. """Store an object in the memo."""
  410.  
  411. # The Pickler memo is a dictionary mapping object ids to -tuples
  412. # that contain the Unpickler memo key and the object being memoized.
  413. # The memo key is written to the pickle and will become
  414. # the key in the Unpickler's memo. The object is stored in the
  415. # Pickler memo so that transient objects are kept alive during
  416. # pickling.
  417.  
  418. # The use of the Unpickler memo length as the memo key is just a
  419. # convention. The only requirement is that the memo values be unique.
  420. # But there appears no advantage to any other scheme, and this
  421. # scheme allows the Unpickler memo to be implemented as a plain (but
  422. # growable) array, indexed by memo key.
  423. if self.fast:
  424. return
  425. assert id(obj) not in self.memo
  426. idx = len(self.memo)
  427. self.write(self.put(idx))
  428. self.memo[id(obj)] = idx, obj
  429.  
  430. # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
  431. def put(self, idx):
  432. if self.proto >= :
  433. return MEMOIZE
  434. elif self.bin:
  435. if idx < :
  436. return BINPUT + pack("<B", idx)
  437. else:
  438. return LONG_BINPUT + pack("<I", idx)
  439. else:
  440. return PUT + repr(idx).encode("ascii") + b'\n'
  441.  
  442. # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
  443. def get(self, i):
  444. if self.bin:
  445. if i < :
  446. return BINGET + pack("<B", i)
  447. else:
  448. return LONG_BINGET + pack("<I", i)
  449.  
  450. return GET + repr(i).encode("ascii") + b'\n'
  451.  
  452. def save(self, obj, save_persistent_id=True):
  453. self.framer.commit_frame()
  454.  
  455. # Check for persistent id (defined by a subclass)
  456. pid = self.persistent_id(obj)
  457. if pid is not None and save_persistent_id:
  458. self.save_pers(pid)
  459. return
  460.  
  461. # Check the memo
  462. x = self.memo.get(id(obj))
  463. if x is not None:
  464. self.write(self.get(x[]))
  465. return
  466.  
  467. # Check the type dispatch table
  468. t = type(obj)
  469. f = self.dispatch.get(t)
  470. if f is not None:
  471. f(self, obj) # Call unbound method with explicit self
  472. return
  473.  
  474. # Check private dispatch table if any, or else copyreg.dispatch_table
  475. reduce = getattr(self, 'dispatch_table', dispatch_table).get(t)
  476. if reduce is not None:
  477. rv = reduce(obj)
  478. else:
  479. # Check for a class with a custom metaclass; treat as regular class
  480. try:
  481. issc = issubclass(t, type)
  482. except TypeError: # t is not a class (old Boost; see SF #)
  483. issc = False
  484. if issc:
  485. self.save_global(obj)
  486. return
  487.  
  488. # Check for a __reduce_ex__ method, fall back to __reduce__
  489. reduce = getattr(obj, "__reduce_ex__", None)
  490. if reduce is not None:
  491. rv = reduce(self.proto)
  492. else:
  493. reduce = getattr(obj, "__reduce__", None)
  494. if reduce is not None:
  495. rv = reduce()
  496. else:
  497. raise PicklingError("Can't pickle %r object: %r" %
  498. (t.__name__, obj))
  499.  
  500. # Check for string returned by reduce(), meaning "save as global"
  501. if isinstance(rv, str):
  502. self.save_global(obj, rv)
  503. return
  504.  
  505. # Assert that reduce() returned a tuple
  506. if not isinstance(rv, tuple):
  507. raise PicklingError("%s must return string or tuple" % reduce)
  508.  
  509. # Assert that it returned an appropriately sized tuple
  510. l = len(rv)
  511. if not ( <= l <= ):
  512. raise PicklingError("Tuple returned by %s must have "
  513. "two to five elements" % reduce)
  514.  
  515. # Save the reduce() output and finally memoize the object
  516. self.save_reduce(obj=obj, *rv)
  517.  
  518. def persistent_id(self, obj):
  519. # This exists so a subclass can override it
  520. return None
  521.  
  522. def save_pers(self, pid):
  523. # Save a persistent id reference
  524. if self.bin:
  525. self.save(pid, save_persistent_id=False)
  526. self.write(BINPERSID)
  527. else:
  528. self.write(PERSID + str(pid).encode("ascii") + b'\n')
  529.  
  530. def save_reduce(self, func, args, state=None, listitems=None,
  531. dictitems=None, obj=None):
  532. # This API is called by some subclasses
  533.  
  534. if not isinstance(args, tuple):
  535. raise PicklingError("args from save_reduce() must be a tuple")
  536. if not callable(func):
  537. raise PicklingError("func from save_reduce() must be callable")
  538.  
  539. save = self.save
  540. write = self.write
  541.  
  542. func_name = getattr(func, "__name__", "")
  543. if self.proto >= and func_name == "__newobj_ex__":
  544. cls, args, kwargs = args
  545. if not hasattr(cls, "__new__"):
  546. raise PicklingError("args[0] from {} args has no __new__"
  547. .format(func_name))
  548. if obj is not None and cls is not obj.__class__:
  549. raise PicklingError("args[0] from {} args has the wrong class"
  550. .format(func_name))
  551. save(cls)
  552. save(args)
  553. save(kwargs)
  554. write(NEWOBJ_EX)
  555. elif self.proto >= and func_name == "__newobj__":
  556. # A __reduce__ implementation can direct protocol or newer to
  557. # use the more efficient NEWOBJ opcode, while still
  558. # allowing protocol and to work normally. For this to
  559. # work, the function returned by __reduce__ should be
  560. # called __newobj__, and its first argument should be a
  561. # class. The implementation for __newobj__
  562. # should be as follows, although pickle has no way to
  563. # verify this:
  564. #
  565. # def __newobj__(cls, *args):
  566. # return cls.__new__(cls, *args)
  567. #
  568. # Protocols and will pickle a reference to __newobj__,
  569. # while protocol (and above) will pickle a reference to
  570. # cls, the remaining args tuple, and the NEWOBJ code,
  571. # which calls cls.__new__(cls, *args) at unpickling time
  572. # (see load_newobj below). If __reduce__ returns a
  573. # three-tuple, the state from the third tuple item will be
  574. # pickled regardless of the protocol, calling __setstate__
  575. # at unpickling time (see load_build below).
  576. #
  577. # Note that no standard __newobj__ implementation exists;
  578. # you have to provide your own. This is to enforce
  579. # compatibility with Python 2.2 (pickles written using
  580. # protocol or in Python 2.3 should be unpicklable by
  581. # Python 2.2).
  582. cls = args[]
  583. if not hasattr(cls, "__new__"):
  584. raise PicklingError(
  585. "args[0] from __newobj__ args has no __new__")
  586. if obj is not None and cls is not obj.__class__:
  587. raise PicklingError(
  588. "args[0] from __newobj__ args has the wrong class")
  589. args = args[:]
  590. save(cls)
  591. save(args)
  592. write(NEWOBJ)
  593. else:
  594. save(func)
  595. save(args)
  596. write(REDUCE)
  597.  
  598. if obj is not None:
  599. # If the object is already in the memo, this means it is
  600. # recursive. In this case, throw away everything we put on the
  601. # stack, and fetch the object back from the memo.
  602. if id(obj) in self.memo:
  603. write(POP + self.get(self.memo[id(obj)][]))
  604. else:
  605. self.memoize(obj)
  606.  
  607. # More new special cases (that work with older protocols as
  608. # well): when __reduce__ returns a tuple with or items,
  609. # the 4th and 5th item should be iterators that provide list
  610. # items and dict items (as (key, value) tuples), or None.
  611.  
  612. if listitems is not None:
  613. self._batch_appends(listitems)
  614.  
  615. if dictitems is not None:
  616. self._batch_setitems(dictitems)
  617.  
  618. if state is not None:
  619. save(state)
  620. write(BUILD)
  621.  
  622. # Methods below this point are dispatched through the dispatch table
  623.  
  624. dispatch = {}
  625.  
  626. def save_none(self, obj):
  627. self.write(NONE)
  628. dispatch[type(None)] = save_none
  629.  
  630. def save_bool(self, obj):
  631. if self.proto >= :
  632. self.write(NEWTRUE if obj else NEWFALSE)
  633. else:
  634. self.write(TRUE if obj else FALSE)
  635. dispatch[bool] = save_bool
  636.  
  637. def save_long(self, obj):
  638. if self.bin:
  639. # If the int is small enough to fit in a signed -byte 's-comp
  640. # format, we can store it more efficiently than the general
  641. # case.
  642. # First one- and two-byte unsigned ints:
  643. if obj >= :
  644. if obj <= 0xff:
  645. self.write(BININT1 + pack("<B", obj))
  646. return
  647. if obj <= 0xffff:
  648. self.write(BININT2 + pack("<H", obj))
  649. return
  650. # Next check for -byte signed ints:
  651. if -0x80000000 <= obj <= 0x7fffffff:
  652. self.write(BININT + pack("<i", obj))
  653. return
  654. if self.proto >= :
  655. encoded = encode_long(obj)
  656. n = len(encoded)
  657. if n < :
  658. self.write(LONG1 + pack("<B", n) + encoded)
  659. else:
  660. self.write(LONG4 + pack("<i", n) + encoded)
  661. return
  662. self.write(LONG + repr(obj).encode("ascii") + b'L\n')
  663. dispatch[int] = save_long
  664.  
  665. def save_float(self, obj):
  666. if self.bin:
  667. self.write(BINFLOAT + pack('>d', obj))
  668. else:
  669. self.write(FLOAT + repr(obj).encode("ascii") + b'\n')
  670. dispatch[float] = save_float
  671.  
  672. def save_bytes(self, obj):
  673. if self.proto < :
  674. if not obj: # bytes object is empty
  675. self.save_reduce(bytes, (), obj=obj)
  676. else:
  677. self.save_reduce(codecs.encode,
  678. (str(obj, 'latin1'), 'latin1'), obj=obj)
  679. return
  680. n = len(obj)
  681. if n <= 0xff:
  682. self.write(SHORT_BINBYTES + pack("<B", n) + obj)
  683. elif n > 0xffffffff and self.proto >= :
  684. self.write(BINBYTES8 + pack("<Q", n) + obj)
  685. else:
  686. self.write(BINBYTES + pack("<I", n) + obj)
  687. self.memoize(obj)
  688. dispatch[bytes] = save_bytes
  689.  
  690. def save_str(self, obj):
  691. if self.bin:
  692. encoded = obj.encode('utf-8', 'surrogatepass')
  693. n = len(encoded)
  694. if n <= 0xff and self.proto >= :
  695. self.write(SHORT_BINUNICODE + pack("<B", n) + encoded)
  696. elif n > 0xffffffff and self.proto >= :
  697. self.write(BINUNICODE8 + pack("<Q", n) + encoded)
  698. else:
  699. self.write(BINUNICODE + pack("<I", n) + encoded)
  700. else:
  701. obj = obj.replace("\\", "\\u005c")
  702. obj = obj.replace("\n", "\\u000a")
  703. self.write(UNICODE + obj.encode('raw-unicode-escape') +
  704. b'\n')
  705. self.memoize(obj)
  706. dispatch[str] = save_str
  707.  
  708. def save_tuple(self, obj):
  709. if not obj: # tuple is empty
  710. if self.bin:
  711. self.write(EMPTY_TUPLE)
  712. else:
  713. self.write(MARK + TUPLE)
  714. return
  715.  
  716. n = len(obj)
  717. save = self.save
  718. memo = self.memo
  719. if n <= and self.proto >= :
  720. for element in obj:
  721. save(element)
  722. # Subtle. Same as in the big comment below.
  723. if id(obj) in memo:
  724. get = self.get(memo[id(obj)][])
  725. self.write(POP * n + get)
  726. else:
  727. self.write(_tuplesize2code[n])
  728. self.memoize(obj)
  729. return
  730.  
  731. # proto or proto and tuple isn't empty, or proto > 1 and tuple
  732. # has more than elements.
  733. write = self.write
  734. write(MARK)
  735. for element in obj:
  736. save(element)
  737.  
  738. if id(obj) in memo:
  739. # Subtle. d was not in memo when we entered save_tuple(), so
  740. # the process of saving the tuple's elements must have saved
  741. # the tuple itself: the tuple is recursive. The proper action
  742. # now is to throw away everything we put on the stack, and
  743. # simply GET the tuple (it's already constructed). This check
  744. # could have been done in the "for element" loop instead, but
  745. # recursive tuples are a rare thing.
  746. get = self.get(memo[id(obj)][])
  747. if self.bin:
  748. write(POP_MARK + get)
  749. else: # proto -- POP_MARK not available
  750. write(POP * (n+) + get)
  751. return
  752.  
  753. # No recursion.
  754. write(TUPLE)
  755. self.memoize(obj)
  756.  
  757. dispatch[tuple] = save_tuple
  758.  
  759. def save_list(self, obj):
  760. if self.bin:
  761. self.write(EMPTY_LIST)
  762. else: # proto -- can't use EMPTY_LIST
  763. self.write(MARK + LIST)
  764.  
  765. self.memoize(obj)
  766. self._batch_appends(obj)
  767.  
  768. dispatch[list] = save_list
  769.  
  770. _BATCHSIZE =
  771.  
  772. def _batch_appends(self, items):
  773. # Helper to batch up APPENDS sequences
  774. save = self.save
  775. write = self.write
  776.  
  777. if not self.bin:
  778. for x in items:
  779. save(x)
  780. write(APPEND)
  781. return
  782.  
  783. it = iter(items)
  784. while True:
  785. tmp = list(islice(it, self._BATCHSIZE))
  786. n = len(tmp)
  787. if n > :
  788. write(MARK)
  789. for x in tmp:
  790. save(x)
  791. write(APPENDS)
  792. elif n:
  793. save(tmp[])
  794. write(APPEND)
  795. # else tmp is empty, and we're done
  796. if n < self._BATCHSIZE:
  797. return
  798.  
  799. def save_dict(self, obj):
  800. if self.bin:
  801. self.write(EMPTY_DICT)
  802. else: # proto -- can't use EMPTY_DICT
  803. self.write(MARK + DICT)
  804.  
  805. self.memoize(obj)
  806. self._batch_setitems(obj.items())
  807.  
  808. dispatch[dict] = save_dict
  809. if PyStringMap is not None:
  810. dispatch[PyStringMap] = save_dict
  811.  
  812. def _batch_setitems(self, items):
  813. # Helper to batch up SETITEMS sequences; proto >= only
  814. save = self.save
  815. write = self.write
  816.  
  817. if not self.bin:
  818. for k, v in items:
  819. save(k)
  820. save(v)
  821. write(SETITEM)
  822. return
  823.  
  824. it = iter(items)
  825. while True:
  826. tmp = list(islice(it, self._BATCHSIZE))
  827. n = len(tmp)
  828. if n > :
  829. write(MARK)
  830. for k, v in tmp:
  831. save(k)
  832. save(v)
  833. write(SETITEMS)
  834. elif n:
  835. k, v = tmp[]
  836. save(k)
  837. save(v)
  838. write(SETITEM)
  839. # else tmp is empty, and we're done
  840. if n < self._BATCHSIZE:
  841. return
  842.  
  843. def save_set(self, obj):
  844. save = self.save
  845. write = self.write
  846.  
  847. if self.proto < :
  848. self.save_reduce(set, (list(obj),), obj=obj)
  849. return
  850.  
  851. write(EMPTY_SET)
  852. self.memoize(obj)
  853.  
  854. it = iter(obj)
  855. while True:
  856. batch = list(islice(it, self._BATCHSIZE))
  857. n = len(batch)
  858. if n > :
  859. write(MARK)
  860. for item in batch:
  861. save(item)
  862. write(ADDITEMS)
  863. if n < self._BATCHSIZE:
  864. return
  865. dispatch[set] = save_set
  866.  
  867. def save_frozenset(self, obj):
  868. save = self.save
  869. write = self.write
  870.  
  871. if self.proto < :
  872. self.save_reduce(frozenset, (list(obj),), obj=obj)
  873. return
  874.  
  875. write(MARK)
  876. for item in obj:
  877. save(item)
  878.  
  879. if id(obj) in self.memo:
  880. # If the object is already in the memo, this means it is
  881. # recursive. In this case, throw away everything we put on the
  882. # stack, and fetch the object back from the memo.
  883. write(POP_MARK + self.get(self.memo[id(obj)][]))
  884. return
  885.  
  886. write(FROZENSET)
  887. self.memoize(obj)
  888. dispatch[frozenset] = save_frozenset
  889.  
  890. def save_global(self, obj, name=None):
  891. write = self.write
  892. memo = self.memo
  893.  
  894. if name is None:
  895. name = getattr(obj, '__qualname__', None)
  896. if name is None:
  897. name = obj.__name__
  898.  
  899. module_name = whichmodule(obj, name)
  900. try:
  901. __import__(module_name, level=)
  902. module = sys.modules[module_name]
  903. obj2, parent = _getattribute(module, name)
  904. except (ImportError, KeyError, AttributeError):
  905. raise PicklingError(
  906. "Can't pickle %r: it's not found as %s.%s" %
  907. (obj, module_name, name))
  908. else:
  909. if obj2 is not obj:
  910. raise PicklingError(
  911. "Can't pickle %r: it's not the same object as %s.%s" %
  912. (obj, module_name, name))
  913.  
  914. if self.proto >= :
  915. code = _extension_registry.get((module_name, name))
  916. if code:
  917. assert code >
  918. if code <= 0xff:
  919. write(EXT1 + pack("<B", code))
  920. elif code <= 0xffff:
  921. write(EXT2 + pack("<H", code))
  922. else:
  923. write(EXT4 + pack("<i", code))
  924. return
  925. lastname = name.rpartition('.')[]
  926. if parent is module:
  927. name = lastname
  928. # Non-ASCII identifiers are supported only with protocols >= .
  929. if self.proto >= :
  930. self.save(module_name)
  931. self.save(name)
  932. write(STACK_GLOBAL)
  933. elif parent is not module:
  934. self.save_reduce(getattr, (parent, lastname))
  935. elif self.proto >= :
  936. write(GLOBAL + bytes(module_name, "utf-8") + b'\n' +
  937. bytes(name, "utf-8") + b'\n')
  938. else:
  939. if self.fix_imports:
  940. r_name_mapping = _compat_pickle.REVERSE_NAME_MAPPING
  941. r_import_mapping = _compat_pickle.REVERSE_IMPORT_MAPPING
  942. if (module_name, name) in r_name_mapping:
  943. module_name, name = r_name_mapping[(module_name, name)]
  944. elif module_name in r_import_mapping:
  945. module_name = r_import_mapping[module_name]
  946. try:
  947. write(GLOBAL + bytes(module_name, "ascii") + b'\n' +
  948. bytes(name, "ascii") + b'\n')
  949. except UnicodeEncodeError:
  950. raise PicklingError(
  951. "can't pickle global identifier '%s.%s' using "
  952. "pickle protocol %i" % (module, name, self.proto))
  953.  
  954. self.memoize(obj)
  955.  
  956. def save_type(self, obj):
  957. if obj is type(None):
  958. return self.save_reduce(type, (None,), obj=obj)
  959. elif obj is type(NotImplemented):
  960. return self.save_reduce(type, (NotImplemented,), obj=obj)
  961. elif obj is type(...):
  962. return self.save_reduce(type, (...,), obj=obj)
  963. return self.save_global(obj)
  964.  
  965. dispatch[FunctionType] = save_global
  966. dispatch[type] = save_type
  967.  
  968. # Unpickling machinery
  969.  
  970. class _Unpickler:
  971.  
  972. def __init__(self, file, *, fix_imports=True,
  973. encoding="ASCII", errors="strict"):
  974. """This takes a binary file for reading a pickle data stream.
  975.  
  976. The protocol version of the pickle is detected automatically, so
  977. no proto argument is needed.
  978.  
  979. The argument *file* must have two methods, a read() method that
  980. takes an integer argument, and a readline() method that requires
  981. no arguments. Both methods should return bytes. Thus *file*
  982. can be a binary file object opened for reading, an io.BytesIO
  983. object, or any other custom object that meets this interface.
  984.  
  985. The file-like object must have two methods, a read() method
  986. that takes an integer argument, and a readline() method that
  987. requires no arguments. Both methods should return bytes.
  988. Thus file-like object can be a binary file object opened for
  989. reading, a BytesIO object, or any other custom object that
  990. meets this interface.
  991.  
  992. Optional keyword arguments are *fix_imports*, *encoding* and
  993. *errors*, which are used to control compatibility support for
  994. pickle stream generated by Python . If *fix_imports* is True,
  995. pickle will try to map the old Python names to the new names
  996. used in Python . The *encoding* and *errors* tell pickle how
  997. to decode -bit string instances pickled by Python ; these
  998. default to 'ASCII' and 'strict', respectively. *encoding* can be
  999. 'bytes' to read theses -bit string instances as bytes objects.
  1000. """
  1001. self._file_readline = file.readline
  1002. self._file_read = file.read
  1003. self.memo = {}
  1004. self.encoding = encoding
  1005. self.errors = errors
  1006. self.proto =
  1007. self.fix_imports = fix_imports
  1008.  
  1009. def load(self):
  1010. """Read a pickled object representation from the open file.
  1011.  
  1012. Return the reconstituted object hierarchy specified in the file.
  1013. """
  1014. # Check whether Unpickler was initialized correctly. This is
  1015. # only needed to mimic the behavior of _pickle.Unpickler.dump().
  1016. if not hasattr(self, "_file_read"):
  1017. raise UnpicklingError("Unpickler.__init__() was not called by "
  1018. "%s.__init__()" % (self.__class__.__name__,))
  1019. self._unframer = _Unframer(self._file_read, self._file_readline)
  1020. self.read = self._unframer.read
  1021. self.readline = self._unframer.readline
  1022. self.mark = object() # any new unique object
  1023. self.stack = []
  1024. self.append = self.stack.append
  1025. self.proto =
  1026. read = self.read
  1027. dispatch = self.dispatch
  1028. try:
  1029. while True:
  1030. key = read()
  1031. if not key:
  1032. raise EOFError
  1033. assert isinstance(key, bytes_types)
  1034. dispatch[key[]](self)
  1035. except _Stop as stopinst:
  1036. return stopinst.value
  1037.  
  1038. # Return largest index k such that self.stack[k] is self.mark.
  1039. # If the stack doesn't contain a mark, eventually raises IndexError.
  1040. # This could be sped by maintaining another stack, of indices at which
  1041. # the mark appears. For that matter, the latter stack would suffice,
  1042. # and we wouldn't need to push mark objects on self.stack at all.
  1043. # Doing so is probably a good thing, though, since if the pickle is
  1044. # corrupt (or hostile) we may get a clue from finding self.mark embedded
  1045. # in unpickled objects.
  1046. def marker(self):
  1047. stack = self.stack
  1048. mark = self.mark
  1049. k = len(stack)-
  1050. while stack[k] is not mark: k = k-
  1051. return k
  1052.  
  1053. def persistent_load(self, pid):
  1054. raise UnpicklingError("unsupported persistent id encountered")
  1055.  
  1056. dispatch = {}
  1057.  
  1058. def load_proto(self):
  1059. proto = self.read()[]
  1060. if not <= proto <= HIGHEST_PROTOCOL:
  1061. raise ValueError("unsupported pickle protocol: %d" % proto)
  1062. self.proto = proto
  1063. dispatch[PROTO[]] = load_proto
  1064.  
  1065. def load_frame(self):
  1066. frame_size, = unpack('<Q', self.read())
  1067. if frame_size > sys.maxsize:
  1068. raise ValueError("frame size > sys.maxsize: %d" % frame_size)
  1069. self._unframer.load_frame(frame_size)
  1070. dispatch[FRAME[]] = load_frame
  1071.  
  1072. def load_persid(self):
  1073. pid = self.readline()[:-].decode("ascii")
  1074. self.append(self.persistent_load(pid))
  1075. dispatch[PERSID[]] = load_persid
  1076.  
  1077. def load_binpersid(self):
  1078. pid = self.stack.pop()
  1079. self.append(self.persistent_load(pid))
  1080. dispatch[BINPERSID[]] = load_binpersid
  1081.  
  1082. def load_none(self):
  1083. self.append(None)
  1084. dispatch[NONE[]] = load_none
  1085.  
  1086. def load_false(self):
  1087. self.append(False)
  1088. dispatch[NEWFALSE[]] = load_false
  1089.  
  1090. def load_true(self):
  1091. self.append(True)
  1092. dispatch[NEWTRUE[]] = load_true
  1093.  
  1094. def load_int(self):
  1095. data = self.readline()
  1096. if data == FALSE[:]:
  1097. val = False
  1098. elif data == TRUE[:]:
  1099. val = True
  1100. else:
  1101. val = int(data, )
  1102. self.append(val)
  1103. dispatch[INT[]] = load_int
  1104.  
  1105. def load_binint(self):
  1106. self.append(unpack('<i', self.read())[])
  1107. dispatch[BININT[]] = load_binint
  1108.  
  1109. def load_binint1(self):
  1110. self.append(self.read()[])
  1111. dispatch[BININT1[]] = load_binint1
  1112.  
  1113. def load_binint2(self):
  1114. self.append(unpack('<H', self.read())[])
  1115. dispatch[BININT2[]] = load_binint2
  1116.  
  1117. def load_long(self):
  1118. val = self.readline()[:-]
  1119. if val and val[-] == b'L'[]:
  1120. val = val[:-]
  1121. self.append(int(val, ))
  1122. dispatch[LONG[]] = load_long
  1123.  
  1124. def load_long1(self):
  1125. n = self.read()[]
  1126. data = self.read(n)
  1127. self.append(decode_long(data))
  1128. dispatch[LONG1[]] = load_long1
  1129.  
  1130. def load_long4(self):
  1131. n, = unpack('<i', self.read())
  1132. if n < :
  1133. # Corrupt or hostile pickle -- we never write one like this
  1134. raise UnpicklingError("LONG pickle has negative byte count")
  1135. data = self.read(n)
  1136. self.append(decode_long(data))
  1137. dispatch[LONG4[]] = load_long4
  1138.  
  1139. def load_float(self):
  1140. self.append(float(self.readline()[:-]))
  1141. dispatch[FLOAT[]] = load_float
  1142.  
  1143. def load_binfloat(self):
  1144. self.append(unpack('>d', self.read())[])
  1145. dispatch[BINFLOAT[]] = load_binfloat
  1146.  
  1147. def _decode_string(self, value):
  1148. # Used to allow strings from Python to be decoded either as
  1149. # bytes or Unicode strings. This should be used only with the
  1150. # STRING, BINSTRING and SHORT_BINSTRING opcodes.
  1151. if self.encoding == "bytes":
  1152. return value
  1153. else:
  1154. return value.decode(self.encoding, self.errors)
  1155.  
  1156. def load_string(self):
  1157. data = self.readline()[:-]
  1158. # Strip outermost quotes
  1159. if len(data) >= and data[] == data[-] and data[] in b'"\'':
  1160. data = data[:-]
  1161. else:
  1162. raise UnpicklingError("the STRING opcode argument must be quoted")
  1163. self.append(self._decode_string(codecs.escape_decode(data)[]))
  1164. dispatch[STRING[]] = load_string
  1165.  
  1166. def load_binstring(self):
  1167. # Deprecated BINSTRING uses signed -bit length
  1168. len, = unpack('<i', self.read())
  1169. if len < :
  1170. raise UnpicklingError("BINSTRING pickle has negative byte count")
  1171. data = self.read(len)
  1172. self.append(self._decode_string(data))
  1173. dispatch[BINSTRING[]] = load_binstring
  1174.  
  1175. def load_binbytes(self):
  1176. len, = unpack('<I', self.read())
  1177. if len > maxsize:
  1178. raise UnpicklingError("BINBYTES exceeds system's maximum size "
  1179. "of %d bytes" % maxsize)
  1180. self.append(self.read(len))
  1181. dispatch[BINBYTES[]] = load_binbytes
  1182.  
  1183. def load_unicode(self):
  1184. self.append(str(self.readline()[:-], 'raw-unicode-escape'))
  1185. dispatch[UNICODE[]] = load_unicode
  1186.  
  1187. def load_binunicode(self):
  1188. len, = unpack('<I', self.read())
  1189. if len > maxsize:
  1190. raise UnpicklingError("BINUNICODE exceeds system's maximum size "
  1191. "of %d bytes" % maxsize)
  1192. self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
  1193. dispatch[BINUNICODE[]] = load_binunicode
  1194.  
  1195. def load_binunicode8(self):
  1196. len, = unpack('<Q', self.read())
  1197. if len > maxsize:
  1198. raise UnpicklingError("BINUNICODE8 exceeds system's maximum size "
  1199. "of %d bytes" % maxsize)
  1200. self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
  1201. dispatch[BINUNICODE8[]] = load_binunicode8
  1202.  
  1203. def load_binbytes8(self):
  1204. len, = unpack('<Q', self.read())
  1205. if len > maxsize:
  1206. raise UnpicklingError("BINBYTES8 exceeds system's maximum size "
  1207. "of %d bytes" % maxsize)
  1208. self.append(self.read(len))
  1209. dispatch[BINBYTES8[]] = load_binbytes8
  1210.  
  1211. def load_short_binstring(self):
  1212. len = self.read()[]
  1213. data = self.read(len)
  1214. self.append(self._decode_string(data))
  1215. dispatch[SHORT_BINSTRING[]] = load_short_binstring
  1216.  
  1217. def load_short_binbytes(self):
  1218. len = self.read()[]
  1219. self.append(self.read(len))
  1220. dispatch[SHORT_BINBYTES[]] = load_short_binbytes
  1221.  
  1222. def load_short_binunicode(self):
  1223. len = self.read()[]
  1224. self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
  1225. dispatch[SHORT_BINUNICODE[]] = load_short_binunicode
  1226.  
  1227. def load_tuple(self):
  1228. k = self.marker()
  1229. self.stack[k:] = [tuple(self.stack[k+:])]
  1230. dispatch[TUPLE[]] = load_tuple
  1231.  
  1232. def load_empty_tuple(self):
  1233. self.append(())
  1234. dispatch[EMPTY_TUPLE[]] = load_empty_tuple
  1235.  
  1236. def load_tuple1(self):
  1237. self.stack[-] = (self.stack[-],)
  1238. dispatch[TUPLE1[]] = load_tuple1
  1239.  
  1240. def load_tuple2(self):
  1241. self.stack[-:] = [(self.stack[-], self.stack[-])]
  1242. dispatch[TUPLE2[]] = load_tuple2
  1243.  
  1244. def load_tuple3(self):
  1245. self.stack[-:] = [(self.stack[-], self.stack[-], self.stack[-])]
  1246. dispatch[TUPLE3[]] = load_tuple3
  1247.  
  1248. def load_empty_list(self):
  1249. self.append([])
  1250. dispatch[EMPTY_LIST[]] = load_empty_list
  1251.  
  1252. def load_empty_dictionary(self):
  1253. self.append({})
  1254. dispatch[EMPTY_DICT[]] = load_empty_dictionary
  1255.  
  1256. def load_empty_set(self):
  1257. self.append(set())
  1258. dispatch[EMPTY_SET[]] = load_empty_set
  1259.  
  1260. def load_frozenset(self):
  1261. k = self.marker()
  1262. self.stack[k:] = [frozenset(self.stack[k+:])]
  1263. dispatch[FROZENSET[]] = load_frozenset
  1264.  
  1265. def load_list(self):
  1266. k = self.marker()
  1267. self.stack[k:] = [self.stack[k+:]]
  1268. dispatch[LIST[]] = load_list
  1269.  
  1270. def load_dict(self):
  1271. k = self.marker()
  1272. items = self.stack[k+:]
  1273. d = {items[i]: items[i+]
  1274. for i in range(, len(items), )}
  1275. self.stack[k:] = [d]
  1276. dispatch[DICT[]] = load_dict
  1277.  
  1278. # INST and OBJ differ only in how they get a class object. It's not
  1279. # only sensible to do the rest in a common routine, the two routines
  1280. # previously diverged and grew different bugs.
  1281. # klass is the class to instantiate, and k points to the topmost mark
  1282. # object, following which are the arguments for klass.__init__.
  1283. def _instantiate(self, klass, k):
  1284. args = tuple(self.stack[k+:])
  1285. del self.stack[k:]
  1286. if (args or not isinstance(klass, type) or
  1287. hasattr(klass, "__getinitargs__")):
  1288. try:
  1289. value = klass(*args)
  1290. except TypeError as err:
  1291. raise TypeError("in constructor for %s: %s" %
  1292. (klass.__name__, str(err)), sys.exc_info()[])
  1293. else:
  1294. value = klass.__new__(klass)
  1295. self.append(value)
  1296.  
  1297. def load_inst(self):
  1298. module = self.readline()[:-].decode("ascii")
  1299. name = self.readline()[:-].decode("ascii")
  1300. klass = self.find_class(module, name)
  1301. self._instantiate(klass, self.marker())
  1302. dispatch[INST[]] = load_inst
  1303.  
  1304. def load_obj(self):
  1305. # Stack is ... markobject classobject arg1 arg2 ...
  1306. k = self.marker()
  1307. klass = self.stack.pop(k+)
  1308. self._instantiate(klass, k)
  1309. dispatch[OBJ[]] = load_obj
  1310.  
  1311. def load_newobj(self):
  1312. args = self.stack.pop()
  1313. cls = self.stack.pop()
  1314. obj = cls.__new__(cls, *args)
  1315. self.append(obj)
  1316. dispatch[NEWOBJ[]] = load_newobj
  1317.  
  1318. def load_newobj_ex(self):
  1319. kwargs = self.stack.pop()
  1320. args = self.stack.pop()
  1321. cls = self.stack.pop()
  1322. obj = cls.__new__(cls, *args, **kwargs)
  1323. self.append(obj)
  1324. dispatch[NEWOBJ_EX[]] = load_newobj_ex
  1325.  
  1326. def load_global(self):
  1327. module = self.readline()[:-].decode("utf-8")
  1328. name = self.readline()[:-].decode("utf-8")
  1329. klass = self.find_class(module, name)
  1330. self.append(klass)
  1331. dispatch[GLOBAL[]] = load_global
  1332.  
  1333. def load_stack_global(self):
  1334. name = self.stack.pop()
  1335. module = self.stack.pop()
  1336. if type(name) is not str or type(module) is not str:
  1337. raise UnpicklingError("STACK_GLOBAL requires str")
  1338. self.append(self.find_class(module, name))
  1339. dispatch[STACK_GLOBAL[]] = load_stack_global
  1340.  
  1341. def load_ext1(self):
  1342. code = self.read()[]
  1343. self.get_extension(code)
  1344. dispatch[EXT1[]] = load_ext1
  1345.  
  1346. def load_ext2(self):
  1347. code, = unpack('<H', self.read())
  1348. self.get_extension(code)
  1349. dispatch[EXT2[]] = load_ext2
  1350.  
  1351. def load_ext4(self):
  1352. code, = unpack('<i', self.read())
  1353. self.get_extension(code)
  1354. dispatch[EXT4[]] = load_ext4
  1355.  
  1356. def get_extension(self, code):
  1357. nil = []
  1358. obj = _extension_cache.get(code, nil)
  1359. if obj is not nil:
  1360. self.append(obj)
  1361. return
  1362. key = _inverted_registry.get(code)
  1363. if not key:
  1364. if code <= : # note that is forbidden
  1365. # Corrupt or hostile pickle.
  1366. raise UnpicklingError("EXT specifies code <= 0")
  1367. raise ValueError("unregistered extension code %d" % code)
  1368. obj = self.find_class(*key)
  1369. _extension_cache[code] = obj
  1370. self.append(obj)
  1371.  
  1372. def find_class(self, module, name):
  1373. # Subclasses may override this.
  1374. if self.proto < and self.fix_imports:
  1375. if (module, name) in _compat_pickle.NAME_MAPPING:
  1376. module, name = _compat_pickle.NAME_MAPPING[(module, name)]
  1377. elif module in _compat_pickle.IMPORT_MAPPING:
  1378. module = _compat_pickle.IMPORT_MAPPING[module]
  1379. __import__(module, level=)
  1380. if self.proto >= :
  1381. return _getattribute(sys.modules[module], name)[]
  1382. else:
  1383. return getattr(sys.modules[module], name)
  1384.  
  1385. def load_reduce(self):
  1386. stack = self.stack
  1387. args = stack.pop()
  1388. func = stack[-]
  1389. stack[-] = func(*args)
  1390. dispatch[REDUCE[]] = load_reduce
  1391.  
  1392. def load_pop(self):
  1393. del self.stack[-]
  1394. dispatch[POP[]] = load_pop
  1395.  
  1396. def load_pop_mark(self):
  1397. k = self.marker()
  1398. del self.stack[k:]
  1399. dispatch[POP_MARK[]] = load_pop_mark
  1400.  
  1401. def load_dup(self):
  1402. self.append(self.stack[-])
  1403. dispatch[DUP[]] = load_dup
  1404.  
  1405. def load_get(self):
  1406. i = int(self.readline()[:-])
  1407. self.append(self.memo[i])
  1408. dispatch[GET[]] = load_get
  1409.  
  1410. def load_binget(self):
  1411. i = self.read()[]
  1412. self.append(self.memo[i])
  1413. dispatch[BINGET[]] = load_binget
  1414.  
  1415. def load_long_binget(self):
  1416. i, = unpack('<I', self.read())
  1417. self.append(self.memo[i])
  1418. dispatch[LONG_BINGET[]] = load_long_binget
  1419.  
  1420. def load_put(self):
  1421. i = int(self.readline()[:-])
  1422. if i < :
  1423. raise ValueError("negative PUT argument")
  1424. self.memo[i] = self.stack[-]
  1425. dispatch[PUT[]] = load_put
  1426.  
  1427. def load_binput(self):
  1428. i = self.read()[]
  1429. if i < :
  1430. raise ValueError("negative BINPUT argument")
  1431. self.memo[i] = self.stack[-]
  1432. dispatch[BINPUT[]] = load_binput
  1433.  
  1434. def load_long_binput(self):
  1435. i, = unpack('<I', self.read())
  1436. if i > maxsize:
  1437. raise ValueError("negative LONG_BINPUT argument")
  1438. self.memo[i] = self.stack[-]
  1439. dispatch[LONG_BINPUT[]] = load_long_binput
  1440.  
  1441. def load_memoize(self):
  1442. memo = self.memo
  1443. memo[len(memo)] = self.stack[-]
  1444. dispatch[MEMOIZE[]] = load_memoize
  1445.  
  1446. def load_append(self):
  1447. stack = self.stack
  1448. value = stack.pop()
  1449. list = stack[-]
  1450. list.append(value)
  1451. dispatch[APPEND[]] = load_append
  1452.  
  1453. def load_appends(self):
  1454. stack = self.stack
  1455. mark = self.marker()
  1456. list_obj = stack[mark - ]
  1457. items = stack[mark + :]
  1458. if isinstance(list_obj, list):
  1459. list_obj.extend(items)
  1460. else:
  1461. append = list_obj.append
  1462. for item in items:
  1463. append(item)
  1464. del stack[mark:]
  1465. dispatch[APPENDS[]] = load_appends
  1466.  
  1467. def load_setitem(self):
  1468. stack = self.stack
  1469. value = stack.pop()
  1470. key = stack.pop()
  1471. dict = stack[-]
  1472. dict[key] = value
  1473. dispatch[SETITEM[]] = load_setitem
  1474.  
  1475. def load_setitems(self):
  1476. stack = self.stack
  1477. mark = self.marker()
  1478. dict = stack[mark - ]
  1479. for i in range(mark + , len(stack), ):
  1480. dict[stack[i]] = stack[i + ]
  1481.  
  1482. del stack[mark:]
  1483. dispatch[SETITEMS[]] = load_setitems
  1484.  
  1485. def load_additems(self):
  1486. stack = self.stack
  1487. mark = self.marker()
  1488. set_obj = stack[mark - ]
  1489. items = stack[mark + :]
  1490. if isinstance(set_obj, set):
  1491. set_obj.update(items)
  1492. else:
  1493. add = set_obj.add
  1494. for item in items:
  1495. add(item)
  1496. del stack[mark:]
  1497. dispatch[ADDITEMS[]] = load_additems
  1498.  
  1499. def load_build(self):
  1500. stack = self.stack
  1501. state = stack.pop()
  1502. inst = stack[-]
  1503. setstate = getattr(inst, "__setstate__", None)
  1504. if setstate is not None:
  1505. setstate(state)
  1506. return
  1507. slotstate = None
  1508. if isinstance(state, tuple) and len(state) == :
  1509. state, slotstate = state
  1510. if state:
  1511. inst_dict = inst.__dict__
  1512. intern = sys.intern
  1513. for k, v in state.items():
  1514. if type(k) is str:
  1515. inst_dict[intern(k)] = v
  1516. else:
  1517. inst_dict[k] = v
  1518. if slotstate:
  1519. for k, v in slotstate.items():
  1520. setattr(inst, k, v)
  1521. dispatch[BUILD[]] = load_build
  1522.  
  1523. def load_mark(self):
  1524. self.append(self.mark)
  1525. dispatch[MARK[]] = load_mark
  1526.  
  1527. def load_stop(self):
  1528. value = self.stack.pop()
  1529. raise _Stop(value)
  1530. dispatch[STOP[]] = load_stop
  1531.  
  1532. # Shorthands
  1533.  
  1534. def _dump(obj, file, protocol=None, *, fix_imports=True):
  1535. _Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
  1536.  
  1537. def _dumps(obj, protocol=None, *, fix_imports=True):
  1538. f = io.BytesIO()
  1539. _Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
  1540. res = f.getvalue()
  1541. assert isinstance(res, bytes_types)
  1542. return res
  1543.  
  1544. def _load(file, *, fix_imports=True, encoding="ASCII", errors="strict"):
  1545. return _Unpickler(file, fix_imports=fix_imports,
  1546. encoding=encoding, errors=errors).load()
  1547.  
  1548. def _loads(s, *, fix_imports=True, encoding="ASCII", errors="strict"):
  1549. if isinstance(s, str):
  1550. raise TypeError("Can't load pickle from unicode string")
  1551. file = io.BytesIO(s)
  1552. return _Unpickler(file, fix_imports=fix_imports,
  1553. encoding=encoding, errors=errors).load()
  1554.  
  1555. # Use the faster _pickle if possible
  1556. try:
  1557. from _pickle import (
  1558. PickleError,
  1559. PicklingError,
  1560. UnpicklingError,
  1561. Pickler,
  1562. Unpickler,
  1563. dump,
  1564. dumps,
  1565. load,
  1566. loads
  1567. )
  1568. except ImportError:
  1569. Pickler, Unpickler = _Pickler, _Unpickler
  1570. dump, dumps, load, loads = _dump, _dumps, _load, _loads
  1571.  
  1572. # Doctest
  1573. def _test():
  1574. import doctest
  1575. return doctest.testmod()
  1576.  
  1577. if __name__ == "__main__":
  1578. import argparse
  1579. parser = argparse.ArgumentParser(
  1580. description='display contents of the pickle files')
  1581. parser.add_argument(
  1582. 'pickle_file', type=argparse.FileType('br'),
  1583. nargs='*', help='the pickle file')
  1584. parser.add_argument(
  1585. '-t', '--test', action='store_true',
  1586. help='run self-test suite')
  1587. parser.add_argument(
  1588. '-v', action='store_true',
  1589. help='run verbosely; only affects self-test run')
  1590. args = parser.parse_args()
  1591. if args.test:
  1592. _test()
  1593. else:
  1594. if not args.pickle_file:
  1595. parser.print_help()
  1596. else:
  1597. import pprint
  1598. for f in args.pickle_file:
  1599. obj = load(f)
  1600. pprint.pprint(obj)

pickle和json模块的更多相关文章

  1. Python开发之序列化与反序列化:pickle、json模块使用详解

    1 引言 在日常开发中,所有的对象都是存储在内存当中,尤其是像python这样的坚持一切接对象的高级程序设计语言,一旦关机,在写在内存中的数据都将不复存在.另一方面,存储在内存够中的对象由于编程语言. ...

  2. Python序列化-pickle和json模块

    Python的“file-like object“就是一种鸭子类型.对真正的文件对象,它有一个read()方法,返回其内容.但是,许多对象,只要有read()方法,都被视为“file-like obj ...

  3. pytho中pickle、json模块

    pickle & json 模块 json,用于字符串 和 python数据类型间进行转换 pickle,用于python特有的类型 和 python的数据类型间进行转换 json模块提供了四 ...

  4. Python中的序列化以及pickle和json模块介绍

    Python中的序列化指的是在程序运行期间,变量都是在内存中保存着的,如果我们想保留一些运行中的变量值,就可以使用序列化操作把变量内容从内存保存到磁盘中,在Python中这个操作叫pickling,等 ...

  5. 序列化模块之 pickle 和 json

    用于序列化的两个模块: json,用于字符串 和 python数据类型间进行转换 pickle,用于python特有的类型 和 python的数据类型间进行转换 Json模块提供了四个功能:dumps ...

  6. 各类模块的粗略总结(time,re,os,sys,序列化,pickle,shelve.#!json )

    ***collections 扩展数据类型*** ***re 正则相关操作 正则 匹配字符串*** ***time 时间相关 三种格式:时间戳,格式化时间(字符串),时间元组(结构化时间).***`` ...

  7. json模块和pickle模块的用法

    在python中,可以使用pickle和json两个模块对数据进行序列化操作 其中: json可以用于字符串或者字典等与python数据类型之间的序列化与反序列化操作 pickle可以用于python ...

  8. 【python标准库模块四】Json模块和Pickle模块学习

    Json模块 原来有个eval函数能能够从字符串中提取出对应的数据类型,比如"{"name":"zhangsan"}",可以提取出一个字典. ...

  9. Python第十四天 序列化 pickle模块 cPickle模块 JSON模块 API的两种格式

    Python第十四天 序列化  pickle模块  cPickle模块  JSON模块  API的两种格式 目录 Pycharm使用技巧(转载) Python第一天  安装  shell  文件 Py ...

随机推荐

  1. Web前端总结(小伙伴的)

    以下总结是我工作室的小伙伴的心得,可以参考一下 html+css知识点总结 HTMl+CSS知识点收集 1.letter-spacing和word-spacing的区别 letter-spacing: ...

  2. [leetcode-572-Subtree of Another Tree]

    Given two non-empty binary trees s and t, check whether tree t hasexactly the same structure and nod ...

  3. 【Android Developers Training】 88. 使用备份API

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  4. GCD 信号量 dispatch_semaphore_t

    1.GCD知识讲解 1)dispatch_semaphore_create(long value) //创建一个信号量,总量为value,value不能小于0 2)dispatch_semaphore ...

  5. 基于angular4.0分页组件

    分页组件一般只某个页面的一小部分,所以我们它是子组件 当然如果你承认这话的,可以往下看,因为我把他当作子组建来写了 分页组件的模版 import { Component } from '@angula ...

  6. 这个类复制文本文件FileCopy

    package JBJADV003; import java.io.File;import java.io.BufferedReader;import java.io.BufferedWriter;i ...

  7. 浅析CQRS的应用部署

    CQRS,中文翻译命令和查询职责分离,它是一种架构,不仅可以从数据库层面实现读写分离,在代码层面上也是推荐读写分离的.在接口上可以更为简单 命令端定义 ICommandResult Execute(I ...

  8. 详解Android Activity---Activity的生命周期

    转载注明来自:  http://www.cnblogs.com/wujiancheng/ 一.正常情况下Activity的生命周期:  Activity的生命周期大概可以归为三部分 整个的生命周期:o ...

  9. 阅读MDN文档之布局(四)

    Introducing positioning Static positioning Relative positioning Introducing top, bottom, left and ri ...

  10. 事务之使用JDBC进行事务的操作2

    本篇将讲诉如何使用JDBC进行数据库有关事务的操作.在上一篇博客中已经介绍了事务的概念,和在MySQL命令行窗口进行开启事务,提交事务以及回滚事务的操作. 似乎事务和批处理都可以一次同时执行多条SQL ...