PageRenderTime 77ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Lib/test/pickletester.py

http://github.com/IronLanguages/main
Python | 1930 lines | 1843 code | 48 blank | 39 comment | 31 complexity | f9998a4c5af2ab8abdc90314cdac79bd MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. import pickle
  4. import cPickle
  5. import StringIO
  6. import cStringIO
  7. import pickletools
  8. import copy_reg
  9. import sys
  10. from test import test_support as support
  11. from test.test_support import TestFailed, verbose, have_unicode, TESTFN
  12. try:
  13. from test.test_support import _2G, _1M, precisionbigmemtest
  14. except ImportError:
  15. # this import might fail when run on older Python versions by test_xpickle
  16. _2G = _1M = 0
  17. def precisionbigmemtest(*args, **kwargs):
  18. return lambda self: None
  19. # Tests that try a number of pickle protocols should have a
  20. # for proto in protocols:
  21. # kind of outer loop.
  22. assert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2
  23. protocols = range(pickle.HIGHEST_PROTOCOL + 1)
  24. # Copy of test.test_support.run_with_locale. This is needed to support Python
  25. # 2.4, which didn't include it. This is all to support test_xpickle, which
  26. # bounces pickled objects through older Python versions to test backwards
  27. # compatibility.
  28. def run_with_locale(catstr, *locales):
  29. def decorator(func):
  30. def inner(*args, **kwds):
  31. try:
  32. import locale
  33. category = getattr(locale, catstr)
  34. orig_locale = locale.setlocale(category)
  35. except AttributeError:
  36. # if the test author gives us an invalid category string
  37. raise
  38. except:
  39. # cannot retrieve original locale, so do nothing
  40. locale = orig_locale = None
  41. else:
  42. for loc in locales:
  43. try:
  44. locale.setlocale(category, loc)
  45. break
  46. except:
  47. pass
  48. # now run the function, resetting the locale on exceptions
  49. try:
  50. return func(*args, **kwds)
  51. finally:
  52. if locale and orig_locale:
  53. locale.setlocale(category, orig_locale)
  54. inner.func_name = func.func_name
  55. inner.__doc__ = func.__doc__
  56. return inner
  57. return decorator
  58. def no_tracing(func):
  59. """Decorator to temporarily turn off tracing for the duration of a test."""
  60. if not hasattr(sys, 'gettrace'):
  61. return func
  62. else:
  63. def wrapper(*args, **kwargs):
  64. original_trace = sys.gettrace()
  65. try:
  66. sys.settrace(None)
  67. return func(*args, **kwargs)
  68. finally:
  69. sys.settrace(original_trace)
  70. wrapper.__name__ = func.__name__
  71. return wrapper
  72. # Return True if opcode code appears in the pickle, else False.
  73. def opcode_in_pickle(code, pickle):
  74. for op, dummy, dummy in pickletools.genops(pickle):
  75. if op.code == code:
  76. return True
  77. return False
  78. # Return the number of times opcode code appears in pickle.
  79. def count_opcode(code, pickle):
  80. n = 0
  81. for op, dummy, dummy in pickletools.genops(pickle):
  82. if op.code == code:
  83. n += 1
  84. return n
  85. class UnseekableIO(StringIO.StringIO):
  86. def peek(self, *args):
  87. raise NotImplementedError
  88. def seek(self, *args):
  89. raise NotImplementedError
  90. def tell(self):
  91. raise NotImplementedError
  92. # We can't very well test the extension registry without putting known stuff
  93. # in it, but we have to be careful to restore its original state. Code
  94. # should do this:
  95. #
  96. # e = ExtensionSaver(extension_code)
  97. # try:
  98. # fiddle w/ the extension registry's stuff for extension_code
  99. # finally:
  100. # e.restore()
  101. class ExtensionSaver:
  102. # Remember current registration for code (if any), and remove it (if
  103. # there is one).
  104. def __init__(self, code):
  105. self.code = code
  106. if code in copy_reg._inverted_registry:
  107. self.pair = copy_reg._inverted_registry[code]
  108. copy_reg.remove_extension(self.pair[0], self.pair[1], code)
  109. else:
  110. self.pair = None
  111. # Restore previous registration for code.
  112. def restore(self):
  113. code = self.code
  114. curpair = copy_reg._inverted_registry.get(code)
  115. if curpair is not None:
  116. copy_reg.remove_extension(curpair[0], curpair[1], code)
  117. pair = self.pair
  118. if pair is not None:
  119. copy_reg.add_extension(pair[0], pair[1], code)
  120. class C:
  121. def __cmp__(self, other):
  122. return cmp(self.__dict__, other.__dict__)
  123. class D(C):
  124. def __init__(self, arg):
  125. pass
  126. class E(C):
  127. def __getinitargs__(self):
  128. return ()
  129. class H(object):
  130. pass
  131. # Hashable mutable key
  132. class K(object):
  133. def __init__(self, value):
  134. self.value = value
  135. def __reduce__(self):
  136. # Shouldn't support the recursion itself
  137. return K, (self.value,)
  138. import __main__
  139. __main__.C = C
  140. C.__module__ = "__main__"
  141. __main__.D = D
  142. D.__module__ = "__main__"
  143. __main__.E = E
  144. E.__module__ = "__main__"
  145. __main__.H = H
  146. H.__module__ = "__main__"
  147. __main__.K = K
  148. K.__module__ = "__main__"
  149. class myint(int):
  150. def __init__(self, x):
  151. self.str = str(x)
  152. class initarg(C):
  153. def __init__(self, a, b):
  154. self.a = a
  155. self.b = b
  156. def __getinitargs__(self):
  157. return self.a, self.b
  158. class metaclass(type):
  159. pass
  160. class use_metaclass(object):
  161. __metaclass__ = metaclass
  162. class pickling_metaclass(type):
  163. def __eq__(self, other):
  164. return (type(self) == type(other) and
  165. self.reduce_args == other.reduce_args)
  166. def __reduce__(self):
  167. return (create_dynamic_class, self.reduce_args)
  168. __hash__ = None
  169. def create_dynamic_class(name, bases):
  170. result = pickling_metaclass(name, bases, dict())
  171. result.reduce_args = (name, bases)
  172. return result
  173. # DATA0 .. DATA2 are the pickles we expect under the various protocols, for
  174. # the object returned by create_data().
  175. # break into multiple strings to avoid confusing font-lock-mode
  176. DATA0 = """(lp1
  177. I0
  178. aL1L
  179. aF2
  180. ac__builtin__
  181. complex
  182. p2
  183. """ + \
  184. """(F3
  185. F0
  186. tRp3
  187. aI1
  188. aI-1
  189. aI255
  190. aI-255
  191. aI-256
  192. aI65535
  193. aI-65535
  194. aI-65536
  195. aI2147483647
  196. aI-2147483647
  197. aI-2147483648
  198. a""" + \
  199. """(S'abc'
  200. p4
  201. g4
  202. """ + \
  203. """(i__main__
  204. C
  205. p5
  206. """ + \
  207. """(dp6
  208. S'foo'
  209. p7
  210. I1
  211. sS'bar'
  212. p8
  213. I2
  214. sbg5
  215. tp9
  216. ag9
  217. aI5
  218. a.
  219. """
  220. # Disassembly of DATA0.
  221. DATA0_DIS = """\
  222. 0: ( MARK
  223. 1: l LIST (MARK at 0)
  224. 2: p PUT 1
  225. 5: I INT 0
  226. 8: a APPEND
  227. 9: L LONG 1L
  228. 13: a APPEND
  229. 14: F FLOAT 2.0
  230. 17: a APPEND
  231. 18: c GLOBAL '__builtin__ complex'
  232. 39: p PUT 2
  233. 42: ( MARK
  234. 43: F FLOAT 3.0
  235. 46: F FLOAT 0.0
  236. 49: t TUPLE (MARK at 42)
  237. 50: R REDUCE
  238. 51: p PUT 3
  239. 54: a APPEND
  240. 55: I INT 1
  241. 58: a APPEND
  242. 59: I INT -1
  243. 63: a APPEND
  244. 64: I INT 255
  245. 69: a APPEND
  246. 70: I INT -255
  247. 76: a APPEND
  248. 77: I INT -256
  249. 83: a APPEND
  250. 84: I INT 65535
  251. 91: a APPEND
  252. 92: I INT -65535
  253. 100: a APPEND
  254. 101: I INT -65536
  255. 109: a APPEND
  256. 110: I INT 2147483647
  257. 122: a APPEND
  258. 123: I INT -2147483647
  259. 136: a APPEND
  260. 137: I INT -2147483648
  261. 150: a APPEND
  262. 151: ( MARK
  263. 152: S STRING 'abc'
  264. 159: p PUT 4
  265. 162: g GET 4
  266. 165: ( MARK
  267. 166: i INST '__main__ C' (MARK at 165)
  268. 178: p PUT 5
  269. 181: ( MARK
  270. 182: d DICT (MARK at 181)
  271. 183: p PUT 6
  272. 186: S STRING 'foo'
  273. 193: p PUT 7
  274. 196: I INT 1
  275. 199: s SETITEM
  276. 200: S STRING 'bar'
  277. 207: p PUT 8
  278. 210: I INT 2
  279. 213: s SETITEM
  280. 214: b BUILD
  281. 215: g GET 5
  282. 218: t TUPLE (MARK at 151)
  283. 219: p PUT 9
  284. 222: a APPEND
  285. 223: g GET 9
  286. 226: a APPEND
  287. 227: I INT 5
  288. 230: a APPEND
  289. 231: . STOP
  290. highest protocol among opcodes = 0
  291. """
  292. DATA1 = (']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00'
  293. 'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00'
  294. '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff'
  295. '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff'
  296. 'J\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00'
  297. '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n'
  298. 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh'
  299. '\x06tq\nh\nK\x05e.'
  300. )
  301. # Disassembly of DATA1.
  302. DATA1_DIS = """\
  303. 0: ] EMPTY_LIST
  304. 1: q BINPUT 1
  305. 3: ( MARK
  306. 4: K BININT1 0
  307. 6: L LONG 1L
  308. 10: G BINFLOAT 2.0
  309. 19: c GLOBAL '__builtin__ complex'
  310. 40: q BINPUT 2
  311. 42: ( MARK
  312. 43: G BINFLOAT 3.0
  313. 52: G BINFLOAT 0.0
  314. 61: t TUPLE (MARK at 42)
  315. 62: R REDUCE
  316. 63: q BINPUT 3
  317. 65: K BININT1 1
  318. 67: J BININT -1
  319. 72: K BININT1 255
  320. 74: J BININT -255
  321. 79: J BININT -256
  322. 84: M BININT2 65535
  323. 87: J BININT -65535
  324. 92: J BININT -65536
  325. 97: J BININT 2147483647
  326. 102: J BININT -2147483647
  327. 107: J BININT -2147483648
  328. 112: ( MARK
  329. 113: U SHORT_BINSTRING 'abc'
  330. 118: q BINPUT 4
  331. 120: h BINGET 4
  332. 122: ( MARK
  333. 123: c GLOBAL '__main__ C'
  334. 135: q BINPUT 5
  335. 137: o OBJ (MARK at 122)
  336. 138: q BINPUT 6
  337. 140: } EMPTY_DICT
  338. 141: q BINPUT 7
  339. 143: ( MARK
  340. 144: U SHORT_BINSTRING 'foo'
  341. 149: q BINPUT 8
  342. 151: K BININT1 1
  343. 153: U SHORT_BINSTRING 'bar'
  344. 158: q BINPUT 9
  345. 160: K BININT1 2
  346. 162: u SETITEMS (MARK at 143)
  347. 163: b BUILD
  348. 164: h BINGET 6
  349. 166: t TUPLE (MARK at 112)
  350. 167: q BINPUT 10
  351. 169: h BINGET 10
  352. 171: K BININT1 5
  353. 173: e APPENDS (MARK at 3)
  354. 174: . STOP
  355. highest protocol among opcodes = 1
  356. """
  357. DATA2 = ('\x80\x02]q\x01(K\x00\x8a\x01\x01G@\x00\x00\x00\x00\x00\x00\x00'
  358. 'c__builtin__\ncomplex\nq\x02G@\x08\x00\x00\x00\x00\x00\x00G\x00'
  359. '\x00\x00\x00\x00\x00\x00\x00\x86Rq\x03K\x01J\xff\xff\xff\xffK'
  360. '\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xff'
  361. 'J\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00'
  362. '\x80(U\x03abcq\x04h\x04(c__main__\nC\nq\x05oq\x06}q\x07(U\x03foo'
  363. 'q\x08K\x01U\x03barq\tK\x02ubh\x06tq\nh\nK\x05e.')
  364. # Disassembly of DATA2.
  365. DATA2_DIS = """\
  366. 0: \x80 PROTO 2
  367. 2: ] EMPTY_LIST
  368. 3: q BINPUT 1
  369. 5: ( MARK
  370. 6: K BININT1 0
  371. 8: \x8a LONG1 1L
  372. 11: G BINFLOAT 2.0
  373. 20: c GLOBAL '__builtin__ complex'
  374. 41: q BINPUT 2
  375. 43: G BINFLOAT 3.0
  376. 52: G BINFLOAT 0.0
  377. 61: \x86 TUPLE2
  378. 62: R REDUCE
  379. 63: q BINPUT 3
  380. 65: K BININT1 1
  381. 67: J BININT -1
  382. 72: K BININT1 255
  383. 74: J BININT -255
  384. 79: J BININT -256
  385. 84: M BININT2 65535
  386. 87: J BININT -65535
  387. 92: J BININT -65536
  388. 97: J BININT 2147483647
  389. 102: J BININT -2147483647
  390. 107: J BININT -2147483648
  391. 112: ( MARK
  392. 113: U SHORT_BINSTRING 'abc'
  393. 118: q BINPUT 4
  394. 120: h BINGET 4
  395. 122: ( MARK
  396. 123: c GLOBAL '__main__ C'
  397. 135: q BINPUT 5
  398. 137: o OBJ (MARK at 122)
  399. 138: q BINPUT 6
  400. 140: } EMPTY_DICT
  401. 141: q BINPUT 7
  402. 143: ( MARK
  403. 144: U SHORT_BINSTRING 'foo'
  404. 149: q BINPUT 8
  405. 151: K BININT1 1
  406. 153: U SHORT_BINSTRING 'bar'
  407. 158: q BINPUT 9
  408. 160: K BININT1 2
  409. 162: u SETITEMS (MARK at 143)
  410. 163: b BUILD
  411. 164: h BINGET 6
  412. 166: t TUPLE (MARK at 112)
  413. 167: q BINPUT 10
  414. 169: h BINGET 10
  415. 171: K BININT1 5
  416. 173: e APPENDS (MARK at 5)
  417. 174: . STOP
  418. highest protocol among opcodes = 2
  419. """
  420. def create_data():
  421. c = C()
  422. c.foo = 1
  423. c.bar = 2
  424. x = [0, 1L, 2.0, 3.0+0j]
  425. # Append some integer test cases at cPickle.c's internal size
  426. # cutoffs.
  427. uint1max = 0xff
  428. uint2max = 0xffff
  429. int4max = 0x7fffffff
  430. x.extend([1, -1,
  431. uint1max, -uint1max, -uint1max-1,
  432. uint2max, -uint2max, -uint2max-1,
  433. int4max, -int4max, -int4max-1])
  434. y = ('abc', 'abc', c, c)
  435. x.append(y)
  436. x.append(y)
  437. x.append(5)
  438. return x
  439. class AbstractUnpickleTests(unittest.TestCase):
  440. # Subclass must define self.loads, self.error.
  441. _testdata = create_data()
  442. def assert_is_copy(self, obj, objcopy, msg=None):
  443. """Utility method to verify if two objects are copies of each others.
  444. """
  445. if msg is None:
  446. msg = "{!r} is not a copy of {!r}".format(obj, objcopy)
  447. self.assertEqual(obj, objcopy, msg=msg)
  448. self.assertIs(type(obj), type(objcopy), msg=msg)
  449. if hasattr(obj, '__dict__'):
  450. self.assertDictEqual(obj.__dict__, objcopy.__dict__, msg=msg)
  451. self.assertIsNot(obj.__dict__, objcopy.__dict__, msg=msg)
  452. if hasattr(obj, '__slots__'):
  453. self.assertListEqual(obj.__slots__, objcopy.__slots__, msg=msg)
  454. for slot in obj.__slots__:
  455. self.assertEqual(
  456. hasattr(obj, slot), hasattr(objcopy, slot), msg=msg)
  457. self.assertEqual(getattr(obj, slot, None),
  458. getattr(objcopy, slot, None), msg=msg)
  459. def check_unpickling_error(self, errors, data):
  460. try:
  461. try:
  462. self.loads(data)
  463. except:
  464. if support.verbose > 1:
  465. exc_type, exc, tb = sys.exc_info()
  466. print '%-32r - %s: %s' % (data, exc_type.__name__, exc)
  467. raise
  468. except errors:
  469. pass
  470. else:
  471. try:
  472. exc_name = errors.__name__
  473. except AttributeError:
  474. exc_name = str(errors)
  475. raise self.failureException('%s not raised' % exc_name)
  476. def test_load_from_canned_string(self):
  477. expected = self._testdata
  478. for canned in DATA0, DATA1, DATA2:
  479. got = self.loads(canned)
  480. self.assert_is_copy(expected, got)
  481. def test_garyp(self):
  482. self.check_unpickling_error(self.error, 'garyp')
  483. def test_maxint64(self):
  484. maxint64 = (1L << 63) - 1
  485. data = 'I' + str(maxint64) + '\n.'
  486. got = self.loads(data)
  487. self.assertEqual(got, maxint64)
  488. # Try too with a bogus literal.
  489. data = 'I' + str(maxint64) + 'JUNK\n.'
  490. self.check_unpickling_error(ValueError, data)
  491. def test_insecure_strings(self):
  492. insecure = ["abc", "2 + 2", # not quoted
  493. #"'abc' + 'def'", # not a single quoted string
  494. "'abc", # quote is not closed
  495. "'abc\"", # open quote and close quote don't match
  496. "'abc' ?", # junk after close quote
  497. "'\\'", # trailing backslash
  498. # issue #17710
  499. "'", '"',
  500. "' ", '" ',
  501. '\'"', '"\'',
  502. " ''", ' ""',
  503. ' ',
  504. # some tests of the quoting rules
  505. #"'abc\"\''",
  506. #"'\\\\a\'\'\'\\\'\\\\\''",
  507. ]
  508. for s in insecure:
  509. buf = "S" + s + "\n."
  510. self.check_unpickling_error(ValueError, buf)
  511. def test_correctly_quoted_string(self):
  512. goodpickles = [("S''\n.", ''),
  513. ('S""\n.', ''),
  514. ('S"\\n"\n.', '\n'),
  515. ("S'\\n'\n.", '\n')]
  516. for p, expected in goodpickles:
  517. self.assertEqual(self.loads(p), expected)
  518. def test_load_classic_instance(self):
  519. # See issue5180. Test loading 2.x pickles that
  520. # contain an instance of old style class.
  521. for X, args in [(C, ()), (D, ('x',)), (E, ())]:
  522. xname = X.__name__.encode('ascii')
  523. # Protocol 0 (text mode pickle):
  524. """
  525. 0: ( MARK
  526. 1: i INST '__main__ X' (MARK at 0)
  527. 13: p PUT 0
  528. 16: ( MARK
  529. 17: d DICT (MARK at 16)
  530. 18: p PUT 1
  531. 21: b BUILD
  532. 22: . STOP
  533. """
  534. pickle0 = ("(i__main__\n"
  535. "X\n"
  536. "p0\n"
  537. "(dp1\nb.").replace('X', xname)
  538. self.assert_is_copy(X(*args), self.loads(pickle0))
  539. # Protocol 1 (binary mode pickle)
  540. """
  541. 0: ( MARK
  542. 1: c GLOBAL '__main__ X'
  543. 13: q BINPUT 0
  544. 15: o OBJ (MARK at 0)
  545. 16: q BINPUT 1
  546. 18: } EMPTY_DICT
  547. 19: q BINPUT 2
  548. 21: b BUILD
  549. 22: . STOP
  550. """
  551. pickle1 = ('(c__main__\n'
  552. 'X\n'
  553. 'q\x00oq\x01}q\x02b.').replace('X', xname)
  554. self.assert_is_copy(X(*args), self.loads(pickle1))
  555. # Protocol 2 (pickle2 = '\x80\x02' + pickle1)
  556. """
  557. 0: \x80 PROTO 2
  558. 2: ( MARK
  559. 3: c GLOBAL '__main__ X'
  560. 15: q BINPUT 0
  561. 17: o OBJ (MARK at 2)
  562. 18: q BINPUT 1
  563. 20: } EMPTY_DICT
  564. 21: q BINPUT 2
  565. 23: b BUILD
  566. 24: . STOP
  567. """
  568. pickle2 = ('\x80\x02(c__main__\n'
  569. 'X\n'
  570. 'q\x00oq\x01}q\x02b.').replace('X', xname)
  571. self.assert_is_copy(X(*args), self.loads(pickle2))
  572. def test_load_str(self):
  573. # From Python 2: pickle.dumps('a\x00\xa0', protocol=0)
  574. self.assertEqual(self.loads("S'a\\x00\\xa0'\n."), 'a\x00\xa0')
  575. # From Python 2: pickle.dumps('a\x00\xa0', protocol=1)
  576. self.assertEqual(self.loads('U\x03a\x00\xa0.'), 'a\x00\xa0')
  577. # From Python 2: pickle.dumps('a\x00\xa0', protocol=2)
  578. self.assertEqual(self.loads('\x80\x02U\x03a\x00\xa0.'), 'a\x00\xa0')
  579. def test_load_unicode(self):
  580. # From Python 2: pickle.dumps(u'Ï€', protocol=0)
  581. self.assertEqual(self.loads('V\\u03c0\n.'), u'Ï€')
  582. # From Python 2: pickle.dumps(u'Ï€', protocol=1)
  583. self.assertEqual(self.loads('X\x02\x00\x00\x00\xcf\x80.'), u'Ï€')
  584. # From Python 2: pickle.dumps(u'Ï€', protocol=2)
  585. self.assertEqual(self.loads('\x80\x02X\x02\x00\x00\x00\xcf\x80.'), u'Ï€')
  586. def test_constants(self):
  587. self.assertIsNone(self.loads('N.'))
  588. self.assertIs(self.loads('\x88.'), True)
  589. self.assertIs(self.loads('\x89.'), False)
  590. self.assertIs(self.loads('I01\n.'), True)
  591. self.assertIs(self.loads('I00\n.'), False)
  592. def test_misc_get(self):
  593. self.check_unpickling_error(self.error, 'g0\np0\n')
  594. self.check_unpickling_error(self.error, 'h\x00q\x00')
  595. def test_get(self):
  596. pickled = '((lp100000\ng100000\nt.'
  597. unpickled = self.loads(pickled)
  598. self.assertEqual(unpickled, ([],)*2)
  599. self.assertIs(unpickled[0], unpickled[1])
  600. def test_binget(self):
  601. pickled = '(]q\xffh\xfft.'
  602. unpickled = self.loads(pickled)
  603. self.assertEqual(unpickled, ([],)*2)
  604. self.assertIs(unpickled[0], unpickled[1])
  605. def test_long_binget(self):
  606. pickled = '(]r\x00\x00\x01\x00j\x00\x00\x01\x00t.'
  607. unpickled = self.loads(pickled)
  608. self.assertEqual(unpickled, ([],)*2)
  609. self.assertIs(unpickled[0], unpickled[1])
  610. def test_dup(self):
  611. pickled = '((l2t.'
  612. unpickled = self.loads(pickled)
  613. self.assertEqual(unpickled, ([],)*2)
  614. self.assertIs(unpickled[0], unpickled[1])
  615. def test_bad_stack(self):
  616. badpickles = [
  617. '.', # STOP
  618. '0', # POP
  619. '1', # POP_MARK
  620. '2', # DUP
  621. # '(2', # PyUnpickler doesn't raise
  622. 'R', # REDUCE
  623. ')R',
  624. 'a', # APPEND
  625. 'Na',
  626. 'b', # BUILD
  627. 'Nb',
  628. 'd', # DICT
  629. 'e', # APPENDS
  630. # '(e', # PyUnpickler raises AttributeError
  631. 'i__builtin__\nlist\n', # INST
  632. 'l', # LIST
  633. 'o', # OBJ
  634. '(o',
  635. 'p1\n', # PUT
  636. 'q\x00', # BINPUT
  637. 'r\x00\x00\x00\x00', # LONG_BINPUT
  638. 's', # SETITEM
  639. 'Ns',
  640. 'NNs',
  641. 't', # TUPLE
  642. 'u', # SETITEMS
  643. # '(u', # PyUnpickler doesn't raise
  644. '}(Nu',
  645. '\x81', # NEWOBJ
  646. ')\x81',
  647. '\x85', # TUPLE1
  648. '\x86', # TUPLE2
  649. 'N\x86',
  650. '\x87', # TUPLE3
  651. 'N\x87',
  652. 'NN\x87',
  653. ]
  654. for p in badpickles:
  655. self.check_unpickling_error(self.bad_stack_errors, p)
  656. def test_bad_mark(self):
  657. badpickles = [
  658. # 'N(.', # STOP
  659. 'N(2', # DUP
  660. 'c__builtin__\nlist\n)(R', # REDUCE
  661. 'c__builtin__\nlist\n()R',
  662. ']N(a', # APPEND
  663. # BUILD
  664. 'c__builtin__\nValueError\n)R}(b',
  665. 'c__builtin__\nValueError\n)R(}b',
  666. '(Nd', # DICT
  667. 'N(p1\n', # PUT
  668. 'N(q\x00', # BINPUT
  669. 'N(r\x00\x00\x00\x00', # LONG_BINPUT
  670. '}NN(s', # SETITEM
  671. '}N(Ns',
  672. '}(NNs',
  673. '}((u', # SETITEMS
  674. # NEWOBJ
  675. 'c__builtin__\nlist\n)(\x81',
  676. 'c__builtin__\nlist\n()\x81',
  677. 'N(\x85', # TUPLE1
  678. 'NN(\x86', # TUPLE2
  679. 'N(N\x86',
  680. 'NNN(\x87', # TUPLE3
  681. 'NN(N\x87',
  682. 'N(NN\x87',
  683. ]
  684. for p in badpickles:
  685. self.check_unpickling_error(self.bad_mark_errors, p)
  686. def test_truncated_data(self):
  687. self.check_unpickling_error(EOFError, '')
  688. self.check_unpickling_error(EOFError, 'N')
  689. badpickles = [
  690. 'F', # FLOAT
  691. 'F0.0',
  692. 'F0.00',
  693. 'G', # BINFLOAT
  694. 'G\x00\x00\x00\x00\x00\x00\x00',
  695. 'I', # INT
  696. 'I0',
  697. 'J', # BININT
  698. 'J\x00\x00\x00',
  699. 'K', # BININT1
  700. 'L', # LONG
  701. 'L0',
  702. 'L10',
  703. 'L0L',
  704. 'L10L',
  705. 'M', # BININT2
  706. 'M\x00',
  707. # 'P', # PERSID
  708. # 'Pabc',
  709. 'S', # STRING
  710. "S'abc'",
  711. 'T', # BINSTRING
  712. 'T\x03\x00\x00',
  713. 'T\x03\x00\x00\x00',
  714. 'T\x03\x00\x00\x00ab',
  715. 'U', # SHORT_BINSTRING
  716. 'U\x03',
  717. 'U\x03ab',
  718. 'V', # UNICODE
  719. 'Vabc',
  720. 'X', # BINUNICODE
  721. 'X\x03\x00\x00',
  722. 'X\x03\x00\x00\x00',
  723. 'X\x03\x00\x00\x00ab',
  724. '(c', # GLOBAL
  725. '(c__builtin__',
  726. '(c__builtin__\n',
  727. '(c__builtin__\nlist',
  728. 'Ng', # GET
  729. 'Ng0',
  730. '(i', # INST
  731. '(i__builtin__',
  732. '(i__builtin__\n',
  733. '(i__builtin__\nlist',
  734. 'Nh', # BINGET
  735. 'Nj', # LONG_BINGET
  736. 'Nj\x00\x00\x00',
  737. 'Np', # PUT
  738. 'Np0',
  739. 'Nq', # BINPUT
  740. 'Nr', # LONG_BINPUT
  741. 'Nr\x00\x00\x00',
  742. '\x80', # PROTO
  743. '\x82', # EXT1
  744. '\x83', # EXT2
  745. '\x84\x01',
  746. '\x84', # EXT4
  747. '\x84\x01\x00\x00',
  748. '\x8a', # LONG1
  749. '\x8b', # LONG4
  750. '\x8b\x00\x00\x00',
  751. ]
  752. for p in badpickles:
  753. self.check_unpickling_error(self.truncated_errors, p)
  754. class AbstractPickleTests(unittest.TestCase):
  755. # Subclass must define self.dumps, self.loads.
  756. _testdata = AbstractUnpickleTests._testdata
  757. def setUp(self):
  758. pass
  759. def test_misc(self):
  760. # test various datatypes not tested by testdata
  761. for proto in protocols:
  762. x = myint(4)
  763. s = self.dumps(x, proto)
  764. y = self.loads(s)
  765. self.assertEqual(x, y)
  766. x = (1, ())
  767. s = self.dumps(x, proto)
  768. y = self.loads(s)
  769. self.assertEqual(x, y)
  770. x = initarg(1, x)
  771. s = self.dumps(x, proto)
  772. y = self.loads(s)
  773. self.assertEqual(x, y)
  774. # XXX test __reduce__ protocol?
  775. def test_roundtrip_equality(self):
  776. expected = self._testdata
  777. for proto in protocols:
  778. s = self.dumps(expected, proto)
  779. got = self.loads(s)
  780. self.assertEqual(expected, got)
  781. # There are gratuitous differences between pickles produced by
  782. # pickle and cPickle, largely because cPickle starts PUT indices at
  783. # 1 and pickle starts them at 0. See XXX comment in cPickle's put2() --
  784. # there's a comment with an exclamation point there whose meaning
  785. # is a mystery. cPickle also suppresses PUT for objects with a refcount
  786. # of 1.
  787. def dont_test_disassembly(self):
  788. from pickletools import dis
  789. for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS):
  790. s = self.dumps(self._testdata, proto)
  791. filelike = cStringIO.StringIO()
  792. dis(s, out=filelike)
  793. got = filelike.getvalue()
  794. self.assertEqual(expected, got)
  795. def test_recursive_list(self):
  796. l = []
  797. l.append(l)
  798. for proto in protocols:
  799. s = self.dumps(l, proto)
  800. x = self.loads(s)
  801. self.assertIsInstance(x, list)
  802. self.assertEqual(len(x), 1)
  803. self.assertIs(x[0], x)
  804. def test_recursive_tuple_and_list(self):
  805. t = ([],)
  806. t[0].append(t)
  807. for proto in protocols:
  808. s = self.dumps(t, proto)
  809. x = self.loads(s)
  810. self.assertIsInstance(x, tuple)
  811. self.assertEqual(len(x), 1)
  812. self.assertIsInstance(x[0], list)
  813. self.assertEqual(len(x[0]), 1)
  814. self.assertIs(x[0][0], x)
  815. def test_recursive_dict(self):
  816. d = {}
  817. d[1] = d
  818. for proto in protocols:
  819. s = self.dumps(d, proto)
  820. x = self.loads(s)
  821. self.assertIsInstance(x, dict)
  822. self.assertEqual(x.keys(), [1])
  823. self.assertIs(x[1], x)
  824. def test_recursive_dict_key(self):
  825. d = {}
  826. k = K(d)
  827. d[k] = 1
  828. for proto in protocols:
  829. s = self.dumps(d, proto)
  830. x = self.loads(s)
  831. self.assertIsInstance(x, dict)
  832. self.assertEqual(len(x.keys()), 1)
  833. self.assertIsInstance(x.keys()[0], K)
  834. self.assertIs(x.keys()[0].value, x)
  835. def test_recursive_list_subclass(self):
  836. y = MyList()
  837. y.append(y)
  838. s = self.dumps(y, 2)
  839. x = self.loads(s)
  840. self.assertIsInstance(x, MyList)
  841. self.assertEqual(len(x), 1)
  842. self.assertIs(x[0], x)
  843. def test_recursive_dict_subclass(self):
  844. d = MyDict()
  845. d[1] = d
  846. s = self.dumps(d, 2)
  847. x = self.loads(s)
  848. self.assertIsInstance(x, MyDict)
  849. self.assertEqual(x.keys(), [1])
  850. self.assertIs(x[1], x)
  851. def test_recursive_dict_subclass_key(self):
  852. d = MyDict()
  853. k = K(d)
  854. d[k] = 1
  855. s = self.dumps(d, 2)
  856. x = self.loads(s)
  857. self.assertIsInstance(x, MyDict)
  858. self.assertEqual(len(x.keys()), 1)
  859. self.assertIsInstance(x.keys()[0], K)
  860. self.assertIs(x.keys()[0].value, x)
  861. def test_recursive_inst(self):
  862. i = C()
  863. i.attr = i
  864. for proto in protocols:
  865. s = self.dumps(i, proto)
  866. x = self.loads(s)
  867. self.assertIsInstance(x, C)
  868. self.assertEqual(dir(x), dir(i))
  869. self.assertIs(x.attr, x)
  870. def test_recursive_multi(self):
  871. l = []
  872. d = {1:l}
  873. i = C()
  874. i.attr = d
  875. l.append(i)
  876. for proto in protocols:
  877. s = self.dumps(l, proto)
  878. x = self.loads(s)
  879. self.assertIsInstance(x, list)
  880. self.assertEqual(len(x), 1)
  881. self.assertEqual(dir(x[0]), dir(i))
  882. self.assertEqual(x[0].attr.keys(), [1])
  883. self.assertTrue(x[0].attr[1] is x)
  884. def check_recursive_collection_and_inst(self, factory):
  885. h = H()
  886. y = factory([h])
  887. h.attr = y
  888. for proto in protocols:
  889. s = self.dumps(y, proto)
  890. x = self.loads(s)
  891. self.assertIsInstance(x, type(y))
  892. self.assertEqual(len(x), 1)
  893. self.assertIsInstance(list(x)[0], H)
  894. self.assertIs(list(x)[0].attr, x)
  895. def test_recursive_list_and_inst(self):
  896. self.check_recursive_collection_and_inst(list)
  897. def test_recursive_tuple_and_inst(self):
  898. self.check_recursive_collection_and_inst(tuple)
  899. def test_recursive_dict_and_inst(self):
  900. self.check_recursive_collection_and_inst(dict.fromkeys)
  901. def test_recursive_set_and_inst(self):
  902. self.check_recursive_collection_and_inst(set)
  903. def test_recursive_frozenset_and_inst(self):
  904. self.check_recursive_collection_and_inst(frozenset)
  905. def test_recursive_list_subclass_and_inst(self):
  906. self.check_recursive_collection_and_inst(MyList)
  907. def test_recursive_tuple_subclass_and_inst(self):
  908. self.check_recursive_collection_and_inst(MyTuple)
  909. def test_recursive_dict_subclass_and_inst(self):
  910. self.check_recursive_collection_and_inst(MyDict.fromkeys)
  911. if have_unicode:
  912. def test_unicode(self):
  913. endcases = [u'', u'<\\u>', u'<\\\u1234>', u'<\n>',
  914. u'<\\>', u'<\\\U00012345>',
  915. # surrogates
  916. u'<\udc80>']
  917. for proto in protocols:
  918. for u in endcases:
  919. p = self.dumps(u, proto)
  920. u2 = self.loads(p)
  921. self.assertEqual(u2, u)
  922. def test_unicode_high_plane(self):
  923. t = u'\U00012345'
  924. for proto in protocols:
  925. p = self.dumps(t, proto)
  926. t2 = self.loads(p)
  927. self.assertEqual(t2, t)
  928. def test_ints(self):
  929. import sys
  930. for proto in protocols:
  931. n = sys.maxint
  932. while n:
  933. for expected in (-n, n):
  934. s = self.dumps(expected, proto)
  935. n2 = self.loads(s)
  936. self.assertEqual(expected, n2)
  937. n = n >> 1
  938. def test_long(self):
  939. for proto in protocols:
  940. # 256 bytes is where LONG4 begins.
  941. for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257:
  942. nbase = 1L << nbits
  943. for npos in nbase-1, nbase, nbase+1:
  944. for n in npos, -npos:
  945. pickle = self.dumps(n, proto)
  946. got = self.loads(pickle)
  947. self.assertEqual(n, got)
  948. # Try a monster. This is quadratic-time in protos 0 & 1, so don't
  949. # bother with those.
  950. nbase = long("deadbeeffeedface", 16)
  951. nbase += nbase << 1000000
  952. for n in nbase, -nbase:
  953. p = self.dumps(n, 2)
  954. got = self.loads(p)
  955. self.assertEqual(n, got)
  956. def test_float(self):
  957. test_values = [0.0, 4.94e-324, 1e-310, 7e-308, 6.626e-34, 0.1, 0.5,
  958. 3.14, 263.44582062374053, 6.022e23, 1e30]
  959. test_values = test_values + [-x for x in test_values]
  960. for proto in protocols:
  961. for value in test_values:
  962. pickle = self.dumps(value, proto)
  963. got = self.loads(pickle)
  964. self.assertEqual(value, got)
  965. @run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
  966. def test_float_format(self):
  967. # make sure that floats are formatted locale independent
  968. self.assertEqual(self.dumps(1.2)[0:3], 'F1.')
  969. def test_reduce(self):
  970. pass
  971. def test_getinitargs(self):
  972. pass
  973. def test_metaclass(self):
  974. a = use_metaclass()
  975. for proto in protocols:
  976. s = self.dumps(a, proto)
  977. b = self.loads(s)
  978. self.assertEqual(a.__class__, b.__class__)
  979. def test_dynamic_class(self):
  980. a = create_dynamic_class("my_dynamic_class", (object,))
  981. copy_reg.pickle(pickling_metaclass, pickling_metaclass.__reduce__)
  982. for proto in protocols:
  983. s = self.dumps(a, proto)
  984. b = self.loads(s)
  985. self.assertEqual(a, b)
  986. self.assertIs(a.__class__, b.__class__)
  987. def test_structseq(self):
  988. import time
  989. import os
  990. t = time.localtime()
  991. for proto in protocols:
  992. s = self.dumps(t, proto)
  993. u = self.loads(s)
  994. self.assertEqual(t, u)
  995. if hasattr(os, "stat"):
  996. t = os.stat(os.curdir)
  997. s = self.dumps(t, proto)
  998. u = self.loads(s)
  999. self.assertEqual(t, u)
  1000. if hasattr(os, "statvfs"):
  1001. t = os.statvfs(os.curdir)
  1002. s = self.dumps(t, proto)
  1003. u = self.loads(s)
  1004. self.assertEqual(t, u)
  1005. # Tests for protocol 2
  1006. def test_proto(self):
  1007. build_none = pickle.NONE + pickle.STOP
  1008. for proto in protocols:
  1009. expected = build_none
  1010. if proto >= 2:
  1011. expected = pickle.PROTO + chr(proto) + expected
  1012. p = self.dumps(None, proto)
  1013. self.assertEqual(p, expected)
  1014. oob = protocols[-1] + 1 # a future protocol
  1015. badpickle = pickle.PROTO + chr(oob) + build_none
  1016. try:
  1017. self.loads(badpickle)
  1018. except ValueError, detail:
  1019. self.assertTrue(str(detail).startswith(
  1020. "unsupported pickle protocol"))
  1021. else:
  1022. self.fail("expected bad protocol number to raise ValueError")
  1023. def test_long1(self):
  1024. x = 12345678910111213141516178920L
  1025. for proto in protocols:
  1026. s = self.dumps(x, proto)
  1027. y = self.loads(s)
  1028. self.assertEqual(x, y)
  1029. self.assertEqual(opcode_in_pickle(pickle.LONG1, s), proto >= 2)
  1030. def test_long4(self):
  1031. x = 12345678910111213141516178920L << (256*8)
  1032. for proto in protocols:
  1033. s = self.dumps(x, proto)
  1034. y = self.loads(s)
  1035. self.assertEqual(x, y)
  1036. self.assertEqual(opcode_in_pickle(pickle.LONG4, s), proto >= 2)
  1037. def test_short_tuples(self):
  1038. # Map (proto, len(tuple)) to expected opcode.
  1039. expected_opcode = {(0, 0): pickle.TUPLE,
  1040. (0, 1): pickle.TUPLE,
  1041. (0, 2): pickle.TUPLE,
  1042. (0, 3): pickle.TUPLE,
  1043. (0, 4): pickle.TUPLE,
  1044. (1, 0): pickle.EMPTY_TUPLE,
  1045. (1, 1): pickle.TUPLE,
  1046. (1, 2): pickle.TUPLE,
  1047. (1, 3): pickle.TUPLE,
  1048. (1, 4): pickle.TUPLE,
  1049. (2, 0): pickle.EMPTY_TUPLE,
  1050. (2, 1): pickle.TUPLE1,
  1051. (2, 2): pickle.TUPLE2,
  1052. (2, 3): pickle.TUPLE3,
  1053. (2, 4): pickle.TUPLE,
  1054. }
  1055. a = ()
  1056. b = (1,)
  1057. c = (1, 2)
  1058. d = (1, 2, 3)
  1059. e = (1, 2, 3, 4)
  1060. for proto in protocols:
  1061. for x in a, b, c, d, e:
  1062. s = self.dumps(x, proto)
  1063. y = self.loads(s)
  1064. self.assertEqual(x, y, (proto, x, s, y))
  1065. expected = expected_opcode[proto, len(x)]
  1066. self.assertEqual(opcode_in_pickle(expected, s), True)
  1067. def test_singletons(self):
  1068. # Map (proto, singleton) to expected opcode.
  1069. expected_opcode = {(0, None): pickle.NONE,
  1070. (1, None): pickle.NONE,
  1071. (2, None): pickle.NONE,
  1072. (0, True): pickle.INT,
  1073. (1, True): pickle.INT,
  1074. (2, True): pickle.NEWTRUE,
  1075. (0, False): pickle.INT,
  1076. (1, False): pickle.INT,
  1077. (2, False): pickle.NEWFALSE,
  1078. }
  1079. for proto in protocols:
  1080. for x in None, False, True:
  1081. s = self.dumps(x, proto)
  1082. y = self.loads(s)
  1083. self.assertTrue(x is y, (proto, x, s, y))
  1084. expected = expected_opcode[proto, x]
  1085. self.assertEqual(opcode_in_pickle(expected, s), True)
  1086. def test_newobj_tuple(self):
  1087. x = MyTuple([1, 2, 3])
  1088. x.foo = 42
  1089. x.bar = "hello"
  1090. for proto in protocols:
  1091. s = self.dumps(x, proto)
  1092. y = self.loads(s)
  1093. self.assertEqual(tuple(x), tuple(y))
  1094. self.assertEqual(x.__dict__, y.__dict__)
  1095. def test_newobj_list(self):
  1096. x = MyList([1, 2, 3])
  1097. x.foo = 42
  1098. x.bar = "hello"
  1099. for proto in protocols:
  1100. s = self.dumps(x, proto)
  1101. y = self.loads(s)
  1102. self.assertEqual(list(x), list(y))
  1103. self.assertEqual(x.__dict__, y.__dict__)
  1104. def test_newobj_generic(self):
  1105. for proto in protocols:
  1106. for C in myclasses:
  1107. B = C.__base__
  1108. x = C(C.sample)
  1109. x.foo = 42
  1110. s = self.dumps(x, proto)
  1111. y = self.loads(s)
  1112. detail = (proto, C, B, x, y, type(y))
  1113. self.assertEqual(B(x), B(y), detail)
  1114. self.assertEqual(x.__dict__, y.__dict__, detail)
  1115. def test_newobj_proxies(self):
  1116. # NEWOBJ should use the __class__ rather than the raw type
  1117. import weakref
  1118. classes = myclasses[:]
  1119. # Cannot create weakproxies to these classes
  1120. for c in (MyInt, MyLong, MyStr, MyTuple):
  1121. classes.remove(c)
  1122. for proto in protocols:
  1123. for C in classes:
  1124. B = C.__base__
  1125. x = C(C.sample)
  1126. x.foo = 42
  1127. p = weakref.proxy(x)
  1128. s = self.dumps(p, proto)
  1129. y = self.loads(s)
  1130. self.assertEqual(type(y), type(x)) # rather than type(p)
  1131. detail = (proto, C, B, x, y, type(y))
  1132. self.assertEqual(B(x), B(y), detail)
  1133. self.assertEqual(x.__dict__, y.__dict__, detail)
  1134. # Register a type with copy_reg, with extension code extcode. Pickle
  1135. # an object of that type. Check that the resulting pickle uses opcode
  1136. # (EXT[124]) under proto 2, and not in proto 1.
  1137. def produce_global_ext(self, extcode, opcode):
  1138. e = ExtensionSaver(extcode)
  1139. try:
  1140. copy_reg.add_extension(__name__, "MyList", extcode)
  1141. x = MyList([1, 2, 3])
  1142. x.foo = 42
  1143. x.bar = "hello"
  1144. # Dump using protocol 1 for comparison.
  1145. s1 = self.dumps(x, 1)
  1146. self.assertIn(__name__, s1)
  1147. self.assertIn("MyList", s1)
  1148. self.assertEqual(opcode_in_pickle(opcode, s1), False)
  1149. y = self.loads(s1)
  1150. self.assertEqual(list(x), list(y))
  1151. self.assertEqual(x.__dict__, y.__dict__)
  1152. # Dump using protocol 2 for test.
  1153. s2 = self.dumps(x, 2)
  1154. self.assertNotIn(__name__, s2)
  1155. self.assertNotIn("MyList", s2)
  1156. self.assertEqual(opcode_in_pickle(opcode, s2), True)
  1157. y = self.loads(s2)
  1158. self.assertEqual(list(x), list(y))
  1159. self.assertEqual(x.__dict__, y.__dict__)
  1160. finally:
  1161. e.restore()
  1162. def test_global_ext1(self):
  1163. self.produce_global_ext(0x00000001, pickle.EXT1) # smallest EXT1 code
  1164. self.produce_global_ext(0x000000ff, pickle.EXT1) # largest EXT1 code
  1165. def test_global_ext2(self):
  1166. self.produce_global_ext(0x00000100, pickle.EXT2) # smallest EXT2 code
  1167. self.produce_global_ext(0x0000ffff, pickle.EXT2) # largest EXT2 code
  1168. self.produce_global_ext(0x0000abcd, pickle.EXT2) # check endianness
  1169. def test_global_ext4(self):
  1170. self.produce_global_ext(0x00010000, pickle.EXT4) # smallest EXT4 code
  1171. self.produce_global_ext(0x7fffffff, pickle.EXT4) # largest EXT4 code
  1172. self.produce_global_ext(0x12abcdef, pickle.EXT4) # check endianness
  1173. def test_list_chunking(self):
  1174. n = 10 # too small to chunk
  1175. x = range(n)
  1176. for proto in protocols:
  1177. s = self.dumps(x, proto)
  1178. y = self.loads(s)
  1179. self.assertEqual(x, y)
  1180. num_appends = count_opcode(pickle.APPENDS, s)
  1181. self.assertEqual(num_appends, proto > 0)
  1182. n = 2500 # expect at least two chunks when proto > 0
  1183. x = range(n)
  1184. for proto in protocols:
  1185. s = self.dumps(x, proto)
  1186. y = self.loads(s)
  1187. self.assertEqual(x, y)
  1188. num_appends = count_opcode(pickle.APPENDS, s)
  1189. if proto == 0:
  1190. self.assertEqual(num_appends, 0)
  1191. else:
  1192. self.assertTrue(num_appends >= 2)
  1193. def test_dict_chunking(self):
  1194. n = 10 # too small to chunk
  1195. x = dict.fromkeys(range(n))
  1196. for proto in protocols:
  1197. s = self.dumps(x, proto)
  1198. y = self.loads(s)
  1199. self.assertEqual(x, y)
  1200. num_setitems = count_opcode(pickle.SETITEMS, s)
  1201. self.assertEqual(num_setitems, proto > 0)
  1202. n = 2500 # expect at least two chunks when proto > 0
  1203. x = dict.fromkeys(range(n))
  1204. for proto in protocols:
  1205. s = self.dumps(x, proto)
  1206. y = self.loads(s)
  1207. self.assertEqual(x, y)
  1208. num_setitems = count_opcode(pickle.SETITEMS, s)
  1209. if proto == 0:
  1210. self.assertEqual(num_setitems, 0)
  1211. else:
  1212. self.assertTrue(num_setitems >= 2)
  1213. def test_simple_newobj(self):
  1214. x = SimpleNewObj.__new__(SimpleNewObj, 0xface) # avoid __init__
  1215. x.abc = 666
  1216. for proto in protocols:
  1217. s = self.dumps(x, proto)
  1218. if proto < 1:
  1219. self.assertIn('\nI64206', s) # INT
  1220. else:
  1221. self.assertIn('M\xce\xfa', s) # BININT2
  1222. self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2)
  1223. y = self.loads(s) # will raise TypeError if __init__ called
  1224. self.assertEqual(y.abc, 666)
  1225. self.assertEqual(x.__dict__, y.__dict__)
  1226. def test_complex_newobj(self):
  1227. x = ComplexNewObj.__new__(ComplexNewObj, 0xface) # avoid __init__
  1228. x.abc = 666
  1229. for proto in protocols:
  1230. s = self.dumps(x, proto)
  1231. if proto < 1:
  1232. self.assertIn('\nI64206', s) # INT
  1233. elif proto < 2:
  1234. self.assertIn('M\xce\xfa', s) # BININT2
  1235. else:
  1236. self.assertIn('U\x04FACE', s) # SHORT_BINSTRING
  1237. self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2)
  1238. y = self.loads(s) # will raise TypeError if __init__ called
  1239. self.assertEqual(y.abc, 666)
  1240. self.assertEqual(x.__dict__, y.__dict__)
  1241. def test_newobj_list_slots(self):
  1242. x = SlotList([1, 2, 3])
  1243. x.foo = 42
  1244. x.bar = "hello"
  1245. s = self.dumps(x, 2)
  1246. y = self.loads(s)
  1247. self.assertEqual(list(x), list(y))
  1248. self.assertEqual(x.__dict__, y.__dict__)
  1249. self.assertEqual(x.foo, y.foo)
  1250. self.assertEqual(x.bar, y.bar)
  1251. def test_reduce_overrides_default_reduce_ex(self):
  1252. for proto in protocols:
  1253. x = REX_one()
  1254. self.assertEqual(x._reduce_called, 0)
  1255. s = self.dumps(x, proto)
  1256. self.assertEqual(x._reduce_called, 1)
  1257. y = self.loads(s)
  1258. self.assertEqual(y._reduce_called, 0)
  1259. def test_reduce_ex_called(self):
  1260. for proto in protocols:
  1261. x = REX_two()
  1262. self.assertEqual(x._proto, None)
  1263. s = self.dumps(x, proto)
  1264. self.assertEqual(x._proto, proto)
  1265. y = self.loads(s)
  1266. self.assertEqual(y._proto, None)
  1267. def test_reduce_ex_overrides_reduce(self):
  1268. for proto in protocols:
  1269. x = REX_three()
  1270. self.assertEqual(x._proto, None)
  1271. s = self.dumps(x, proto)
  1272. self.assertEqual(x._proto, proto)
  1273. y = self.loads(s)
  1274. self.assertEqual(y._proto, None)
  1275. def test_reduce_ex_calls_base(self):
  1276. for proto in protocols:
  1277. x = REX_four()
  1278. self.assertEqual(x._proto, None)
  1279. s = self.dumps(x, proto)
  1280. self.assertEqual(x._proto, proto)
  1281. y = self.loads(s)
  1282. self.assertEqual(y._proto, proto)
  1283. def test_reduce_calls_base(self):
  1284. for proto in protocols:
  1285. x = REX_five()
  1286. self.assertEqual(x._reduce_called, 0)
  1287. s = self.dumps(x, proto)
  1288. self.assertEqual(x._reduce_called, 1)
  1289. y = self.loads(s)
  1290. self.assertEqual(y._reduce_called, 1)
  1291. @no_tracing
  1292. def test_bad_getattr(self):
  1293. # Issue #3514: crash when there is an infinite loop in __getattr__
  1294. x = BadGetattr()
  1295. for proto in protocols:
  1296. self.assertRaises(RuntimeError, self.dumps, x, proto)
  1297. def test_reduce_bad_iterator(self):
  1298. # Issue4176: crash when 4th and 5th items of __reduce__()
  1299. # are not iterators
  1300. class C(object):
  1301. def __reduce__(self):
  1302. # 4th item is not an iterator
  1303. return list, (), None, [], None
  1304. class D(object):
  1305. def __reduce__(self):
  1306. # 5th item is not an iterator
  1307. return dict, (), None, None, []
  1308. # Protocol 0 in Python implementation is less strict and also accepts
  1309. # iterables.
  1310. for proto in protocols:
  1311. try:
  1312. self.dumps(C(), proto)
  1313. except (AttributeError, pickle.PicklingError, cPickle.PicklingError):
  1314. pass
  1315. try:
  1316. self.dumps(D(), proto)
  1317. except (AttributeError, pickle.PicklingError, cPickle.PicklingError):
  1318. pass
  1319. def test_many_puts_and_gets(self):
  1320. # Test that internal data structures correctly deal with lots of
  1321. # puts/gets.
  1322. keys = ("aaa" + str(i) for i in xrange(100))
  1323. large_dict = dict((k, [4, 5, 6]) for k in keys)
  1324. obj = [dict(large_dict), dict(large_dict), dict(large_dict)]
  1325. for proto in protocols:
  1326. dumped = self.dumps(obj, proto)
  1327. loaded = self.loads(dumped)
  1328. self.assertEqual(loaded, obj,
  1329. "Failed protocol %d: %r != %r"
  1330. % (proto, obj, loaded))
  1331. def test_attribute_name_interning(self):
  1332. # Test that attribute names of pickled objects are interned when
  1333. # unpickling.
  1334. for proto in protocols:
  1335. x = C()
  1336. x.foo = 42
  1337. x.bar = "hello"
  1338. s = self.dumps(x, proto)
  1339. y = self.loads(s)
  1340. x_keys = sorted(x.__dict__)
  1341. y_keys = sorted(y.__dict__)
  1342. for x_key, y_key in zip(x_keys, y_keys):
  1343. self.assertIs(x_key, y_key)
  1344. def test_large_pickles(self):
  1345. # Test the correctness of internal buffering routines when handling
  1346. # large data.
  1347. for proto in protocols:
  1348. data = (1, min, 'xy' * (30 * 1024), len)
  1349. dumped = self.dumps(data, proto)
  1350. loaded = self.loads(dumped)
  1351. self.assertEqual(len(loaded), len(data))
  1352. self.assertEqual(loaded, data)
  1353. def _check_pickling_with_opcode(self, obj, opcode, proto):
  1354. pickled = self.dumps(obj, proto)
  1355. self.assertTrue(opcode_in_pickle(opcode, pickled))
  1356. unpickled = self.loads(pickled)
  1357. self.assertEqual(obj, unpickled)
  1358. def test_appends_on_non_lists(self):
  1359. # Issue #17720
  1360. obj = REX_six([1, 2, 3])
  1361. for proto in protocols:
  1362. if proto == 0:
  1363. self._check_pickling_with_opcode(obj, pickle.APPEND, proto)
  1364. else:
  1365. self._check_pickling_with_opcode(obj, pickle.APPENDS, proto)
  1366. def test_setitems_on_non_dicts(self):
  1367. obj = REX_seven({1: -1, 2: -2, 3: -3})
  1368. for proto in protocols:
  1369. if proto == 0:
  1370. self._check_pickling_with_opcode(obj, pickle.SETITEM, proto)
  1371. else:
  1372. self._check_pickling_with_opcode(obj, pickle.SETITEMS, proto)
  1373. # Test classes for reduce_ex
  1374. class REX_one(object):
  1375. _reduce_called = 0
  1376. def __reduce__(self):
  1377. self._reduce_called = 1
  1378. return REX_one, ()
  1379. # No __reduce_ex__ here, but inheriting it from object
  1380. class REX_two(object):
  1381. _proto = None
  1382. def __reduce_ex__(self, proto):
  1383. self._proto = proto
  1384. return REX_two, ()
  1385. # No __reduce__ here, but inheriting it from object
  1386. class REX_three(object):
  1387. _proto = None
  1388. def __reduce_ex__(self, proto):
  1389. self._proto = proto
  1390. return REX_two, ()
  1391. def __reduce__(self):
  1392. raise TestFailed, "This __reduce__ shouldn't be called"
  1393. class REX_four(object):
  1394. _proto = None
  1395. def __reduce_ex__(self, proto):
  1396. self._proto = proto
  1397. return object.__reduce_ex__(self, proto)
  1398. # Calling base class method should succeed
  1399. class REX_five(object):
  1400. _reduce_called = 0
  1401. def __reduce__(self):
  1402. self._reduce_called = 1
  1403. return object.__reduce__(self)
  1404. # This one used to fail with infinite recursion
  1405. class REX_six(object):
  1406. """This class is used to check the 4th argument (list iterator) of
  1407. the reduce protocol.
  1408. """
  1409. def __init__(self, items=None):
  1410. if items is None:
  1411. items = []
  1412. self.items = items
  1413. def __eq__(self, other):
  1414. return type(self) is type(other) and self.items == other.items
  1415. def append(self, item):
  1416. self.items.append(item)
  1417. def extend(self, items):
  1418. for item in items:
  1419. self.append(item)
  1420. def __reduce__(self):
  1421. return type(self), (), None, iter(self.items), None
  1422. class REX_seven(object):
  1423. """This class is used to check the 5th argument (dict iterator) of
  1424. the reduce protocol.
  1425. """
  1426. def __init__(self, table=None):
  1427. if table is None:
  1428. table = {}
  1429. self.table = table
  1430. def __eq__(self, other):
  1431. return type(self) is type(other) and self.table == other.table
  1432. def __setitem__(self, key, value):
  1433. self.table[key] = value
  1434. def __reduce__(self):
  1435. return type(self), (), None, None, iter(self.table.items())
  1436. # Test classes for newobj
  1437. class MyInt(int):
  1438. sample = 1
  1439. class MyLong(long):
  1440. sample = 1L
  1441. class MyFloat(float):
  1442. sample = 1.0
  1443. class MyComplex(complex):
  1444. sample = 1.0 + 0.0j
  1445. class MyStr(str):
  1446. sample = "hello"
  1447. class MyUnicode(unicode):
  1448. sample = u"hello \u1234"
  1449. class MyTuple(tuple):
  1450. sample = (1, 2, 3)
  1451. class MyList(list):
  1452. sample = [1, 2, 3]
  1453. class MyDict(dict):
  1454. sample = {"a": 1, "b": 2}
  1455. myclasses = [MyInt, MyLong, MyFloat,
  1456. MyComplex,
  1457. MyStr, MyUnicode,
  1458. MyTuple, MyList, MyDict]
  1459. class SlotList(MyList):
  1460. __slots__ = ["foo"]
  1461. class SimpleNewObj(int):
  1462. def __init__(self, *args, **kwargs):
  1463. # raise an error, to make sure this isn't called
  1464. raise TypeError("SimpleNewObj.__init__() didn't expect to get called")
  1465. def __eq__(self, other):
  1466. return int(self) == int(other) and self.__dict__ == other.__dict__
  1467. class ComplexNewObj(SimpleNewObj):
  1468. def __getnewargs__(self):
  1469. return ('%X' % self, 16)
  1470. class BadGetattr:
  1471. def __getattr__(self, key):
  1472. self.foo
  1473. class AbstractPickleModuleTests(unittest.TestCase):
  1474. def test_dump_closed_file(self):
  1475. import os
  1476. f = open(TESTFN, "w")
  1477. try:
  1478. f.close()
  1479. self.assertRaises(ValueError, self.module.dump, 123, f)
  1480. finally:
  1481. os.remove(TESTFN)
  1482. def test_load_closed_file(self):
  1483. import os
  1484. f = open(TESTFN, "w")
  1485. try:
  1486. f.close()
  1487. self.assertRaises(ValueError, self.module.dump, 123, f)
  1488. finally:
  1489. os.remove(TESTFN)
  1490. def test_load_from_and_dump_to_file(self):
  1491. stream = cStringIO.StringIO()
  1492. data = [123, {}, 124]
  1493. self.module.dump(data, stream)
  1494. stream.seek(0)
  1495. unpickled = self.module.load(stream)
  1496. self.assertEqual(unpickled, data)
  1497. def test_highest_protocol(self):
  1498. # Of course this needs to be changed when HIGHEST_PROTOCOL changes.
  1499. self.assertEqual(self.module.HIGHEST_PROTOCOL, 2)
  1500. def test_callapi(self):
  1501. f = cStringIO.StringIO()
  1502. # With and without keyword arguments
  1503. self.module.dump(123, f, -1)
  1504. self.module.dump(123, file=f, protocol=-1)
  1505. self.module.dumps(123, -1)
  1506. self.module.dumps(123, protocol=-1)
  1507. self.module.Pickler(f, -1)
  1508. self.module.Pickler(f, protocol=-1)
  1509. def test_incomplete_input(self):
  1510. s = StringIO.StringIO("X''.")
  1511. self.assertRaises(EOFError, self.module.load, s)
  1512. def test_restricted(self):
  1513. # issue7128: cPickle failed in restricted mode
  1514. builtins = {self.module.__name__: self.module,
  1515. '__import__': __import__}
  1516. d = {}
  1517. teststr = "def f(): {0}.dumps(0)".format(self.module.__name__)
  1518. exec teststr in {'__builtins__': builtins}, d
  1519. d['f']()
  1520. def test_bad_input(self):
  1521. # Test issue4298
  1522. s = '\x58\0\0\0\x54'
  1523. self.assertRaises(EOFError, self.module.loads, s)
  1524. class AbstractPersistentPicklerTests(unittest.TestCase):
  1525. # This class defines persistent_id() and persistent_load()
  1526. # functions that should be used by the pickler. All even integers
  1527. # are pickled using persistent ids.
  1528. def persistent_id(self, object):
  1529. if isinstance(object, int) and object % 2 == 0:
  1530. self.id_count += 1
  1531. return str(object)
  1532. elif object == "test_false_value":
  1533. self.false_count += 1
  1534. return ""
  1535. else:
  1536. return None
  1537. def persistent_load(self, oid):
  1538. if not oid:
  1539. self.load_false_count += 1
  1540. return "test_false_value"
  1541. else:
  1542. self.load_count += 1
  1543. object = int(oid)
  1544. assert object % 2 == 0
  1545. return object
  1546. def test_persistence(self):
  1547. L = range(10) + ["test_false_value"]
  1548. for proto in protocols:
  1549. self.id_count = 0
  1550. self.false_count = 0
  1551. self.load_false_count = 0
  1552. self.load_count = 0
  1553. self.assertEqual(self.loads(self.dumps(L, proto)), L)
  1554. self.assertEqual(self.id_count, 5)
  1555. self.assertEqual(self.false_count, 1)
  1556. self.assertEqual(self.load_count, 5)
  1557. self.assertEqual(self.load_false_count, 1)
  1558. class AbstractPicklerUnpicklerObjectTests(unittest.TestCase):
  1559. pickler_class = None
  1560. unpickler_class = None
  1561. def setUp(self):
  1562. assert self.pickler_class
  1563. assert self.unpickler_class
  1564. def test_clear_pickler_memo(self):
  1565. # To test whether clear_memo() has any effect, we pickle an object,
  1566. # then pickle it again without clearing the memo; the two serialized
  1567. # forms should be different. If we clear_memo() and then pickle the
  1568. # object again, the third serialized form should be identical to the
  1569. # first one we obtained.
  1570. data = ["abcdefg", "abcdefg", 44]
  1571. f = cStringIO.StringIO()
  1572. pickler = self.pickler_class(f)
  1573. pickler.dump(data)
  1574. first_pickled = f.getvalue()
  1575. # Reset StringIO object.
  1576. f.seek(0)
  1577. f.truncate()
  1578. pickler.dump(data)
  1579. second_pickled = f.getvalue()
  1580. # Reset the Pickler and StringIO objects.
  1581. pickler.clear_memo()
  1582. f.seek(0)
  1583. f.truncate()
  1584. pickler.dump(data)
  1585. third_pickled = f.getvalue()
  1586. self.assertNotEqual(first_pickled, second_pickled)
  1587. self.assertEqual(first_pickled, third_pickled)
  1588. def test_priming_pickler_memo(self):
  1589. # Verify that we can set the Pickler's memo attribute.
  1590. data = ["abcdefg", "abcdefg", 44]
  1591. f = cStringIO.StringIO()
  1592. pickler = self.pickler_class(f)
  1593. pickler.dump(data)
  1594. first_pickled = f.getvalue()
  1595. f = cStringIO.StringIO()
  1596. primed = self.pickler_class(f)
  1597. primed.memo = pickler.memo
  1598. primed.dump(data)
  1599. primed_pickled = f.getvalue()
  1600. self.assertNotEqual(first_pickled, primed_pickled)
  1601. def test_priming_unpickler_memo(self):
  1602. # Verify that we can set the Unpickler's memo attribute.
  1603. data = ["abcdefg", "abcdefg", 44]
  1604. f = cStringIO.StringIO()
  1605. pickler = self.pickler_class(f)
  1606. pickler.dump(data)
  1607. first_pickled = f.getvalue()
  1608. f = cStringIO.StringIO()
  1609. primed = self.pickler_class(f)
  1610. primed.memo = pickler.memo
  1611. primed.dump(data)
  1612. primed_pickled = f.getvalue()
  1613. unpickler = self.unpickler_class(cStringIO.StringIO(first_pickled))
  1614. unpickled_data1 = unpickler.load()
  1615. self.assertEqual(unpickled_data1, data)
  1616. primed = self.unpickler_class(cStringIO.StringIO(primed_pickled))
  1617. primed.memo = unpickler.memo
  1618. unpickled_data2 = primed.load()
  1619. primed.memo.clear()
  1620. self.assertEqual(unpickled_data2, data)
  1621. self.assertTrue(unpickled_data2 is unpickled_data1)
  1622. def test_reusing_unpickler_objects(self):
  1623. data1 = ["abcdefg", "abcdefg", 44]
  1624. f = cStringIO.StringIO()
  1625. pickler = self.pickler_class(f)
  1626. pickler.dump(data1)
  1627. pickled1 = f.getvalue()
  1628. data2 = ["abcdefg", 44, 44]
  1629. f = cStringIO.StringIO()
  1630. pickler = self.pickler_class(f)
  1631. pickler.dump(data2)
  1632. pickled2 = f.getvalue()
  1633. f = cStringIO.StringIO()
  1634. f.write(pickled1)
  1635. f.seek(0)
  1636. unpickler = self.unpickler_class(f)
  1637. self.assertEqual(unpickler.load(), data1)
  1638. f.seek(0)
  1639. f.truncate()
  1640. f.write(pickled2)
  1641. f.seek(0)
  1642. self.assertEqual(unpickler.load(), data2)
  1643. def _check_multiple_unpicklings(self, ioclass, seekable):
  1644. for proto in protocols:
  1645. data1 = [(x, str(x)) for x in xrange(2000)] + ["abcde", len]
  1646. f = ioclass()
  1647. pickler = self.pickler_class(f, protocol=proto)
  1648. pickler.dump(data1)
  1649. pickled = f.getvalue()
  1650. N = 5
  1651. f = ioclass(pickled * N)
  1652. unpickler = self.unpickler_class(f)
  1653. for i in xrange(N):
  1654. if seekable:
  1655. pos = f.tell()
  1656. self.assertEqual(unpickler.load(), data1)
  1657. if seekable:
  1658. self.assertEqual(f.tell(), pos + len(pickled))
  1659. self.assertRaises(EOFError, unpickler.load)
  1660. def test_multiple_unpicklings_seekable(self):
  1661. self._check_multiple_unpicklings(StringIO.StringIO, True)
  1662. def test_multiple_unpicklings_unseekable(self):
  1663. self._check_multiple_unpicklings(UnseekableIO, False)
  1664. def test_unpickling_buffering_readline(self):
  1665. # Issue #12687: the unpickler's buffering logic could fail with
  1666. # text mode opcodes.
  1667. import io
  1668. data = list(xrange(10))
  1669. for proto in protocols:
  1670. for buf_size in xrange(1, 11):
  1671. f = io.BufferedRandom(io.BytesIO(), buffer_size=buf_size)
  1672. pickler = self.pickler_class(f, protocol=proto)
  1673. pickler.dump(data)
  1674. f.seek(0)
  1675. unpickler = self.unpickler_class(f)
  1676. self.assertEqual(unpickler.load(), data)
  1677. class BigmemPickleTests(unittest.TestCase):
  1678. # Memory requirements: 1 byte per character for input strings, 1 byte
  1679. # for pickled data, 1 byte for unpickled strings, 1 byte for internal
  1680. # buffer and 1 byte of free space for resizing of internal buffer.
  1681. @precisionbigmemtest(size=_2G + 100*_1M, memuse=5)
  1682. def test_huge_strlist(self, size):
  1683. chunksize = 2**20
  1684. data = []
  1685. while size > chunksize:
  1686. data.append('x' * chunksize)
  1687. size -= chunksize
  1688. chunksize += 1
  1689. data.append('y' * size)
  1690. try:
  1691. for proto in protocols:
  1692. try:
  1693. pickled = self.dumps(data, proto)
  1694. res = self.loads(pickled)
  1695. self.assertEqual(res, data)
  1696. finally:
  1697. res = None
  1698. pickled = None
  1699. finally:
  1700. data = None