test_deepcopy.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. """
  2. This file was modified from CPython.
  3. Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
  4. 2011, 2012, 2013, 2014, 2015, 2016 Python Software Foundation; All Rights Reserved
  5. """
  6. import copy, types
  7. # import copy_reg
  8. # import weakref
  9. # import abc
  10. from operator import le, lt, ge, gt, eq, ne
  11. import unittest
  12. order_comparisons = le, lt, ge, gt
  13. equality_comparisons = eq, ne
  14. comparisons = order_comparisons + equality_comparisons
  15. n = []
  16. class _deep_reduce_ex:
  17. def __reduce_ex__(self, proto):
  18. n.append(1)
  19. print "append to n"
  20. return ""
  21. def __reduce__(self):
  22. self.fail("shouldn't call this")
  23. class Deepcopy_Deepcopy:
  24. def __init__(self, foo):
  25. self.foo = foo
  26. def __deepcopy__(self, memo=None):
  27. return Deepcopy_Deepcopy(self.foo)
  28. e = []
  29. class deep_reduce:
  30. def __reduce__(self):
  31. e.append(1)
  32. print "append to e"
  33. return ""
  34. class Inst_Deepcopy:
  35. def __init__(self, foo):
  36. self.foo = foo
  37. def __deepcopy__(self, memo):
  38. return Inst_Deepcopy(copy.deepcopy(self.foo, memo))
  39. def __eq__(self, other):
  40. return self.foo == other.foo
  41. class reconstruct_nostate(object):
  42. def __reduce__(self):
  43. return (reconstruct_nostate, ())
  44. class reconstruct_state():
  45. def __reduce__(self):
  46. return (reconstruct_state, (), self.__dict__)
  47. def __eq__(self, other):
  48. return self.__dict__ == other.__dict__
  49. class reconstruct_state_setstate(object):
  50. def __reduce__(self):
  51. return (reconstruct_state_setstate, (), self.__dict__)
  52. def __setstate__(self, state):
  53. self.__dict__.update(state)
  54. def __eq__(self, other):
  55. return self.__dict__ == other.__dict__
  56. class reduce_4tuple(list):
  57. def __reduce__(self):
  58. return (reduce_4tuple, (), self.__dict__, iter(self))
  59. def __eq__(self, other):
  60. return (list(self) == list(other) and
  61. self.__dict__ == other.__dict__)
  62. class reduce_5tuple(dict):
  63. def __reduce__(self):
  64. return (reduce_5tuple, (), self.__dict__, None, self.items())
  65. def __eq__(self, other):
  66. return (dict(self) == dict(other) and
  67. self.__dict__ == other.__dict__)
  68. class TestCopy(unittest.TestCase):
  69. # # The deepcopy() method
  70. def test_deepcopy_basic(self):
  71. x = 42
  72. y = copy.deepcopy(x)
  73. self.assertEqual(y, x)
  74. def test_deepcopy_memo(self):
  75. # Tests of reflexive objects are under type-specific sections below.
  76. # This tests only repetitions of objects.
  77. x = []
  78. x = [x, x]
  79. y = copy.deepcopy(x)
  80. self.assertEqual(y, x)
  81. self.assertIsNot(y, x)
  82. self.assertIsNot(y[0], x[0])
  83. self.assertIs(y[0], y[1])
  84. def test_deepcopy_issubclass(self):
  85. # XXX Note: there's no way to test the TypeError coming out of
  86. # issubclass() -- this can only happen when an extension
  87. # module defines a "type" that doesn't formally inherit from
  88. # type.
  89. # class Meta(type):
  90. class Meta():
  91. pass
  92. class C(Meta):
  93. pass
  94. self.assertEqual(copy.deepcopy(C), C)
  95. def test_deepcopy_deepcopy(self):
  96. x = Deepcopy_Deepcopy(42)
  97. y = copy.deepcopy(x)
  98. self.assertEqual(y.__class__, x.__class__)
  99. self.assertEqual(y.foo, x.foo)
  100. # def test_deepcopy_registry(self):
  101. # class C(object):
  102. # def __new__(cls, foo):
  103. # obj = object.__new__(cls)
  104. # obj.foo = foo
  105. # return obj
  106. # def pickle_C(obj):
  107. # return (C, (obj.foo,))
  108. # x = C(42)
  109. # self.assertRaises(TypeError, copy.deepcopy, x)
  110. # # copy_reg.pickle(C, pickle_C, C)
  111. # # y = copy.deepcopy(x)
  112. def test_deepcopy_reduce_ex(self):
  113. x = _deep_reduce_ex()
  114. y = copy.deepcopy(x)
  115. self.assertEqual(n, [])
  116. # def test_deepcopy_reduce(self):
  117. # x = deep_reduce()
  118. # y = copy.deepcopy(x)
  119. # self.assertIs(y, x)
  120. # self.assertEqual(e, [1])
  121. # def test_deepcopy_cant(self):
  122. # class C:
  123. # def __getattribute__(self, name):
  124. # if name.startswith("__reduce"):
  125. # raise AttributeError(name)
  126. # return object.__getattribute__(self, name)
  127. # x = C()
  128. # # print "Should have error here", copy.deepcopy(x)
  129. # self.assertRaises(TypeError, copy.deepcopy, x)
  130. # # Type-specific _deepcopy_xxx() methods
  131. def test_deepcopy_atomic(self):
  132. class Classic:
  133. pass
  134. class NewStyle(object):
  135. pass
  136. def f():
  137. pass
  138. tests = [None, 42, 3.14, True, False, "hello"]
  139. # not implemented yet : f.__code__, 2**100, 1j, NewStyle, max, "hello\u1234", Classic]
  140. for x in tests:
  141. self.assertIs(copy.deepcopy(x), x)
  142. def test_deepcopy_list(self):
  143. x = [[1, 2], 3]
  144. y = copy.deepcopy(x)
  145. self.assertEqual(y, x)
  146. self.assertIsNot(x, y)
  147. self.assertIsNot(x[0], y[0])
  148. def test_deepcopy_reflexive_list(self):
  149. x = []
  150. x.append(x)
  151. y = copy.deepcopy(x)
  152. # for op in comparisons:
  153. # self.assertRaises(RecursionError, op, y, x)
  154. self.assertIsNot(y, x)
  155. self.assertIs(y[0], y)
  156. self.assertEqual(len(y), 1)
  157. def test_deepcopy_set(self):
  158. class FooSet:
  159. def bar(self):
  160. pass
  161. a = set([1,2,3])
  162. b = copy.deepcopy(a)
  163. a = set([1,2,3])
  164. b = copy.deepcopy(a)
  165. self.assertTrue(a == b)
  166. self.assertFalse(a is b)
  167. mixed = set([(1,2), FooSet.bar])
  168. mixedcopy = copy.deepcopy(mixed)
  169. self.assertTrue(mixed == mixedcopy)
  170. self.assertFalse(mixed is mixedcopy)
  171. mixed.add(9)
  172. mixedcopy2 = copy.deepcopy(mixed)
  173. self.assertTrue(mixed == mixedcopy2)
  174. self.assertFalse(mixed is mixedcopy2)
  175. def test_deepcopy_empty_tuple(self):
  176. x = ()
  177. y = copy.deepcopy(x)
  178. self.assertIs(x, y)
  179. def test_deepcopy_tuple(self):
  180. x = ([1, 2], 3)
  181. y = copy.deepcopy(x)
  182. self.assertEqual(y, x)
  183. self.assertIsNot(x, y)
  184. self.assertIsNot(x[0], y[0])
  185. def test_deepcopy_tuple_of_immutables(self):
  186. x = ((1, 2), 3)
  187. y = copy.deepcopy(x)
  188. self.assertIs(x, y)
  189. # def test_deepcopy_reflexive_tuple(self):
  190. # x = ([],)
  191. # x[0].append(x)
  192. # print "here is x", x
  193. # print type(x[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0])
  194. # y = copy.deepcopy(x)
  195. # # for op in comparisons:
  196. # # self.assertRaises(RecursionError, op, y, x)
  197. # self.assertIsNot(y, x)
  198. # # self.assertIsNot(y[0], x[0])
  199. # # self.assertIs(y[0][0], y)
  200. def test_deepcopy_dict(self):
  201. x = {"foo": [1, 2], "bar": 3}
  202. y = copy.deepcopy(x)
  203. self.assertEqual(y, x)
  204. self.assertIsNot(x, y)
  205. self.assertIsNot(x["foo"], y["foo"])
  206. def test_deepcopy_reflexive_dict(self):
  207. x = {}
  208. x['foo'] = x
  209. y = copy.deepcopy(x)
  210. # for op in order_comparisons:
  211. # self.assertRaises(TypeError, op, y, x)
  212. # for op in equality_comparisons:
  213. # self.assertRaises(RecursionError, op, y, x)
  214. self.assertIsNot(y, x)
  215. self.assertIs(y['foo'], y)
  216. self.assertEqual(len(y), 1)
  217. def test_deepcopy_keepalive(self):
  218. memo = {}
  219. x = []
  220. y = copy.deepcopy(x, memo)
  221. self.assertIs(memo[id(memo)][0], x)
  222. def test_deepcopy_dont_memo_immutable(self):
  223. memo = {}
  224. x = [1, 2, 3, 4]
  225. y = copy.deepcopy(x, memo)
  226. self.assertEqual(y, x)
  227. # There's the entry for the new list, and the keep alive.
  228. self.assertEqual(len(memo), 6)
  229. memo = {}
  230. x = [(1, 2)]
  231. y = copy.deepcopy(x, memo)
  232. self.assertEqual(y, x)
  233. # Tuples with immutable contents are immutable for deepcopy.
  234. self.assertEqual(len(memo), 5)
  235. memo = {}
  236. n = [False, True, None, "string"]
  237. o = copy.deepcopy(n, memo)
  238. self.assertEqual(len(memo), 6)
  239. def test_deepcopy_inst_vanilla(self):
  240. class C:
  241. def __init__(self, foo):
  242. self.foo = foo
  243. def __eq__(self, other):
  244. return self.foo == other.foo
  245. x = C([42])
  246. y = copy.deepcopy(x)
  247. self.assertEqual(y, x)
  248. self.assertIsNot(y.foo, x.foo)
  249. # print "types", type(y), type(x), x.__class__, y.__class__
  250. def test_deepcopy_inst_deepcopy(self):
  251. x = Inst_Deepcopy([42])
  252. y = copy.deepcopy(x)
  253. self.assertEqual(y, x)
  254. self.assertIsNot(y, x)
  255. self.assertIsNot(y.foo, x.foo)
  256. def test_deepcopy_inst_getinitargs(self):
  257. class C:
  258. def __init__(self, foo):
  259. self.foo = foo
  260. def __getinitargs__(self):
  261. return (self.foo,)
  262. def __eq__(self, other):
  263. return self.foo == other.foo
  264. x = C([42])
  265. self.assertRaises(NotImplementedError, copy.deepcopy, x)
  266. # y = copy.deepcopy(x)
  267. # self.assertEqual(y, x)
  268. # self.assertIsNot(y, x)
  269. # self.assertIsNot(y.foo, x.foo)
  270. # # def test_deepcopy_inst_getnewargs(self):
  271. # # class C(int):
  272. # # def __new__(cls, foo):
  273. # # self = int.__new__(cls)
  274. # # self.foo = foo
  275. # # return self
  276. # # def __getnewargs__(self):
  277. # # return self.foo,
  278. # # def __eq__(self, other):
  279. # # return self.foo == other.foo
  280. # # x = C(42)
  281. # # y = copy.deepcopy(x)
  282. # # self.assertIsInstance(y, C)
  283. # # print isinstance(y, C)
  284. # # self.assertEqual(y, x)
  285. # # self.assertIsNot(y, x)
  286. # # self.assertEqual(y.foo, x.foo)
  287. # # self.assertIsNot(y.foo, x.foo)
  288. # # def test_deepcopy_inst_getnewargs_ex(self):
  289. # # class C(int):
  290. # # def __new__(cls, *, foo):
  291. # # self = int.__new__(cls)
  292. # # self.foo = foo
  293. # # return self
  294. # # def __getnewargs_ex__(self):
  295. # # return (), {'foo': self.foo}
  296. # # def __eq__(self, other):
  297. # # return self.foo == other.foo
  298. # # x = C(foo=[42])
  299. # # y = copy.deepcopy(x)
  300. # # self.assertIsInstance(y, C)
  301. # # self.assertEqual(y, x)
  302. # # self.assertIsNot(y, x)
  303. # # self.assertEqual(y.foo, x.foo)
  304. # # self.assertIsNot(y.foo, x.foo)
  305. # def test_deepcopy_inst_getstate(self):
  306. # class C:
  307. # def __init__(self, foo):
  308. # self.foo = foo
  309. # def __getstate__(self):
  310. # return {"foo": self.foo}
  311. # def __eq__(self, other):
  312. # return self.foo == other.foo
  313. # x = C([42])
  314. # y = copy.deepcopy(x)
  315. # self.assertEqual(y, x)
  316. # self.assertIsNot(y, x)
  317. # self.assertIsNot(y.foo, x.foo)
  318. # def test_deepcopy_inst_setstate(self):
  319. # class C:
  320. # def __init__(self, foo):
  321. # self.foo = foo
  322. # def __setstate__(self, state):
  323. # self.foo = state["foo"]
  324. # def __eq__(self, other):
  325. # return self.foo == other.foo
  326. # x = C([42])
  327. # y = copy.deepcopy(x)
  328. # self.assertEqual(y, x)
  329. # self.assertIsNot(y, x)
  330. # self.assertIsNot(y.foo, x.foo)
  331. # # def test_deepcopy_inst_getstate_setstate(self):
  332. # # class C:
  333. # # def __init__(self, foo):
  334. # # self.foo = foo
  335. # # def __getstate__(self):
  336. # # return self.foo
  337. # # def __setstate__(self, state):
  338. # # self.foo = state
  339. # # def __eq__(self, other):
  340. # # return self.foo == other.foo
  341. # # x = C([42])
  342. # # y = copy.deepcopy(x)
  343. # # self.assertEqual(y, x)
  344. # # self.assertIsNot(y, x)
  345. # # self.assertIsNot(y.foo, x.foo)
  346. # # # State with boolean value is false (issue #25718)
  347. # # x = C([])
  348. # # y = copy.deepcopy(x)
  349. # # self.assertEqual(y, x)
  350. # # self.assertIsNot(y, x)
  351. # # self.assertIsNot(y.foo, x.foo)
  352. # def test_deepcopy_reflexive_inst(self):
  353. # class C:
  354. # pass
  355. # x = C()
  356. # x.foo = x
  357. # y = copy.deepcopy(x)
  358. # self.assertIsNot(y, x)
  359. # self.assertIs(y.foo, y)
  360. # def test_deepcopy_range(self):
  361. # class I(int):
  362. # pass
  363. # x = range(I(10))
  364. # y = copy.deepcopy(x)
  365. # self.assertIsNot(y, x)
  366. # self.assertEqual(y, x)
  367. # self.assertIsNot(y.stop, x.stop)
  368. # self.assertEqual(y.stop, x.stop)
  369. # self.assertIsInstance(y.stop, I)
  370. # # # _reconstruct()
  371. # def test_reconstruct_string(self):
  372. # class C:
  373. # def __reduce__(self):
  374. # return ""
  375. # x = C()
  376. # y = copy.copy(x)
  377. # self.assertIs(y, x)
  378. # y = copy.deepcopy(x)
  379. # self.assertIs(y, x)
  380. def test_reconstruct_nostate(self):
  381. x = reconstruct_nostate()
  382. x.foo = 42
  383. y = copy.copy(x)
  384. # self.assertIs(y.__class__, x.__class__)
  385. y = copy.deepcopy(x)
  386. # self.assertIs(y.__class__, x.__class__)
  387. def test_reconstruct_state(self):
  388. x = reconstruct_state()
  389. x.foo = [42]
  390. y = copy.copy(x)
  391. # self.assertEqual(y, x)
  392. y = copy.deepcopy(x)
  393. # self.assertEqual(y, x)
  394. self.assertIsNot(y.foo, x.foo)
  395. def test_reconstruct_state_setstate(self):
  396. x = reconstruct_state_setstate()
  397. x.foo = [42]
  398. # y = copy.copy(x)
  399. self.assertRaises(NotImplementedError, copy.copy, x)
  400. # self.assertEqual(y, x)
  401. # y = copy.deepcopy(x)
  402. # self.assertEqual(y, x)
  403. # self.assertIsNot(y.foo, x.foo)
  404. def test_reconstruct_reflexive(self):
  405. class C(object):
  406. pass
  407. x = C()
  408. x.foo = x
  409. y = copy.deepcopy(x)
  410. self.assertIsNot(y, x)
  411. self.assertIs(y.foo, y)
  412. # # Additions for Python 2.3 and pickle protocol 2
  413. # def test_reduce_4tuple(self):
  414. # x = reduce_4tuple([[1, 2], 3])
  415. # y = copy.copy(x)
  416. # self.assertEqual(x, y)
  417. # self.assertIsNot(x, y)
  418. # self.assertIs(x[0], y[0])
  419. # y = copy.deepcopy(x)
  420. # self.assertEqual(x, y)
  421. # self.assertIsNot(x, y)
  422. # self.assertIsNot(x[0], y[0])
  423. # def test_reduce_5tuple(self):
  424. # x = reduce_5tuple([("foo", [1, 2]), ("bar", 3)])
  425. # y = copy.copy(x)
  426. # # self.assertEqual(x, y)
  427. # self.assertIsNot(x, y)
  428. # self.assertIs(x["foo"], y["foo"])
  429. # y = copy.deepcopy(x)
  430. # # self.assertEqual(x, y)
  431. # self.assertIsNot(x, y)
  432. # self.assertIsNot(x["foo"], y["foo"])
  433. def test_copy_slots(self):
  434. class C:
  435. __slots__ = ["foo"]
  436. x = C()
  437. x.foo = [42]
  438. y = copy.copy(x)
  439. self.assertIs(x.foo, y.foo)
  440. def test_deepcopy_slots(self):
  441. class C(object):
  442. __slots__ = ["foo"]
  443. x = C()
  444. x.foo = [42]
  445. y = copy.deepcopy(x)
  446. self.assertEqual(x.foo, y.foo)
  447. self.assertIsNot(x.foo, y.foo)
  448. # def test_deepcopy_dict_subclass(self):
  449. # class C(dict):
  450. # def __init__(self, d=None):
  451. # if not d:
  452. # d = {}
  453. # self._keys = list(d.keys())
  454. # super().__init__(d)
  455. # def __setitem__(self, key, item):
  456. # super().__setitem__(key, item)
  457. # if key not in self._keys:
  458. # self._keys.append(key)
  459. # x = C(d={'foo':0})
  460. # y = copy.deepcopy(x)
  461. # self.assertEqual(x, y)
  462. # self.assertEqual(x._keys, y._keys)
  463. # self.assertIsNot(x, y)
  464. # x['bar'] = 1
  465. # self.assertNotEqual(x, y)
  466. # self.assertNotEqual(x._keys, y._keys)
  467. # def test_copy_list_subclass(self):
  468. # class C(list):
  469. # pass
  470. # x = C([[1, 2], 3])
  471. # x.foo = [4, 5]
  472. # y = copy.copy(x)
  473. # self.assertEqual(list(x), list(y))
  474. # self.assertEqual(x.foo, y.foo)
  475. # self.assertIs(x[0], y[0])
  476. # self.assertIs(x.foo, y.foo)
  477. # def test_deepcopy_list_subclass(self):
  478. # class C(list):
  479. # pass
  480. # x = C([[1, 2], 3])
  481. # x.foo = [4, 5]
  482. # y = copy.deepcopy(x)
  483. # self.assertEqual(list(x), list(y))
  484. # self.assertEqual(x.foo, y.foo)
  485. # self.assertIsNot(x[0], y[0])
  486. # self.assertIsNot(x.foo, y.foo)
  487. # def test_copy_tuple_subclass(self):
  488. # class C(tuple):
  489. # pass
  490. # x = C([1, 2, 3])
  491. # # self.assertEqual(tus
  492. # def test_deepcopy_tuple_subclass(self):
  493. # class C(tuple):
  494. # pass
  495. # x = C([[1, 2], 3])
  496. # self.assertEqual(tuple(x), ([1, 2], 3))
  497. # y = copy.deepcopy(x)
  498. # self.assertEqual(tuple(y), ([1, 2], 3))
  499. # self.assertIsNot(x, y)
  500. # self.assertIsNot(x[0], y[0])
  501. # def test_getstate_exc(self):
  502. # class EvilState(object):
  503. # def __getstate__(self):
  504. # raise ValueError("ain't got no stickin' state")
  505. # self.assertRaises(ValueError, copy.copy, EvilState())
  506. def test_copy_function(self):
  507. self.assertEqual(copy.copy(global_foo), global_foo)
  508. def foo(x, y): return x+y
  509. self.assertEqual(copy.copy(foo), foo)
  510. bar = lambda: None
  511. self.assertEqual(copy.copy(bar), bar)
  512. def test_deepcopy_function(self):
  513. self.assertEqual(copy.deepcopy(global_foo), global_foo)
  514. def foo(x, y): return x+y
  515. self.assertEqual(copy.deepcopy(foo), foo)
  516. bar = lambda: None
  517. self.assertEqual(copy.deepcopy(bar), bar)
  518. # def _check_weakref(self, _copy):
  519. # class C(object):
  520. # pass
  521. # obj = C()
  522. # x = weakref.ref(obj)
  523. # y = _copy(x)
  524. # self.assertIs(y, x)
  525. # del obj
  526. # y = _copy(x)
  527. # self.assertIs(y, x)
  528. # def test_copy_weakref(self):
  529. # self._check_weakref(copy.copy)
  530. # def test_deepcopy_weakref(self):
  531. # self._check_weakref(copy.deepcopy)
  532. def _check_copy_weakdict(self, _dicttype):
  533. class C(object):
  534. pass
  535. a, b, c, d = [C() for i in range(4)]
  536. u = _dicttype()
  537. u[a] = b
  538. u[c] = d
  539. v = copy.copy(u)
  540. self.assertIsNot(v, u)
  541. self.assertEqual(v, u)
  542. self.assertEqual(v[a], b)
  543. self.assertEqual(v[c], d)
  544. self.assertEqual(len(v), 2)
  545. del c, d
  546. self.assertEqual(len(v), 1)
  547. x, y = C(), C()
  548. # The underlying containers are decoupled
  549. v[x] = y
  550. self.assertNotIn(x, u)
  551. # def test_copy_weakkeydict(self):
  552. # self._check_copy_weakdict(weakref.WeakKeyDictionary)
  553. # def test_copy_weakvaluedict(self):
  554. # self._check_copy_weakdict(weakref.WeakValueDictionary)
  555. # def test_deepcopy_weakkeydict(self):
  556. # class C(object):
  557. # def __init__(self, i):
  558. # self.i = i
  559. # a, b, c, d = [C(i) for i in range(4)]
  560. # u = weakref.WeakKeyDictionary()
  561. # u[a] = b
  562. # u[c] = d
  563. # # Keys aren't copied, values are
  564. # v = copy.deepcopy(u)
  565. # self.assertNotEqual(v, u)
  566. # self.assertEqual(len(v), 2)
  567. # self.assertIsNot(v[a], b)
  568. # self.assertIsNot(v[c], d)
  569. # self.assertEqual(v[a].i, b.i)
  570. # self.assertEqual(v[c].i, d.i)
  571. # del c
  572. # self.assertEqual(len(v), 1)
  573. # def test_deepcopy_weakvaluedict(self):
  574. # class C(object):
  575. # def __init__(self, i):
  576. # self.i = i
  577. # a, b, c, d = [C(i) for i in range(4)]
  578. # u = weakref.WeakValueDictionary()
  579. # u[a] = b
  580. # u[c] = d
  581. # # Keys are copied, values aren't
  582. # v = copy.deepcopy(u)
  583. # self.assertNotEqual(v, u)
  584. # self.assertEqual(len(v), 2)
  585. # (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i)
  586. # self.assertIsNot(x, a)
  587. # self.assertEqual(x.i, a.i)
  588. # self.assertIs(y, b)
  589. # self.assertIsNot(z, c)
  590. # self.assertEqual(z.i, c.i)
  591. # self.assertIs(t, d)
  592. # del x, y, z, t
  593. # del d
  594. # self.assertEqual(len(v), 1)
  595. def test_deepcopy_methods(self):
  596. class potato:
  597. def cut(self):
  598. print "slices"
  599. mashed = potato()
  600. d1 = {"a": mashed.cut}
  601. d2 = copy.deepcopy(d1)
  602. # self.assertFalse(d1 == d2)
  603. # self.assertFalse(d1 is d2)
  604. # im = mashed.cut
  605. # im_dc = copy.deepcopy(im)
  606. # self.assertTrue(isinstance, (im, types.MethodType))
  607. # self.assertFalse(im == im_dc)
  608. # self.assertFalse(im is im_dc)
  609. def global_foo(x, y): return x+y
  610. if __name__ == "__main__":
  611. unittest.main()