PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/pypy/pypy/
Python | 381 lines | 320 code | 49 blank | 12 comment | 56 complexity | c8910098b4ef5c02f7dbd17d4300e505 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import unittest
  2. from test import test_support
  3. from weakref import proxy, ref, WeakSet
  4. import operator
  5. import copy
  6. import string
  7. import os
  8. from random import randrange, shuffle
  9. import sys
  10. import warnings
  11. import collections
  12. import gc
  13. import contextlib
  14. class Foo:
  15. pass
  16. class SomeClass(object):
  17. def __init__(self, value):
  18. self.value = value
  19. def __eq__(self, other):
  20. if type(other) != type(self):
  21. return False
  22. return other.value == self.value
  23. def __ne__(self, other):
  24. return not self.__eq__(other)
  25. def __hash__(self):
  26. return hash((SomeClass, self.value))
  27. class TestWeakSet(unittest.TestCase):
  28. def setUp(self):
  29. # need to keep references to them
  30. self.items = [SomeClass(c) for c in ('a', 'b', 'c')]
  31. self.items2 = [SomeClass(c) for c in ('x', 'y', 'z')]
  32. self.letters = [SomeClass(c) for c in string.ascii_letters]
  33. self.s = WeakSet(self.items)
  34. self.d = dict.fromkeys(self.items)
  35. self.obj = SomeClass('F')
  36. self.fs = WeakSet([self.obj])
  37. def test_methods(self):
  38. weaksetmethods = dir(WeakSet)
  39. for method in dir(set):
  40. if method == 'test_c_api' or method.startswith('_'):
  41. continue
  42. self.assertIn(method, weaksetmethods,
  43. "WeakSet missing method " + method)
  44. def test_new_or_init(self):
  45. self.assertRaises(TypeError, WeakSet, [], 2)
  46. def test_len(self):
  47. self.assertEqual(len(self.s), len(self.d))
  48. self.assertEqual(len(self.fs), 1)
  49. del self.obj
  50. test_support.gc_collect()
  51. self.assertEqual(len(self.fs), 0)
  52. def test_contains(self):
  53. for c in self.letters:
  54. self.assertEqual(c in self.s, c in self.d)
  55. # 1 is not weakref'able, but that TypeError is caught by __contains__
  56. self.assertNotIn(1, self.s)
  57. self.assertIn(self.obj, self.fs)
  58. del self.obj
  59. test_support.gc_collect()
  60. self.assertNotIn(SomeClass('F'), self.fs)
  61. def test_union(self):
  62. u = self.s.union(self.items2)
  63. for c in self.letters:
  64. self.assertEqual(c in u, c in self.d or c in self.items2)
  65. self.assertEqual(self.s, WeakSet(self.items))
  66. self.assertEqual(type(u), WeakSet)
  67. self.assertRaises(TypeError, self.s.union, [[]])
  68. for C in set, frozenset, dict.fromkeys, list, tuple:
  69. x = WeakSet(self.items + self.items2)
  70. c = C(self.items2)
  71. self.assertEqual(self.s.union(c), x)
  72. def test_or(self):
  73. i = self.s.union(self.items2)
  74. self.assertEqual(self.s | set(self.items2), i)
  75. self.assertEqual(self.s | frozenset(self.items2), i)
  76. def test_intersection(self):
  77. i = self.s.intersection(self.items2)
  78. for c in self.letters:
  79. self.assertEqual(c in i, c in self.d and c in self.items2)
  80. self.assertEqual(self.s, WeakSet(self.items))
  81. self.assertEqual(type(i), WeakSet)
  82. for C in set, frozenset, dict.fromkeys, list, tuple:
  83. x = WeakSet([])
  84. self.assertEqual(self.s.intersection(C(self.items2)), x)
  85. def test_isdisjoint(self):
  86. self.assertTrue(self.s.isdisjoint(WeakSet(self.items2)))
  87. self.assertTrue(not self.s.isdisjoint(WeakSet(self.letters)))
  88. def test_and(self):
  89. i = self.s.intersection(self.items2)
  90. self.assertEqual(self.s & set(self.items2), i)
  91. self.assertEqual(self.s & frozenset(self.items2), i)
  92. def test_difference(self):
  93. i = self.s.difference(self.items2)
  94. for c in self.letters:
  95. self.assertEqual(c in i, c in self.d and c not in self.items2)
  96. self.assertEqual(self.s, WeakSet(self.items))
  97. self.assertEqual(type(i), WeakSet)
  98. self.assertRaises(TypeError, self.s.difference, [[]])
  99. def test_sub(self):
  100. i = self.s.difference(self.items2)
  101. self.assertEqual(self.s - set(self.items2), i)
  102. self.assertEqual(self.s - frozenset(self.items2), i)
  103. def test_symmetric_difference(self):
  104. i = self.s.symmetric_difference(self.items2)
  105. for c in self.letters:
  106. self.assertEqual(c in i, (c in self.d) ^ (c in self.items2))
  107. self.assertEqual(self.s, WeakSet(self.items))
  108. self.assertEqual(type(i), WeakSet)
  109. self.assertRaises(TypeError, self.s.symmetric_difference, [[]])
  110. def test_xor(self):
  111. i = self.s.symmetric_difference(self.items2)
  112. self.assertEqual(self.s ^ set(self.items2), i)
  113. self.assertEqual(self.s ^ frozenset(self.items2), i)
  114. def test_sub_and_super(self):
  115. pl, ql, rl = map(lambda s: [SomeClass(c) for c in s], ['ab', 'abcde', 'def'])
  116. p, q, r = map(WeakSet, (pl, ql, rl))
  117. self.assertTrue(p < q)
  118. self.assertTrue(p <= q)
  119. self.assertTrue(q <= q)
  120. self.assertTrue(q > p)
  121. self.assertTrue(q >= p)
  122. self.assertFalse(q < r)
  123. self.assertFalse(q <= r)
  124. self.assertFalse(q > r)
  125. self.assertFalse(q >= r)
  126. self.assertTrue(set('a').issubset('abc'))
  127. self.assertTrue(set('abc').issuperset('a'))
  128. self.assertFalse(set('a').issubset('cbs'))
  129. self.assertFalse(set('cbs').issuperset('a'))
  130. def test_gc(self):
  131. # Create a nest of cycles to exercise overall ref count check
  132. s = WeakSet(Foo() for i in range(1000))
  133. for elem in s:
  134. elem.cycle = s
  135. elem.sub = elem
  136. elem.set = WeakSet([elem])
  137. def test_subclass_with_custom_hash(self):
  138. # Bug #1257731
  139. class H(WeakSet):
  140. def __hash__(self):
  141. return int(id(self) & 0x7fffffff)
  142. s=H()
  143. f=set()
  144. f.add(s)
  145. self.assertIn(s, f)
  146. f.remove(s)
  147. f.add(s)
  148. f.discard(s)
  149. def test_init(self):
  150. s = WeakSet()
  151. s.__init__(self.items)
  152. self.assertEqual(s, self.s)
  153. s.__init__(self.items2)
  154. self.assertEqual(s, WeakSet(self.items2))
  155. self.assertRaises(TypeError, s.__init__, s, 2);
  156. self.assertRaises(TypeError, s.__init__, 1);
  157. def test_constructor_identity(self):
  158. s = WeakSet(self.items)
  159. t = WeakSet(s)
  160. self.assertNotEqual(id(s), id(t))
  161. def test_hash(self):
  162. self.assertRaises(TypeError, hash, self.s)
  163. def test_clear(self):
  164. self.s.clear()
  165. self.assertEqual(self.s, WeakSet([]))
  166. self.assertEqual(len(self.s), 0)
  167. def test_copy(self):
  168. dup = self.s.copy()
  169. self.assertEqual(self.s, dup)
  170. self.assertNotEqual(id(self.s), id(dup))
  171. def test_add(self):
  172. x = SomeClass('Q')
  173. self.s.add(x)
  174. self.assertIn(x, self.s)
  175. dup = self.s.copy()
  176. self.s.add(x)
  177. self.assertEqual(self.s, dup)
  178. self.assertRaises(TypeError, self.s.add, [])
  179. self.fs.add(Foo())
  180. test_support.gc_collect()
  181. self.assertTrue(len(self.fs) == 1)
  182. self.fs.add(self.obj)
  183. self.assertTrue(len(self.fs) == 1)
  184. def test_remove(self):
  185. x = SomeClass('a')
  186. self.s.remove(x)
  187. self.assertNotIn(x, self.s)
  188. self.assertRaises(KeyError, self.s.remove, x)
  189. self.assertRaises(TypeError, self.s.remove, [])
  190. def test_discard(self):
  191. a, q = SomeClass('a'), SomeClass('Q')
  192. self.s.discard(a)
  193. self.assertNotIn(a, self.s)
  194. self.s.discard(q)
  195. self.assertRaises(TypeError, self.s.discard, [])
  196. def test_pop(self):
  197. for i in range(len(self.s)):
  198. elem = self.s.pop()
  199. self.assertNotIn(elem, self.s)
  200. self.assertRaises(KeyError, self.s.pop)
  201. def test_update(self):
  202. retval = self.s.update(self.items2)
  203. self.assertEqual(retval, None)
  204. for c in (self.items + self.items2):
  205. self.assertIn(c, self.s)
  206. self.assertRaises(TypeError, self.s.update, [[]])
  207. def test_update_set(self):
  208. self.s.update(set(self.items2))
  209. for c in (self.items + self.items2):
  210. self.assertIn(c, self.s)
  211. def test_ior(self):
  212. self.s |= set(self.items2)
  213. for c in (self.items + self.items2):
  214. self.assertIn(c, self.s)
  215. def test_intersection_update(self):
  216. retval = self.s.intersection_update(self.items2)
  217. self.assertEqual(retval, None)
  218. for c in (self.items + self.items2):
  219. if c in self.items2 and c in self.items:
  220. self.assertIn(c, self.s)
  221. else:
  222. self.assertNotIn(c, self.s)
  223. self.assertRaises(TypeError, self.s.intersection_update, [[]])
  224. def test_iand(self):
  225. self.s &= set(self.items2)
  226. for c in (self.items + self.items2):
  227. if c in self.items2 and c in self.items:
  228. self.assertIn(c, self.s)
  229. else:
  230. self.assertNotIn(c, self.s)
  231. def test_difference_update(self):
  232. retval = self.s.difference_update(self.items2)
  233. self.assertEqual(retval, None)
  234. for c in (self.items + self.items2):
  235. if c in self.items and c not in self.items2:
  236. self.assertIn(c, self.s)
  237. else:
  238. self.assertNotIn(c, self.s)
  239. self.assertRaises(TypeError, self.s.difference_update, [[]])
  240. self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
  241. def test_isub(self):
  242. self.s -= set(self.items2)
  243. for c in (self.items + self.items2):
  244. if c in self.items and c not in self.items2:
  245. self.assertIn(c, self.s)
  246. else:
  247. self.assertNotIn(c, self.s)
  248. def test_symmetric_difference_update(self):
  249. retval = self.s.symmetric_difference_update(self.items2)
  250. self.assertEqual(retval, None)
  251. for c in (self.items + self.items2):
  252. if (c in self.items) ^ (c in self.items2):
  253. self.assertIn(c, self.s)
  254. else:
  255. self.assertNotIn(c, self.s)
  256. self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
  257. def test_ixor(self):
  258. self.s ^= set(self.items2)
  259. for c in (self.items + self.items2):
  260. if (c in self.items) ^ (c in self.items2):
  261. self.assertIn(c, self.s)
  262. else:
  263. self.assertNotIn(c, self.s)
  264. def test_inplace_on_self(self):
  265. t = self.s.copy()
  266. t |= t
  267. self.assertEqual(t, self.s)
  268. t &= t
  269. self.assertEqual(t, self.s)
  270. t -= t
  271. self.assertEqual(t, WeakSet())
  272. t = self.s.copy()
  273. t ^= t
  274. self.assertEqual(t, WeakSet())
  275. def test_eq(self):
  276. # issue 5964
  277. self.assertTrue(self.s == self.s)
  278. self.assertTrue(self.s == WeakSet(self.items))
  279. self.assertFalse(self.s == set(self.items))
  280. self.assertFalse(self.s == list(self.items))
  281. self.assertFalse(self.s == tuple(self.items))
  282. self.assertFalse(self.s == 1)
  283. def test_weak_destroy_while_iterating(self):
  284. # Issue #7105: iterators shouldn't crash when a key is implicitly removed
  285. # Create new items to be sure no-one else holds a reference
  286. items = [SomeClass(c) for c in ('a', 'b', 'c')]
  287. s = WeakSet(items)
  288. it = iter(s)
  289. next(it) # Trigger internal iteration
  290. # Destroy an item
  291. del items[-1]
  292. test_support.gc_collect()
  293. # We have removed either the first consumed items, or another one
  294. self.assertIn(len(list(it)), [len(items), len(items) - 1])
  295. del it
  296. test_support.gc_collect()
  297. # The removal has been committed
  298. self.assertEqual(len(s), len(items))
  299. def test_weak_destroy_and_mutate_while_iterating(self):
  300. # Issue #7105: iterators shouldn't crash when a key is implicitly removed
  301. items = [SomeClass(c) for c in string.ascii_letters]
  302. s = WeakSet(items)
  303. @contextlib.contextmanager
  304. def testcontext():
  305. try:
  306. it = iter(s)
  307. next(it)
  308. # Schedule an item for removal and recreate it
  309. u = SomeClass(str(items.pop()))
  310. gc.collect() # just in case
  311. yield u
  312. finally:
  313. it = None # should commit all removals
  314. with testcontext() as u:
  315. self.assertNotIn(u, s)
  316. with testcontext() as u:
  317. self.assertRaises(KeyError, s.remove, u)
  318. self.assertNotIn(u, s)
  319. with testcontext() as u:
  320. s.add(u)
  321. self.assertIn(u, s)
  322. t = s.copy()
  323. with testcontext() as u:
  324. s.update(t)
  325. self.assertEqual(len(s), len(t))
  326. with testcontext() as u:
  327. s.clear()
  328. self.assertEqual(len(s), 0)
  329. def test_main(verbose=None):
  330. test_support.run_unittest(TestWeakSet)
  331. if __name__ == "__main__":
  332. test_main(verbose=True)