PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/IronLanguages/main
Python | 764 lines | 738 code | 19 blank | 7 comment | 2 complexity | ec61d57e5bd67ff4e9c78fe0a2b3b59c MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. """Unit tests for the copy module."""
  2. import copy
  3. import copy_reg
  4. import weakref
  5. import unittest
  6. from test import test_support
  7. class TestCopy(unittest.TestCase):
  8. # Attempt full line coverage of copy.py from top to bottom
  9. def test_exceptions(self):
  10. self.assertTrue(copy.Error is copy.error)
  11. self.assertTrue(issubclass(copy.Error, Exception))
  12. # The copy() method
  13. def test_copy_basic(self):
  14. x = 42
  15. y = copy.copy(x)
  16. self.assertEqual(x, y)
  17. def test_copy_copy(self):
  18. class C(object):
  19. def __init__(self, foo):
  20. self.foo = foo
  21. def __copy__(self):
  22. return C(self.foo)
  23. x = C(42)
  24. y = copy.copy(x)
  25. self.assertEqual(y.__class__, x.__class__)
  26. self.assertEqual(y.foo, x.foo)
  27. def test_copy_registry(self):
  28. class C(object):
  29. def __new__(cls, foo):
  30. obj = object.__new__(cls)
  31. obj.foo = foo
  32. return obj
  33. def pickle_C(obj):
  34. return (C, (obj.foo,))
  35. x = C(42)
  36. self.assertRaises(TypeError, copy.copy, x)
  37. copy_reg.pickle(C, pickle_C, C)
  38. y = copy.copy(x)
  39. def test_copy_reduce_ex(self):
  40. class C(object):
  41. def __reduce_ex__(self, proto):
  42. return ""
  43. def __reduce__(self):
  44. raise test_support.TestFailed, "shouldn't call this"
  45. x = C()
  46. y = copy.copy(x)
  47. self.assertTrue(y is x)
  48. def test_copy_reduce(self):
  49. class C(object):
  50. def __reduce__(self):
  51. return ""
  52. x = C()
  53. y = copy.copy(x)
  54. self.assertTrue(y is x)
  55. def test_copy_cant(self):
  56. class C(object):
  57. def __getattribute__(self, name):
  58. if name.startswith("__reduce"):
  59. raise AttributeError, name
  60. return object.__getattribute__(self, name)
  61. x = C()
  62. self.assertRaises(copy.Error, copy.copy, x)
  63. # Type-specific _copy_xxx() methods
  64. def test_copy_atomic(self):
  65. class Classic:
  66. pass
  67. class NewStyle(object):
  68. pass
  69. def f():
  70. pass
  71. tests = [None, Ellipsis,
  72. 42, 2L**100, 3.14, True, False, 1j,
  73. "hello", u"hello\u1234", f.func_code,
  74. NewStyle, xrange(10), Classic, max]
  75. for x in tests:
  76. self.assertTrue(copy.copy(x) is x, repr(x))
  77. def test_copy_list(self):
  78. x = [1, 2, 3]
  79. y = copy.copy(x)
  80. self.assertEqual(y, x)
  81. self.assertIsNot(y, x)
  82. x = []
  83. y = copy.copy(x)
  84. self.assertEqual(y, x)
  85. self.assertIsNot(y, x)
  86. def test_copy_tuple(self):
  87. x = (1, 2, 3)
  88. self.assertIs(copy.copy(x), x)
  89. x = ()
  90. self.assertIs(copy.copy(x), x)
  91. x = (1, 2, 3, [])
  92. self.assertIs(copy.copy(x), x)
  93. def test_copy_dict(self):
  94. x = {"foo": 1, "bar": 2}
  95. y = copy.copy(x)
  96. self.assertEqual(y, x)
  97. self.assertIsNot(y, x)
  98. x = {}
  99. y = copy.copy(x)
  100. self.assertEqual(y, x)
  101. self.assertIsNot(y, x)
  102. def test_copy_set(self):
  103. x = {1, 2, 3}
  104. y = copy.copy(x)
  105. self.assertEqual(y, x)
  106. self.assertIsNot(y, x)
  107. x = set()
  108. y = copy.copy(x)
  109. self.assertEqual(y, x)
  110. self.assertIsNot(y, x)
  111. def test_copy_frozenset(self):
  112. x = frozenset({1, 2, 3})
  113. self.assertIs(copy.copy(x), x)
  114. x = frozenset()
  115. self.assertIs(copy.copy(x), x)
  116. def test_copy_bytearray(self):
  117. x = bytearray(b'abc')
  118. y = copy.copy(x)
  119. self.assertEqual(y, x)
  120. self.assertIsNot(y, x)
  121. x = bytearray()
  122. y = copy.copy(x)
  123. self.assertEqual(y, x)
  124. self.assertIsNot(y, x)
  125. def test_copy_inst_vanilla(self):
  126. class C:
  127. def __init__(self, foo):
  128. self.foo = foo
  129. def __cmp__(self, other):
  130. return cmp(self.foo, other.foo)
  131. x = C(42)
  132. self.assertEqual(copy.copy(x), x)
  133. def test_copy_inst_copy(self):
  134. class C:
  135. def __init__(self, foo):
  136. self.foo = foo
  137. def __copy__(self):
  138. return C(self.foo)
  139. def __cmp__(self, other):
  140. return cmp(self.foo, other.foo)
  141. x = C(42)
  142. self.assertEqual(copy.copy(x), x)
  143. def test_copy_inst_getinitargs(self):
  144. class C:
  145. def __init__(self, foo):
  146. self.foo = foo
  147. def __getinitargs__(self):
  148. return (self.foo,)
  149. def __cmp__(self, other):
  150. return cmp(self.foo, other.foo)
  151. x = C(42)
  152. self.assertEqual(copy.copy(x), x)
  153. def test_copy_inst_getstate(self):
  154. class C:
  155. def __init__(self, foo):
  156. self.foo = foo
  157. def __getstate__(self):
  158. return {"foo": self.foo}
  159. def __cmp__(self, other):
  160. return cmp(self.foo, other.foo)
  161. x = C(42)
  162. self.assertEqual(copy.copy(x), x)
  163. def test_copy_inst_setstate(self):
  164. class C:
  165. def __init__(self, foo):
  166. self.foo = foo
  167. def __setstate__(self, state):
  168. self.foo = state["foo"]
  169. def __cmp__(self, other):
  170. return cmp(self.foo, other.foo)
  171. x = C(42)
  172. self.assertEqual(copy.copy(x), x)
  173. def test_copy_inst_getstate_setstate(self):
  174. class C:
  175. def __init__(self, foo):
  176. self.foo = foo
  177. def __getstate__(self):
  178. return self.foo
  179. def __setstate__(self, state):
  180. self.foo = state
  181. def __cmp__(self, other):
  182. return cmp(self.foo, other.foo)
  183. x = C(42)
  184. self.assertEqual(copy.copy(x), x)
  185. # State with boolean value is false (issue #25718)
  186. x = C(0.0)
  187. self.assertEqual(copy.copy(x), x)
  188. # The deepcopy() method
  189. def test_deepcopy_basic(self):
  190. x = 42
  191. y = copy.deepcopy(x)
  192. self.assertEqual(y, x)
  193. def test_deepcopy_memo(self):
  194. # Tests of reflexive objects are under type-specific sections below.
  195. # This tests only repetitions of objects.
  196. x = []
  197. x = [x, x]
  198. y = copy.deepcopy(x)
  199. self.assertEqual(y, x)
  200. self.assertTrue(y is not x)
  201. self.assertTrue(y[0] is not x[0])
  202. self.assertTrue(y[0] is y[1])
  203. def test_deepcopy_issubclass(self):
  204. # XXX Note: there's no way to test the TypeError coming out of
  205. # issubclass() -- this can only happen when an extension
  206. # module defines a "type" that doesn't formally inherit from
  207. # type.
  208. class Meta(type):
  209. pass
  210. class C:
  211. __metaclass__ = Meta
  212. self.assertEqual(copy.deepcopy(C), C)
  213. def test_deepcopy_deepcopy(self):
  214. class C(object):
  215. def __init__(self, foo):
  216. self.foo = foo
  217. def __deepcopy__(self, memo=None):
  218. return C(self.foo)
  219. x = C(42)
  220. y = copy.deepcopy(x)
  221. self.assertEqual(y.__class__, x.__class__)
  222. self.assertEqual(y.foo, x.foo)
  223. def test_deepcopy_registry(self):
  224. class C(object):
  225. def __new__(cls, foo):
  226. obj = object.__new__(cls)
  227. obj.foo = foo
  228. return obj
  229. def pickle_C(obj):
  230. return (C, (obj.foo,))
  231. x = C(42)
  232. self.assertRaises(TypeError, copy.deepcopy, x)
  233. copy_reg.pickle(C, pickle_C, C)
  234. y = copy.deepcopy(x)
  235. def test_deepcopy_reduce_ex(self):
  236. class C(object):
  237. def __reduce_ex__(self, proto):
  238. return ""
  239. def __reduce__(self):
  240. raise test_support.TestFailed, "shouldn't call this"
  241. x = C()
  242. y = copy.deepcopy(x)
  243. self.assertTrue(y is x)
  244. def test_deepcopy_reduce(self):
  245. class C(object):
  246. def __reduce__(self):
  247. return ""
  248. x = C()
  249. y = copy.deepcopy(x)
  250. self.assertTrue(y is x)
  251. def test_deepcopy_cant(self):
  252. class C(object):
  253. def __getattribute__(self, name):
  254. if name.startswith("__reduce"):
  255. raise AttributeError, name
  256. return object.__getattribute__(self, name)
  257. x = C()
  258. self.assertRaises(copy.Error, copy.deepcopy, x)
  259. # Type-specific _deepcopy_xxx() methods
  260. def test_deepcopy_atomic(self):
  261. class Classic:
  262. pass
  263. class NewStyle(object):
  264. pass
  265. def f():
  266. pass
  267. tests = [None, 42, 2L**100, 3.14, True, False, 1j,
  268. "hello", u"hello\u1234", f.func_code,
  269. NewStyle, xrange(10), Classic, max]
  270. for x in tests:
  271. self.assertTrue(copy.deepcopy(x) is x, repr(x))
  272. def test_deepcopy_list(self):
  273. x = [[1, 2], 3]
  274. y = copy.deepcopy(x)
  275. self.assertEqual(y, x)
  276. self.assertTrue(x is not y)
  277. self.assertTrue(x[0] is not y[0])
  278. def test_deepcopy_reflexive_list(self):
  279. x = []
  280. x.append(x)
  281. y = copy.deepcopy(x)
  282. self.assertRaises(RuntimeError, cmp, y, x)
  283. self.assertTrue(y is not x)
  284. self.assertTrue(y[0] is y)
  285. self.assertEqual(len(y), 1)
  286. def test_deepcopy_tuple(self):
  287. x = ([1, 2], 3)
  288. y = copy.deepcopy(x)
  289. self.assertEqual(y, x)
  290. self.assertTrue(x is not y)
  291. self.assertTrue(x[0] is not y[0])
  292. def test_deepcopy_reflexive_tuple(self):
  293. x = ([],)
  294. x[0].append(x)
  295. y = copy.deepcopy(x)
  296. self.assertRaises(RuntimeError, cmp, y, x)
  297. self.assertTrue(y is not x)
  298. self.assertTrue(y[0] is not x[0])
  299. self.assertTrue(y[0][0] is y)
  300. def test_deepcopy_dict(self):
  301. x = {"foo": [1, 2], "bar": 3}
  302. y = copy.deepcopy(x)
  303. self.assertEqual(y, x)
  304. self.assertTrue(x is not y)
  305. self.assertTrue(x["foo"] is not y["foo"])
  306. def test_deepcopy_reflexive_dict(self):
  307. x = {}
  308. x['foo'] = x
  309. y = copy.deepcopy(x)
  310. self.assertRaises(RuntimeError, cmp, y, x)
  311. self.assertTrue(y is not x)
  312. self.assertTrue(y['foo'] is y)
  313. self.assertEqual(len(y), 1)
  314. def test_deepcopy_keepalive(self):
  315. memo = {}
  316. x = 42
  317. y = copy.deepcopy(x, memo)
  318. self.assertTrue(memo[id(x)] is x)
  319. def test_deepcopy_inst_vanilla(self):
  320. class C:
  321. def __init__(self, foo):
  322. self.foo = foo
  323. def __cmp__(self, other):
  324. return cmp(self.foo, other.foo)
  325. x = C([42])
  326. y = copy.deepcopy(x)
  327. self.assertEqual(y, x)
  328. self.assertTrue(y.foo is not x.foo)
  329. def test_deepcopy_inst_deepcopy(self):
  330. class C:
  331. def __init__(self, foo):
  332. self.foo = foo
  333. def __deepcopy__(self, memo):
  334. return C(copy.deepcopy(self.foo, memo))
  335. def __cmp__(self, other):
  336. return cmp(self.foo, other.foo)
  337. x = C([42])
  338. y = copy.deepcopy(x)
  339. self.assertEqual(y, x)
  340. self.assertTrue(y is not x)
  341. self.assertTrue(y.foo is not x.foo)
  342. def test_deepcopy_inst_getinitargs(self):
  343. class C:
  344. def __init__(self, foo):
  345. self.foo = foo
  346. def __getinitargs__(self):
  347. return (self.foo,)
  348. def __cmp__(self, other):
  349. return cmp(self.foo, other.foo)
  350. x = C([42])
  351. y = copy.deepcopy(x)
  352. self.assertEqual(y, x)
  353. self.assertTrue(y is not x)
  354. self.assertTrue(y.foo is not x.foo)
  355. def test_deepcopy_inst_getstate(self):
  356. class C:
  357. def __init__(self, foo):
  358. self.foo = foo
  359. def __getstate__(self):
  360. return {"foo": self.foo}
  361. def __cmp__(self, other):
  362. return cmp(self.foo, other.foo)
  363. x = C([42])
  364. y = copy.deepcopy(x)
  365. self.assertEqual(y, x)
  366. self.assertTrue(y is not x)
  367. self.assertTrue(y.foo is not x.foo)
  368. def test_deepcopy_inst_setstate(self):
  369. class C:
  370. def __init__(self, foo):
  371. self.foo = foo
  372. def __setstate__(self, state):
  373. self.foo = state["foo"]
  374. def __cmp__(self, other):
  375. return cmp(self.foo, other.foo)
  376. x = C([42])
  377. y = copy.deepcopy(x)
  378. self.assertEqual(y, x)
  379. self.assertTrue(y is not x)
  380. self.assertTrue(y.foo is not x.foo)
  381. def test_deepcopy_inst_getstate_setstate(self):
  382. class C:
  383. def __init__(self, foo):
  384. self.foo = foo
  385. def __getstate__(self):
  386. return self.foo
  387. def __setstate__(self, state):
  388. self.foo = state
  389. def __cmp__(self, other):
  390. return cmp(self.foo, other.foo)
  391. x = C([42])
  392. y = copy.deepcopy(x)
  393. self.assertEqual(y, x)
  394. self.assertIsNot(y, x)
  395. self.assertIsNot(y.foo, x.foo)
  396. # State with boolean value is false (issue #25718)
  397. x = C([])
  398. y = copy.deepcopy(x)
  399. self.assertEqual(y, x)
  400. self.assertTrue(y is not x)
  401. self.assertTrue(y.foo is not x.foo)
  402. def test_deepcopy_reflexive_inst(self):
  403. class C:
  404. pass
  405. x = C()
  406. x.foo = x
  407. y = copy.deepcopy(x)
  408. self.assertTrue(y is not x)
  409. self.assertTrue(y.foo is y)
  410. # _reconstruct()
  411. def test_reconstruct_string(self):
  412. class C(object):
  413. def __reduce__(self):
  414. return ""
  415. x = C()
  416. y = copy.copy(x)
  417. self.assertTrue(y is x)
  418. y = copy.deepcopy(x)
  419. self.assertTrue(y is x)
  420. def test_reconstruct_nostate(self):
  421. class C(object):
  422. def __reduce__(self):
  423. return (C, ())
  424. x = C()
  425. x.foo = 42
  426. y = copy.copy(x)
  427. self.assertTrue(y.__class__ is x.__class__)
  428. y = copy.deepcopy(x)
  429. self.assertTrue(y.__class__ is x.__class__)
  430. def test_reconstruct_state(self):
  431. class C(object):
  432. def __reduce__(self):
  433. return (C, (), self.__dict__)
  434. def __cmp__(self, other):
  435. return cmp(self.__dict__, other.__dict__)
  436. __hash__ = None # Silence Py3k warning
  437. x = C()
  438. x.foo = [42]
  439. y = copy.copy(x)
  440. self.assertEqual(y, x)
  441. y = copy.deepcopy(x)
  442. self.assertEqual(y, x)
  443. self.assertTrue(y.foo is not x.foo)
  444. def test_reconstruct_state_setstate(self):
  445. class C(object):
  446. def __reduce__(self):
  447. return (C, (), self.__dict__)
  448. def __setstate__(self, state):
  449. self.__dict__.update(state)
  450. def __cmp__(self, other):
  451. return cmp(self.__dict__, other.__dict__)
  452. __hash__ = None # Silence Py3k warning
  453. x = C()
  454. x.foo = [42]
  455. y = copy.copy(x)
  456. self.assertEqual(y, x)
  457. y = copy.deepcopy(x)
  458. self.assertEqual(y, x)
  459. self.assertTrue(y.foo is not x.foo)
  460. def test_reconstruct_reflexive(self):
  461. class C(object):
  462. pass
  463. x = C()
  464. x.foo = x
  465. y = copy.deepcopy(x)
  466. self.assertTrue(y is not x)
  467. self.assertTrue(y.foo is y)
  468. # Additions for Python 2.3 and pickle protocol 2
  469. def test_reduce_4tuple(self):
  470. class C(list):
  471. def __reduce__(self):
  472. return (C, (), self.__dict__, iter(self))
  473. def __cmp__(self, other):
  474. return (cmp(list(self), list(other)) or
  475. cmp(self.__dict__, other.__dict__))
  476. __hash__ = None # Silence Py3k warning
  477. x = C([[1, 2], 3])
  478. y = copy.copy(x)
  479. self.assertEqual(x, y)
  480. self.assertTrue(x is not y)
  481. self.assertTrue(x[0] is y[0])
  482. y = copy.deepcopy(x)
  483. self.assertEqual(x, y)
  484. self.assertTrue(x is not y)
  485. self.assertTrue(x[0] is not y[0])
  486. def test_reduce_5tuple(self):
  487. class C(dict):
  488. def __reduce__(self):
  489. return (C, (), self.__dict__, None, self.iteritems())
  490. def __cmp__(self, other):
  491. return (cmp(dict(self), list(dict)) or
  492. cmp(self.__dict__, other.__dict__))
  493. __hash__ = None # Silence Py3k warning
  494. x = C([("foo", [1, 2]), ("bar", 3)])
  495. y = copy.copy(x)
  496. self.assertEqual(x, y)
  497. self.assertTrue(x is not y)
  498. self.assertTrue(x["foo"] is y["foo"])
  499. y = copy.deepcopy(x)
  500. self.assertEqual(x, y)
  501. self.assertTrue(x is not y)
  502. self.assertTrue(x["foo"] is not y["foo"])
  503. def test_copy_slots(self):
  504. class C(object):
  505. __slots__ = ["foo"]
  506. x = C()
  507. x.foo = [42]
  508. y = copy.copy(x)
  509. self.assertTrue(x.foo is y.foo)
  510. def test_deepcopy_slots(self):
  511. class C(object):
  512. __slots__ = ["foo"]
  513. x = C()
  514. x.foo = [42]
  515. y = copy.deepcopy(x)
  516. self.assertEqual(x.foo, y.foo)
  517. self.assertTrue(x.foo is not y.foo)
  518. def test_deepcopy_dict_subclass(self):
  519. class C(dict):
  520. def __init__(self, d=None):
  521. if not d:
  522. d = {}
  523. self._keys = list(d.keys())
  524. dict.__init__(self, d)
  525. def __setitem__(self, key, item):
  526. dict.__setitem__(self, key, item)
  527. if key not in self._keys:
  528. self._keys.append(key)
  529. x = C(d={'foo':0})
  530. y = copy.deepcopy(x)
  531. self.assertEqual(x, y)
  532. self.assertEqual(x._keys, y._keys)
  533. self.assertTrue(x is not y)
  534. x['bar'] = 1
  535. self.assertNotEqual(x, y)
  536. self.assertNotEqual(x._keys, y._keys)
  537. def test_copy_list_subclass(self):
  538. class C(list):
  539. pass
  540. x = C([[1, 2], 3])
  541. x.foo = [4, 5]
  542. y = copy.copy(x)
  543. self.assertEqual(list(x), list(y))
  544. self.assertEqual(x.foo, y.foo)
  545. self.assertTrue(x[0] is y[0])
  546. self.assertTrue(x.foo is y.foo)
  547. def test_deepcopy_list_subclass(self):
  548. class C(list):
  549. pass
  550. x = C([[1, 2], 3])
  551. x.foo = [4, 5]
  552. y = copy.deepcopy(x)
  553. self.assertEqual(list(x), list(y))
  554. self.assertEqual(x.foo, y.foo)
  555. self.assertTrue(x[0] is not y[0])
  556. self.assertTrue(x.foo is not y.foo)
  557. def test_copy_tuple_subclass(self):
  558. class C(tuple):
  559. pass
  560. x = C([1, 2, 3])
  561. self.assertEqual(tuple(x), (1, 2, 3))
  562. y = copy.copy(x)
  563. self.assertEqual(tuple(y), (1, 2, 3))
  564. def test_deepcopy_tuple_subclass(self):
  565. class C(tuple):
  566. pass
  567. x = C([[1, 2], 3])
  568. self.assertEqual(tuple(x), ([1, 2], 3))
  569. y = copy.deepcopy(x)
  570. self.assertEqual(tuple(y), ([1, 2], 3))
  571. self.assertTrue(x is not y)
  572. self.assertTrue(x[0] is not y[0])
  573. def test_getstate_exc(self):
  574. class EvilState(object):
  575. def __getstate__(self):
  576. raise ValueError, "ain't got no stickin' state"
  577. self.assertRaises(ValueError, copy.copy, EvilState())
  578. def test_copy_function(self):
  579. self.assertEqual(copy.copy(global_foo), global_foo)
  580. def foo(x, y): return x+y
  581. self.assertEqual(copy.copy(foo), foo)
  582. bar = lambda: None
  583. self.assertEqual(copy.copy(bar), bar)
  584. def test_deepcopy_function(self):
  585. self.assertEqual(copy.deepcopy(global_foo), global_foo)
  586. def foo(x, y): return x+y
  587. self.assertEqual(copy.deepcopy(foo), foo)
  588. bar = lambda: None
  589. self.assertEqual(copy.deepcopy(bar), bar)
  590. def _check_weakref(self, _copy):
  591. class C(object):
  592. pass
  593. obj = C()
  594. x = weakref.ref(obj)
  595. y = _copy(x)
  596. self.assertTrue(y is x)
  597. del obj
  598. y = _copy(x)
  599. self.assertTrue(y is x)
  600. def test_copy_weakref(self):
  601. self._check_weakref(copy.copy)
  602. def test_deepcopy_weakref(self):
  603. self._check_weakref(copy.deepcopy)
  604. def _check_copy_weakdict(self, _dicttype):
  605. class C(object):
  606. pass
  607. a, b, c, d = [C() for i in xrange(4)]
  608. u = _dicttype()
  609. u[a] = b
  610. u[c] = d
  611. v = copy.copy(u)
  612. self.assertFalse(v is u)
  613. self.assertEqual(v, u)
  614. self.assertEqual(v[a], b)
  615. self.assertEqual(v[c], d)
  616. self.assertEqual(len(v), 2)
  617. del c, d
  618. self.assertEqual(len(v), 1)
  619. x, y = C(), C()
  620. # The underlying containers are decoupled
  621. v[x] = y
  622. self.assertNotIn(x, u)
  623. def test_copy_weakkeydict(self):
  624. self._check_copy_weakdict(weakref.WeakKeyDictionary)
  625. def test_copy_weakvaluedict(self):
  626. self._check_copy_weakdict(weakref.WeakValueDictionary)
  627. def test_deepcopy_weakkeydict(self):
  628. class C(object):
  629. def __init__(self, i):
  630. self.i = i
  631. a, b, c, d = [C(i) for i in xrange(4)]
  632. u = weakref.WeakKeyDictionary()
  633. u[a] = b
  634. u[c] = d
  635. # Keys aren't copied, values are
  636. v = copy.deepcopy(u)
  637. self.assertNotEqual(v, u)
  638. self.assertEqual(len(v), 2)
  639. self.assertFalse(v[a] is b)
  640. self.assertFalse(v[c] is d)
  641. self.assertEqual(v[a].i, b.i)
  642. self.assertEqual(v[c].i, d.i)
  643. del c
  644. self.assertEqual(len(v), 1)
  645. def test_deepcopy_weakvaluedict(self):
  646. class C(object):
  647. def __init__(self, i):
  648. self.i = i
  649. a, b, c, d = [C(i) for i in xrange(4)]
  650. u = weakref.WeakValueDictionary()
  651. u[a] = b
  652. u[c] = d
  653. # Keys are copied, values aren't
  654. v = copy.deepcopy(u)
  655. self.assertNotEqual(v, u)
  656. self.assertEqual(len(v), 2)
  657. (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i)
  658. self.assertFalse(x is a)
  659. self.assertEqual(x.i, a.i)
  660. self.assertTrue(y is b)
  661. self.assertFalse(z is c)
  662. self.assertEqual(z.i, c.i)
  663. self.assertTrue(t is d)
  664. del x, y, z, t
  665. del d
  666. self.assertEqual(len(v), 1)
  667. def test_deepcopy_bound_method(self):
  668. class Foo(object):
  669. def m(self):
  670. pass
  671. f = Foo()
  672. f.b = f.m
  673. g = copy.deepcopy(f)
  674. self.assertEqual(g.m, g.b)
  675. self.assertTrue(g.b.im_self is g)
  676. g.b()
  677. def global_foo(x, y): return x+y
  678. def test_main():
  679. test_support.run_unittest(TestCopy)
  680. if __name__ == "__main__":
  681. test_main()