PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/modified-2.7/test/test_copy.py

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