test_copy.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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, unittest
  7. from operator import le, lt, ge, gt, eq, ne
  8. class C:
  9. def __init__(self, foo):
  10. self.foo = foo
  11. def __copy__(self):
  12. return C(self.foo)
  13. class ReduceEx(object):
  14. def __reduce_ex__(self, proto):
  15. c.append(1)
  16. return ""
  17. def __reduce__(self):
  18. self.fail("shouldn't call this")
  19. c = []
  20. class Reduce(object):
  21. def __reduce__(self):
  22. d.append(1)
  23. return ""
  24. d = []
  25. class InstVan:
  26. def __init__(self, foo):
  27. self.foo = foo
  28. # def __eq__(self, other):
  29. # return self.foo == other.foo
  30. class Copy_eq:
  31. def __init__(self, foo):
  32. self.foo = foo
  33. def __copy__(self):
  34. return C(self.foo)
  35. def __eq__(self, other):
  36. return self.foo == other.foo
  37. class User_def:
  38. def __init__(self, name):
  39. self.name = name
  40. def rename(self,newname):
  41. self.name = newname
  42. class NewArgs(int):
  43. def __new__(cls, foo):
  44. self = int.__new__(cls)
  45. self.foo = foo
  46. return self
  47. def __getnewargs__(self):
  48. return self.foo,
  49. def __eq__(self, other):
  50. return self.foo == other.foo
  51. class TestCopy(unittest.TestCase):
  52. def test_exceptions(self):
  53. self.assertIs(copy.Error, copy.error)
  54. self.assertTrue(issubclass(copy.Error, Exception))
  55. # The copy() method
  56. def test_copy_basic(self):
  57. x = 42
  58. y = copy.copy(x)
  59. self.assertEqual(x, y)
  60. def test_copy_copy(self):
  61. x = C(42)
  62. y = copy.copy(x)
  63. self.assertEqual(y.__class__, x.__class__)
  64. self.assertEqual(y.foo, x.foo)
  65. def test_copy_inst(self):
  66. x = User_def("One")
  67. y = copy.copy(x)
  68. self.assertEqual(x.name, "One")
  69. self.assertFalse(x==y)
  70. self.assertFalse(x is y)
  71. self.assertTrue(x.name is y.name)
  72. x.rename("Two")
  73. self.assertFalse(x==y)
  74. self.assertFalse(x is y)
  75. self.assertEqual(x.name, "Two")
  76. self.assertEqual(y.name, "One")
  77. def test_copy_cant(self):
  78. class Cant:
  79. def __getattribute__(self, name):
  80. pass
  81. # if name.startswith("__reduce"):
  82. # raise AttributeError(name)
  83. # return object.__getattribute__(self, name)
  84. x = Cant()
  85. self.assertRaises(TypeError, copy.copy, x)
  86. def test_copy_atomic(self):
  87. class Classic:
  88. pass
  89. def f():
  90. pass
  91. # class WithMetaclass(metaclass=abc.ABCMeta):
  92. # pass
  93. tests = [None, 42, 3.14, True, False,
  94. "hello", "hello\u1234", Classic]
  95. #None, .../, NotImplemented,
  96. # 42, 2**100, 3.14, True, False, 1j,
  97. # "hello", "hello\u1234", f.__code__,
  98. # b"world", bytes(range(256)), slice(1, 10, 2),
  99. # NewStyle, Classic, max, WithMetaclass]
  100. for x in tests:
  101. self.assertIs(copy.copy(x), x)
  102. def test_copy_list(self):
  103. x = [1, 2, 3]
  104. y = copy.copy(x)
  105. self.assertEqual(y, x)
  106. self.assertIsNot(y, x)
  107. x = []
  108. y = copy.copy(x)
  109. self.assertEqual(y, x)
  110. self.assertIsNot(y, x)
  111. def test_copy_mixed_set(self):
  112. class FooSet:
  113. def bar(self):
  114. pass
  115. a = set([1,2,3])
  116. b = copy.deepcopy(a)
  117. a = set([1,2,3])
  118. b = copy.deepcopy(a)
  119. self.assertTrue(a == b)
  120. self.assertFalse(a is b)
  121. mixed = set([(1,2), FooSet.bar])
  122. mixedcopy = copy.deepcopy(mixed)
  123. self.assertTrue(mixed == mixedcopy)
  124. self.assertFalse(mixed is mixedcopy)
  125. mixed.add(9)
  126. mixedcopy2 = copy.deepcopy(mixed)
  127. self.assertTrue(mixed == mixedcopy2)
  128. self.assertFalse(mixed is mixedcopy2)
  129. mixed.add(9)
  130. self.assertFalse(mixed == mixedcopy)
  131. mixedcopy2 = copy.copy(mixed)
  132. self.assertTrue(mixed == mixedcopy2)
  133. def test_copy_tuple(self):
  134. x = (1, 2, 3)
  135. self.assertIs(copy.copy(x), x)
  136. x = ()
  137. self.assertIs(copy.copy(x), x)
  138. x = (1, 2, 3, [])
  139. self.assertIs(copy.copy(x), x)
  140. # def test_copy_registry(self):
  141. # class C:
  142. # def __new__(cls, foo):
  143. # obj = object.__new__(cls)
  144. # obj.foo = foo
  145. # return obj
  146. # def pickle_C(obj):
  147. # return (C, (obj.foo,))
  148. # x = C(42)
  149. # self.assertRaises(TypeError, copy.copy, x)
  150. # copyreg.pickle(C, pickle_C, C)
  151. # y = copy.copy(x)
  152. def test_copy_reduce_ex(self):
  153. x = ReduceEx()
  154. y = copy.copy(x)
  155. self.assertIs(y, x)
  156. self.assertEqual(c, [1])
  157. def test_copy_reduce(self):
  158. x = Reduce()
  159. y = copy.copy(x)
  160. self.assertIs(y, x)
  161. self.assertEqual(d, [1])
  162. def test_copy_list(self):
  163. x = [1, 2, 3]
  164. y = copy.copy(x)
  165. self.assertEqual(y, x)
  166. self.assertIsNot(y, x)
  167. x = []
  168. y = copy.copy(x)
  169. self.assertEqual(y, x)
  170. self.assertIsNot(y, x)
  171. def test_copy_tuple(self):
  172. x = (1, 2, 3)
  173. self.assertIs(copy.copy(x), x)
  174. x = ()
  175. self.assertIs(copy.copy(x), x)
  176. x = (1, 2, 3, [])
  177. self.assertIs(copy.copy(x), x)
  178. def test_copy_dict(self):
  179. x = {"foo": 1, "bar": 2}
  180. y = copy.copy(x)
  181. self.assertEqual(y, x)
  182. self.assertIsNot(y, x)
  183. x = {}
  184. y = copy.copy(x)
  185. self.assertEqual(y, x)
  186. self.assertIsNot(y, x)
  187. def test_copy_set(self):
  188. x = {1, 2, 3}
  189. y = copy.copy(x)
  190. self.assertEqual(y, x)
  191. self.assertIsNot(y, x)
  192. x = set()
  193. y = copy.copy(x)
  194. self.assertEqual(y, x)
  195. self.assertIsNot(y, x)
  196. # # def test_copy_frozenset(self):
  197. # # x = frozenset({1, 2, 3})
  198. # # self.assertIs(copy.copy(x), x)
  199. # # x = frozenset()
  200. # # self.assertIs(copy.copy(x), x)
  201. # # def test_copy_bytearray(self):
  202. # # x = bytearray(b'abc')
  203. # # y = copy.copy(x)
  204. # # self.assertEqual(y, x)
  205. # # self.assertIsNot(y, x)
  206. # # x = bytearray()
  207. # # y = copy.copy(x)
  208. # # self.assertEqual(y, x)
  209. # # self.assertIsNot(y, x)
  210. def test_copy_inst_vanilla(self):
  211. x = InstVan(42)
  212. # print x == copy.copy(x)
  213. self.assertFalse(copy.copy(x) == x)
  214. def test_copy_inst_copy(self):
  215. x = Copy_eq(42)
  216. self.assertEqual(copy.copy(x), x)
  217. def test_copy_inst_getinitargs(self):
  218. class C:
  219. def __init__(self, foo):
  220. self.foo = foo
  221. def __getinitargs__(self):
  222. return (self.foo,)
  223. def __eq__(self, other):
  224. return self.foo == other.foo
  225. x = C(42)
  226. self.assertRaises(NotImplementedError, copy.copy, x)
  227. # self.assertEqual(copy.copy(x), x)
  228. # def test_copy_inst_getnewargs(self):
  229. # # class NewArgs(int):
  230. # # def __new__(cls, foo):
  231. # # self = int.__new__(cls)
  232. # # self.foo = foo
  233. # # return self
  234. # # def __getnewargs__(self):
  235. # # return self.foo,
  236. # # def __eq__(self, other):
  237. # # return self.foo == other.foo
  238. # x = NewArgs(42)
  239. # y = copy.copy(x)
  240. # self.assertIsInstance(y, NewArgs)
  241. # self.assertEqual(y, x)
  242. # self.assertIsNot(y, x)
  243. # self.assertEqual(y.foo, x.foo)
  244. # def test_copy_inst_getnewargs_ex(self):
  245. # class C(int):
  246. # def __new__(cls, *, foo):
  247. # self = int.__new__(cls)
  248. # self.foo = foo
  249. # return self
  250. # def __getnewargs_ex__(self):
  251. # return (), {'foo': self.foo}
  252. # def __eq__(self, other):
  253. # return self.foo == other.foo
  254. # x = C(foo=42)
  255. # y = copy.copy(x)
  256. # self.assertIsInstance(y, C)
  257. # self.assertEqual(y, x)
  258. # self.assertIsNot(y, x)
  259. # self.assertEqual(y.foo, x.foo)
  260. def test_copy_inst_getstate(self):
  261. class C:
  262. def __init__(self, foo):
  263. self.foo = foo
  264. def __getstate__(self):
  265. return {"foo": self.foo}
  266. def __eq__(self, other):
  267. return self.foo == other.foo
  268. x = C(42)
  269. self.assertRaises(NotImplementedError, copy.copy, x)
  270. # self.assertEqual(copy.copy(x), x)
  271. def test_copy_inst_setstate(self):
  272. class C:
  273. def __init__(self, foo):
  274. self.foo = foo
  275. def __setstate__(self, state):
  276. self.foo = state["foo"]
  277. def __eq__(self, other):
  278. return self.foo == other.foo
  279. x = C(42)
  280. self.assertRaises(NotImplementedError, copy.copy, x)
  281. # self.assertEqual(copy.copy(x), x)
  282. def test_copy_inst_getstate_setstate(self):
  283. class C:
  284. def __init__(self, foo):
  285. self.foo = foo
  286. def __getstate__(self):
  287. return self.foo
  288. def __setstate__(self, state):
  289. self.foo = state
  290. def __eq__(self, other):
  291. return self.foo == other.foo
  292. x = C(42)
  293. self.assertRaises(NotImplementedError, copy.copy, x)
  294. # self.assertEqual(copy.copy(x), x)
  295. # # State with boolean value is false (issue #25718)
  296. # x = C(0.0)
  297. # self.assertEqual(copy.copy(x), x)
  298. if __name__ == "__main__":
  299. unittest.main()