test_dict.py 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. import collections
  2. import collections.abc
  3. import gc
  4. import pickle
  5. import random
  6. import string
  7. import sys
  8. import unittest
  9. import weakref
  10. from test import support
  11. from test.support import import_helper
  12. class DictTest(unittest.TestCase):
  13. def test_invalid_keyword_arguments(self):
  14. class Custom(dict):
  15. pass
  16. for invalid in {1 : 2}, Custom({1 : 2}):
  17. with self.assertRaises(TypeError):
  18. dict(**invalid)
  19. with self.assertRaises(TypeError):
  20. {}.update(**invalid)
  21. def test_constructor(self):
  22. # calling built-in types without argument must return empty
  23. self.assertEqual(dict(), {})
  24. self.assertIsNot(dict(), {})
  25. def test_literal_constructor(self):
  26. # check literal constructor for different sized dicts
  27. # (to exercise the BUILD_MAP oparg).
  28. for n in (0, 1, 6, 256, 400):
  29. items = [(''.join(random.sample(string.ascii_letters, 8)), i)
  30. for i in range(n)]
  31. random.shuffle(items)
  32. formatted_items = ('{!r}: {:d}'.format(k, v) for k, v in items)
  33. dictliteral = '{' + ', '.join(formatted_items) + '}'
  34. self.assertEqual(eval(dictliteral), dict(items))
  35. def test_merge_operator(self):
  36. a = {0: 0, 1: 1, 2: 1}
  37. b = {1: 1, 2: 2, 3: 3}
  38. c = a.copy()
  39. c |= b
  40. self.assertEqual(a | b, {0: 0, 1: 1, 2: 2, 3: 3})
  41. self.assertEqual(c, {0: 0, 1: 1, 2: 2, 3: 3})
  42. c = b.copy()
  43. c |= a
  44. self.assertEqual(b | a, {1: 1, 2: 1, 3: 3, 0: 0})
  45. self.assertEqual(c, {1: 1, 2: 1, 3: 3, 0: 0})
  46. c = a.copy()
  47. c |= [(1, 1), (2, 2), (3, 3)]
  48. self.assertEqual(c, {0: 0, 1: 1, 2: 2, 3: 3})
  49. self.assertIs(a.__or__(None), NotImplemented)
  50. self.assertIs(a.__or__(()), NotImplemented)
  51. self.assertIs(a.__or__("BAD"), NotImplemented)
  52. self.assertIs(a.__or__(""), NotImplemented)
  53. self.assertRaises(TypeError, a.__ior__, None)
  54. self.assertEqual(a.__ior__(()), {0: 0, 1: 1, 2: 1})
  55. self.assertRaises(ValueError, a.__ior__, "BAD")
  56. self.assertEqual(a.__ior__(""), {0: 0, 1: 1, 2: 1})
  57. def test_bool(self):
  58. self.assertIs(not {}, True)
  59. self.assertTrue({1: 2})
  60. self.assertIs(bool({}), False)
  61. self.assertIs(bool({1: 2}), True)
  62. def test_keys(self):
  63. d = {}
  64. self.assertEqual(set(d.keys()), set())
  65. d = {'a': 1, 'b': 2}
  66. k = d.keys()
  67. self.assertEqual(set(k), {'a', 'b'})
  68. self.assertIn('a', k)
  69. self.assertIn('b', k)
  70. self.assertIn('a', d)
  71. self.assertIn('b', d)
  72. self.assertRaises(TypeError, d.keys, None)
  73. self.assertEqual(repr(dict(a=1).keys()), "dict_keys(['a'])")
  74. def test_values(self):
  75. d = {}
  76. self.assertEqual(set(d.values()), set())
  77. d = {1:2}
  78. self.assertEqual(set(d.values()), {2})
  79. self.assertRaises(TypeError, d.values, None)
  80. self.assertEqual(repr(dict(a=1).values()), "dict_values([1])")
  81. def test_items(self):
  82. d = {}
  83. self.assertEqual(set(d.items()), set())
  84. d = {1:2}
  85. self.assertEqual(set(d.items()), {(1, 2)})
  86. self.assertRaises(TypeError, d.items, None)
  87. self.assertEqual(repr(dict(a=1).items()), "dict_items([('a', 1)])")
  88. def test_views_mapping(self):
  89. mappingproxy = type(type.__dict__)
  90. class Dict(dict):
  91. pass
  92. for cls in [dict, Dict]:
  93. d = cls()
  94. m1 = d.keys().mapping
  95. m2 = d.values().mapping
  96. m3 = d.items().mapping
  97. for m in [m1, m2, m3]:
  98. self.assertIsInstance(m, mappingproxy)
  99. self.assertEqual(m, d)
  100. d["foo"] = "bar"
  101. for m in [m1, m2, m3]:
  102. self.assertIsInstance(m, mappingproxy)
  103. self.assertEqual(m, d)
  104. def test_contains(self):
  105. d = {}
  106. self.assertNotIn('a', d)
  107. self.assertFalse('a' in d)
  108. self.assertTrue('a' not in d)
  109. d = {'a': 1, 'b': 2}
  110. self.assertIn('a', d)
  111. self.assertIn('b', d)
  112. self.assertNotIn('c', d)
  113. self.assertRaises(TypeError, d.__contains__)
  114. def test_len(self):
  115. d = {}
  116. self.assertEqual(len(d), 0)
  117. d = {'a': 1, 'b': 2}
  118. self.assertEqual(len(d), 2)
  119. def test_getitem(self):
  120. d = {'a': 1, 'b': 2}
  121. self.assertEqual(d['a'], 1)
  122. self.assertEqual(d['b'], 2)
  123. d['c'] = 3
  124. d['a'] = 4
  125. self.assertEqual(d['c'], 3)
  126. self.assertEqual(d['a'], 4)
  127. del d['b']
  128. self.assertEqual(d, {'a': 4, 'c': 3})
  129. self.assertRaises(TypeError, d.__getitem__)
  130. class BadEq(object):
  131. def __eq__(self, other):
  132. raise Exc()
  133. def __hash__(self):
  134. return 24
  135. d = {}
  136. d[BadEq()] = 42
  137. self.assertRaises(KeyError, d.__getitem__, 23)
  138. class Exc(Exception): pass
  139. class BadHash(object):
  140. fail = False
  141. def __hash__(self):
  142. if self.fail:
  143. raise Exc()
  144. else:
  145. return 42
  146. x = BadHash()
  147. d[x] = 42
  148. x.fail = True
  149. self.assertRaises(Exc, d.__getitem__, x)
  150. def test_clear(self):
  151. d = {1:1, 2:2, 3:3}
  152. d.clear()
  153. self.assertEqual(d, {})
  154. self.assertRaises(TypeError, d.clear, None)
  155. def test_update(self):
  156. d = {}
  157. d.update({1:100})
  158. d.update({2:20})
  159. d.update({1:1, 2:2, 3:3})
  160. self.assertEqual(d, {1:1, 2:2, 3:3})
  161. d.update()
  162. self.assertEqual(d, {1:1, 2:2, 3:3})
  163. self.assertRaises((TypeError, AttributeError), d.update, None)
  164. class SimpleUserDict:
  165. def __init__(self):
  166. self.d = {1:1, 2:2, 3:3}
  167. def keys(self):
  168. return self.d.keys()
  169. def __getitem__(self, i):
  170. return self.d[i]
  171. d.clear()
  172. d.update(SimpleUserDict())
  173. self.assertEqual(d, {1:1, 2:2, 3:3})
  174. class Exc(Exception): pass
  175. d.clear()
  176. class FailingUserDict:
  177. def keys(self):
  178. raise Exc
  179. self.assertRaises(Exc, d.update, FailingUserDict())
  180. class FailingUserDict:
  181. def keys(self):
  182. class BogonIter:
  183. def __init__(self):
  184. self.i = 1
  185. def __iter__(self):
  186. return self
  187. def __next__(self):
  188. if self.i:
  189. self.i = 0
  190. return 'a'
  191. raise Exc
  192. return BogonIter()
  193. def __getitem__(self, key):
  194. return key
  195. self.assertRaises(Exc, d.update, FailingUserDict())
  196. class FailingUserDict:
  197. def keys(self):
  198. class BogonIter:
  199. def __init__(self):
  200. self.i = ord('a')
  201. def __iter__(self):
  202. return self
  203. def __next__(self):
  204. if self.i <= ord('z'):
  205. rtn = chr(self.i)
  206. self.i += 1
  207. return rtn
  208. raise StopIteration
  209. return BogonIter()
  210. def __getitem__(self, key):
  211. raise Exc
  212. self.assertRaises(Exc, d.update, FailingUserDict())
  213. class badseq(object):
  214. def __iter__(self):
  215. return self
  216. def __next__(self):
  217. raise Exc()
  218. self.assertRaises(Exc, {}.update, badseq())
  219. self.assertRaises(ValueError, {}.update, [(1, 2, 3)])
  220. def test_fromkeys(self):
  221. self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
  222. d = {}
  223. self.assertIsNot(d.fromkeys('abc'), d)
  224. self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
  225. self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0})
  226. self.assertEqual(d.fromkeys([]), {})
  227. def g():
  228. yield 1
  229. self.assertEqual(d.fromkeys(g()), {1:None})
  230. self.assertRaises(TypeError, {}.fromkeys, 3)
  231. class dictlike(dict): pass
  232. self.assertEqual(dictlike.fromkeys('a'), {'a':None})
  233. self.assertEqual(dictlike().fromkeys('a'), {'a':None})
  234. self.assertIsInstance(dictlike.fromkeys('a'), dictlike)
  235. self.assertIsInstance(dictlike().fromkeys('a'), dictlike)
  236. class mydict(dict):
  237. def __new__(cls):
  238. return collections.UserDict()
  239. ud = mydict.fromkeys('ab')
  240. self.assertEqual(ud, {'a':None, 'b':None})
  241. self.assertIsInstance(ud, collections.UserDict)
  242. self.assertRaises(TypeError, dict.fromkeys)
  243. class Exc(Exception): pass
  244. class baddict1(dict):
  245. def __init__(self):
  246. raise Exc()
  247. self.assertRaises(Exc, baddict1.fromkeys, [1])
  248. class BadSeq(object):
  249. def __iter__(self):
  250. return self
  251. def __next__(self):
  252. raise Exc()
  253. self.assertRaises(Exc, dict.fromkeys, BadSeq())
  254. class baddict2(dict):
  255. def __setitem__(self, key, value):
  256. raise Exc()
  257. self.assertRaises(Exc, baddict2.fromkeys, [1])
  258. # test fast path for dictionary inputs
  259. d = dict(zip(range(6), range(6)))
  260. self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0]*6)))
  261. class baddict3(dict):
  262. def __new__(cls):
  263. return d
  264. d = {i : i for i in range(10)}
  265. res = d.copy()
  266. res.update(a=None, b=None, c=None)
  267. self.assertEqual(baddict3.fromkeys({"a", "b", "c"}), res)
  268. def test_copy(self):
  269. d = {1: 1, 2: 2, 3: 3}
  270. self.assertIsNot(d.copy(), d)
  271. self.assertEqual(d.copy(), d)
  272. self.assertEqual(d.copy(), {1: 1, 2: 2, 3: 3})
  273. copy = d.copy()
  274. d[4] = 4
  275. self.assertNotEqual(copy, d)
  276. self.assertEqual({}.copy(), {})
  277. self.assertRaises(TypeError, d.copy, None)
  278. def test_copy_fuzz(self):
  279. for dict_size in [10, 100, 1000, 10000, 100000]:
  280. dict_size = random.randrange(
  281. dict_size // 2, dict_size + dict_size // 2)
  282. with self.subTest(dict_size=dict_size):
  283. d = {}
  284. for i in range(dict_size):
  285. d[i] = i
  286. d2 = d.copy()
  287. self.assertIsNot(d2, d)
  288. self.assertEqual(d, d2)
  289. d2['key'] = 'value'
  290. self.assertNotEqual(d, d2)
  291. self.assertEqual(len(d2), len(d) + 1)
  292. def test_copy_maintains_tracking(self):
  293. class A:
  294. pass
  295. key = A()
  296. for d in ({}, {'a': 1}, {key: 'val'}):
  297. d2 = d.copy()
  298. self.assertEqual(gc.is_tracked(d), gc.is_tracked(d2))
  299. def test_copy_noncompact(self):
  300. # Dicts don't compact themselves on del/pop operations.
  301. # Copy will use a slow merging strategy that produces
  302. # a compacted copy when roughly 33% of dict is a non-used
  303. # keys-space (to optimize memory footprint).
  304. # In this test we want to hit the slow/compacting
  305. # branch of dict.copy() and make sure it works OK.
  306. d = {k: k for k in range(1000)}
  307. for k in range(950):
  308. del d[k]
  309. d2 = d.copy()
  310. self.assertEqual(d2, d)
  311. def test_get(self):
  312. d = {}
  313. self.assertIs(d.get('c'), None)
  314. self.assertEqual(d.get('c', 3), 3)
  315. d = {'a': 1, 'b': 2}
  316. self.assertIs(d.get('c'), None)
  317. self.assertEqual(d.get('c', 3), 3)
  318. self.assertEqual(d.get('a'), 1)
  319. self.assertEqual(d.get('a', 3), 1)
  320. self.assertRaises(TypeError, d.get)
  321. self.assertRaises(TypeError, d.get, None, None, None)
  322. def test_setdefault(self):
  323. # dict.setdefault()
  324. d = {}
  325. self.assertIs(d.setdefault('key0'), None)
  326. d.setdefault('key0', [])
  327. self.assertIs(d.setdefault('key0'), None)
  328. d.setdefault('key', []).append(3)
  329. self.assertEqual(d['key'][0], 3)
  330. d.setdefault('key', []).append(4)
  331. self.assertEqual(len(d['key']), 2)
  332. self.assertRaises(TypeError, d.setdefault)
  333. class Exc(Exception): pass
  334. class BadHash(object):
  335. fail = False
  336. def __hash__(self):
  337. if self.fail:
  338. raise Exc()
  339. else:
  340. return 42
  341. x = BadHash()
  342. d[x] = 42
  343. x.fail = True
  344. self.assertRaises(Exc, d.setdefault, x, [])
  345. def test_setdefault_atomic(self):
  346. # Issue #13521: setdefault() calls __hash__ and __eq__ only once.
  347. class Hashed(object):
  348. def __init__(self):
  349. self.hash_count = 0
  350. self.eq_count = 0
  351. def __hash__(self):
  352. self.hash_count += 1
  353. return 42
  354. def __eq__(self, other):
  355. self.eq_count += 1
  356. return id(self) == id(other)
  357. hashed1 = Hashed()
  358. y = {hashed1: 5}
  359. hashed2 = Hashed()
  360. y.setdefault(hashed2, [])
  361. self.assertEqual(hashed1.hash_count, 1)
  362. self.assertEqual(hashed2.hash_count, 1)
  363. self.assertEqual(hashed1.eq_count + hashed2.eq_count, 1)
  364. def test_setitem_atomic_at_resize(self):
  365. class Hashed(object):
  366. def __init__(self):
  367. self.hash_count = 0
  368. self.eq_count = 0
  369. def __hash__(self):
  370. self.hash_count += 1
  371. return 42
  372. def __eq__(self, other):
  373. self.eq_count += 1
  374. return id(self) == id(other)
  375. hashed1 = Hashed()
  376. # 5 items
  377. y = {hashed1: 5, 0: 0, 1: 1, 2: 2, 3: 3}
  378. hashed2 = Hashed()
  379. # 6th item forces a resize
  380. y[hashed2] = []
  381. self.assertEqual(hashed1.hash_count, 1)
  382. self.assertEqual(hashed2.hash_count, 1)
  383. self.assertEqual(hashed1.eq_count + hashed2.eq_count, 1)
  384. def test_popitem(self):
  385. # dict.popitem()
  386. for copymode in -1, +1:
  387. # -1: b has same structure as a
  388. # +1: b is a.copy()
  389. for log2size in range(12):
  390. size = 2**log2size
  391. a = {}
  392. b = {}
  393. for i in range(size):
  394. a[repr(i)] = i
  395. if copymode < 0:
  396. b[repr(i)] = i
  397. if copymode > 0:
  398. b = a.copy()
  399. for i in range(size):
  400. ka, va = ta = a.popitem()
  401. self.assertEqual(va, int(ka))
  402. kb, vb = tb = b.popitem()
  403. self.assertEqual(vb, int(kb))
  404. self.assertFalse(copymode < 0 and ta != tb)
  405. self.assertFalse(a)
  406. self.assertFalse(b)
  407. d = {}
  408. self.assertRaises(KeyError, d.popitem)
  409. def test_pop(self):
  410. # Tests for pop with specified key
  411. d = {}
  412. k, v = 'abc', 'def'
  413. d[k] = v
  414. self.assertRaises(KeyError, d.pop, 'ghi')
  415. self.assertEqual(d.pop(k), v)
  416. self.assertEqual(len(d), 0)
  417. self.assertRaises(KeyError, d.pop, k)
  418. self.assertEqual(d.pop(k, v), v)
  419. d[k] = v
  420. self.assertEqual(d.pop(k, 1), v)
  421. self.assertRaises(TypeError, d.pop)
  422. class Exc(Exception): pass
  423. class BadHash(object):
  424. fail = False
  425. def __hash__(self):
  426. if self.fail:
  427. raise Exc()
  428. else:
  429. return 42
  430. x = BadHash()
  431. d[x] = 42
  432. x.fail = True
  433. self.assertRaises(Exc, d.pop, x)
  434. def test_mutating_iteration(self):
  435. # changing dict size during iteration
  436. d = {}
  437. d[1] = 1
  438. with self.assertRaises(RuntimeError):
  439. for i in d:
  440. d[i+1] = 1
  441. def test_mutating_iteration_delete(self):
  442. # change dict content during iteration
  443. d = {}
  444. d[0] = 0
  445. with self.assertRaises(RuntimeError):
  446. for i in d:
  447. del d[0]
  448. d[0] = 0
  449. def test_mutating_iteration_delete_over_values(self):
  450. # change dict content during iteration
  451. d = {}
  452. d[0] = 0
  453. with self.assertRaises(RuntimeError):
  454. for i in d.values():
  455. del d[0]
  456. d[0] = 0
  457. def test_mutating_iteration_delete_over_items(self):
  458. # change dict content during iteration
  459. d = {}
  460. d[0] = 0
  461. with self.assertRaises(RuntimeError):
  462. for i in d.items():
  463. del d[0]
  464. d[0] = 0
  465. def test_mutating_lookup(self):
  466. # changing dict during a lookup (issue #14417)
  467. class NastyKey:
  468. mutate_dict = None
  469. def __init__(self, value):
  470. self.value = value
  471. def __hash__(self):
  472. # hash collision!
  473. return 1
  474. def __eq__(self, other):
  475. if NastyKey.mutate_dict:
  476. mydict, key = NastyKey.mutate_dict
  477. NastyKey.mutate_dict = None
  478. del mydict[key]
  479. return self.value == other.value
  480. key1 = NastyKey(1)
  481. key2 = NastyKey(2)
  482. d = {key1: 1}
  483. NastyKey.mutate_dict = (d, key1)
  484. d[key2] = 2
  485. self.assertEqual(d, {key2: 2})
  486. def test_repr(self):
  487. d = {}
  488. self.assertEqual(repr(d), '{}')
  489. d[1] = 2
  490. self.assertEqual(repr(d), '{1: 2}')
  491. d = {}
  492. d[1] = d
  493. self.assertEqual(repr(d), '{1: {...}}')
  494. class Exc(Exception): pass
  495. class BadRepr(object):
  496. def __repr__(self):
  497. raise Exc()
  498. d = {1: BadRepr()}
  499. self.assertRaises(Exc, repr, d)
  500. def test_repr_deep(self):
  501. d = {}
  502. for i in range(sys.getrecursionlimit() + 100):
  503. d = {1: d}
  504. self.assertRaises(RecursionError, repr, d)
  505. def test_eq(self):
  506. self.assertEqual({}, {})
  507. self.assertEqual({1: 2}, {1: 2})
  508. class Exc(Exception): pass
  509. class BadCmp(object):
  510. def __eq__(self, other):
  511. raise Exc()
  512. def __hash__(self):
  513. return 1
  514. d1 = {BadCmp(): 1}
  515. d2 = {1: 1}
  516. with self.assertRaises(Exc):
  517. d1 == d2
  518. def test_keys_contained(self):
  519. self.helper_keys_contained(lambda x: x.keys())
  520. self.helper_keys_contained(lambda x: x.items())
  521. def helper_keys_contained(self, fn):
  522. # Test rich comparisons against dict key views, which should behave the
  523. # same as sets.
  524. empty = fn(dict())
  525. empty2 = fn(dict())
  526. smaller = fn({1:1, 2:2})
  527. larger = fn({1:1, 2:2, 3:3})
  528. larger2 = fn({1:1, 2:2, 3:3})
  529. larger3 = fn({4:1, 2:2, 3:3})
  530. self.assertTrue(smaller < larger)
  531. self.assertTrue(smaller <= larger)
  532. self.assertTrue(larger > smaller)
  533. self.assertTrue(larger >= smaller)
  534. self.assertFalse(smaller >= larger)
  535. self.assertFalse(smaller > larger)
  536. self.assertFalse(larger <= smaller)
  537. self.assertFalse(larger < smaller)
  538. self.assertFalse(smaller < larger3)
  539. self.assertFalse(smaller <= larger3)
  540. self.assertFalse(larger3 > smaller)
  541. self.assertFalse(larger3 >= smaller)
  542. # Inequality strictness
  543. self.assertTrue(larger2 >= larger)
  544. self.assertTrue(larger2 <= larger)
  545. self.assertFalse(larger2 > larger)
  546. self.assertFalse(larger2 < larger)
  547. self.assertTrue(larger == larger2)
  548. self.assertTrue(smaller != larger)
  549. # There is an optimization on the zero-element case.
  550. self.assertTrue(empty == empty2)
  551. self.assertFalse(empty != empty2)
  552. self.assertFalse(empty == smaller)
  553. self.assertTrue(empty != smaller)
  554. # With the same size, an elementwise compare happens
  555. self.assertTrue(larger != larger3)
  556. self.assertFalse(larger == larger3)
  557. def test_errors_in_view_containment_check(self):
  558. class C:
  559. def __eq__(self, other):
  560. raise RuntimeError
  561. d1 = {1: C()}
  562. d2 = {1: C()}
  563. with self.assertRaises(RuntimeError):
  564. d1.items() == d2.items()
  565. with self.assertRaises(RuntimeError):
  566. d1.items() != d2.items()
  567. with self.assertRaises(RuntimeError):
  568. d1.items() <= d2.items()
  569. with self.assertRaises(RuntimeError):
  570. d1.items() >= d2.items()
  571. d3 = {1: C(), 2: C()}
  572. with self.assertRaises(RuntimeError):
  573. d2.items() < d3.items()
  574. with self.assertRaises(RuntimeError):
  575. d3.items() > d2.items()
  576. def test_dictview_set_operations_on_keys(self):
  577. k1 = {1:1, 2:2}.keys()
  578. k2 = {1:1, 2:2, 3:3}.keys()
  579. k3 = {4:4}.keys()
  580. self.assertEqual(k1 - k2, set())
  581. self.assertEqual(k1 - k3, {1,2})
  582. self.assertEqual(k2 - k1, {3})
  583. self.assertEqual(k3 - k1, {4})
  584. self.assertEqual(k1 & k2, {1,2})
  585. self.assertEqual(k1 & k3, set())
  586. self.assertEqual(k1 | k2, {1,2,3})
  587. self.assertEqual(k1 ^ k2, {3})
  588. self.assertEqual(k1 ^ k3, {1,2,4})
  589. def test_dictview_set_operations_on_items(self):
  590. k1 = {1:1, 2:2}.items()
  591. k2 = {1:1, 2:2, 3:3}.items()
  592. k3 = {4:4}.items()
  593. self.assertEqual(k1 - k2, set())
  594. self.assertEqual(k1 - k3, {(1,1), (2,2)})
  595. self.assertEqual(k2 - k1, {(3,3)})
  596. self.assertEqual(k3 - k1, {(4,4)})
  597. self.assertEqual(k1 & k2, {(1,1), (2,2)})
  598. self.assertEqual(k1 & k3, set())
  599. self.assertEqual(k1 | k2, {(1,1), (2,2), (3,3)})
  600. self.assertEqual(k1 ^ k2, {(3,3)})
  601. self.assertEqual(k1 ^ k3, {(1,1), (2,2), (4,4)})
  602. def test_items_symmetric_difference(self):
  603. rr = random.randrange
  604. for _ in range(100):
  605. left = {x:rr(3) for x in range(20) if rr(2)}
  606. right = {x:rr(3) for x in range(20) if rr(2)}
  607. with self.subTest(left=left, right=right):
  608. expected = set(left.items()) ^ set(right.items())
  609. actual = left.items() ^ right.items()
  610. self.assertEqual(actual, expected)
  611. def test_dictview_mixed_set_operations(self):
  612. # Just a few for .keys()
  613. self.assertTrue({1:1}.keys() == {1})
  614. self.assertTrue({1} == {1:1}.keys())
  615. self.assertEqual({1:1}.keys() | {2}, {1, 2})
  616. self.assertEqual({2} | {1:1}.keys(), {1, 2})
  617. # And a few for .items()
  618. self.assertTrue({1:1}.items() == {(1,1)})
  619. self.assertTrue({(1,1)} == {1:1}.items())
  620. self.assertEqual({1:1}.items() | {2}, {(1,1), 2})
  621. self.assertEqual({2} | {1:1}.items(), {(1,1), 2})
  622. def test_missing(self):
  623. # Make sure dict doesn't have a __missing__ method
  624. self.assertFalse(hasattr(dict, "__missing__"))
  625. self.assertFalse(hasattr({}, "__missing__"))
  626. # Test several cases:
  627. # (D) subclass defines __missing__ method returning a value
  628. # (E) subclass defines __missing__ method raising RuntimeError
  629. # (F) subclass sets __missing__ instance variable (no effect)
  630. # (G) subclass doesn't define __missing__ at all
  631. class D(dict):
  632. def __missing__(self, key):
  633. return 42
  634. d = D({1: 2, 3: 4})
  635. self.assertEqual(d[1], 2)
  636. self.assertEqual(d[3], 4)
  637. self.assertNotIn(2, d)
  638. self.assertNotIn(2, d.keys())
  639. self.assertEqual(d[2], 42)
  640. class E(dict):
  641. def __missing__(self, key):
  642. raise RuntimeError(key)
  643. e = E()
  644. with self.assertRaises(RuntimeError) as c:
  645. e[42]
  646. self.assertEqual(c.exception.args, (42,))
  647. class F(dict):
  648. def __init__(self):
  649. # An instance variable __missing__ should have no effect
  650. self.__missing__ = lambda key: None
  651. f = F()
  652. with self.assertRaises(KeyError) as c:
  653. f[42]
  654. self.assertEqual(c.exception.args, (42,))
  655. class G(dict):
  656. pass
  657. g = G()
  658. with self.assertRaises(KeyError) as c:
  659. g[42]
  660. self.assertEqual(c.exception.args, (42,))
  661. def test_tuple_keyerror(self):
  662. # SF #1576657
  663. d = {}
  664. with self.assertRaises(KeyError) as c:
  665. d[(1,)]
  666. self.assertEqual(c.exception.args, ((1,),))
  667. def test_bad_key(self):
  668. # Dictionary lookups should fail if __eq__() raises an exception.
  669. class CustomException(Exception):
  670. pass
  671. class BadDictKey:
  672. def __hash__(self):
  673. return hash(self.__class__)
  674. def __eq__(self, other):
  675. if isinstance(other, self.__class__):
  676. raise CustomException
  677. return other
  678. d = {}
  679. x1 = BadDictKey()
  680. x2 = BadDictKey()
  681. d[x1] = 1
  682. for stmt in ['d[x2] = 2',
  683. 'z = d[x2]',
  684. 'x2 in d',
  685. 'd.get(x2)',
  686. 'd.setdefault(x2, 42)',
  687. 'd.pop(x2)',
  688. 'd.update({x2: 2})']:
  689. with self.assertRaises(CustomException):
  690. exec(stmt, locals())
  691. def test_resize1(self):
  692. # Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
  693. # This version got an assert failure in debug build, infinite loop in
  694. # release build. Unfortunately, provoking this kind of stuff requires
  695. # a mix of inserts and deletes hitting exactly the right hash codes in
  696. # exactly the right order, and I can't think of a randomized approach
  697. # that would be *likely* to hit a failing case in reasonable time.
  698. d = {}
  699. for i in range(5):
  700. d[i] = i
  701. for i in range(5):
  702. del d[i]
  703. for i in range(5, 9): # i==8 was the problem
  704. d[i] = i
  705. def test_resize2(self):
  706. # Another dict resizing bug (SF bug #1456209).
  707. # This caused Segmentation faults or Illegal instructions.
  708. class X(object):
  709. def __hash__(self):
  710. return 5
  711. def __eq__(self, other):
  712. if resizing:
  713. d.clear()
  714. return False
  715. d = {}
  716. resizing = False
  717. d[X()] = 1
  718. d[X()] = 2
  719. d[X()] = 3
  720. d[X()] = 4
  721. d[X()] = 5
  722. # now trigger a resize
  723. resizing = True
  724. d[9] = 6
  725. def test_empty_presized_dict_in_freelist(self):
  726. # Bug #3537: if an empty but presized dict with a size larger
  727. # than 7 was in the freelist, it triggered an assertion failure
  728. with self.assertRaises(ZeroDivisionError):
  729. d = {'a': 1 // 0, 'b': None, 'c': None, 'd': None, 'e': None,
  730. 'f': None, 'g': None, 'h': None}
  731. d = {}
  732. def test_container_iterator(self):
  733. # Bug #3680: tp_traverse was not implemented for dictiter and
  734. # dictview objects.
  735. class C(object):
  736. pass
  737. views = (dict.items, dict.values, dict.keys)
  738. for v in views:
  739. obj = C()
  740. ref = weakref.ref(obj)
  741. container = {obj: 1}
  742. obj.v = v(container)
  743. obj.x = iter(obj.v)
  744. del obj, container
  745. gc.collect()
  746. self.assertIs(ref(), None, "Cycle was not collected")
  747. def _not_tracked(self, t):
  748. # Nested containers can take several collections to untrack
  749. gc.collect()
  750. gc.collect()
  751. self.assertFalse(gc.is_tracked(t), t)
  752. def _tracked(self, t):
  753. self.assertTrue(gc.is_tracked(t), t)
  754. gc.collect()
  755. gc.collect()
  756. self.assertTrue(gc.is_tracked(t), t)
  757. def test_string_keys_can_track_values(self):
  758. # Test that this doesn't leak.
  759. for i in range(10):
  760. d = {}
  761. for j in range(10):
  762. d[str(j)] = j
  763. d["foo"] = d
  764. @support.cpython_only
  765. def test_track_literals(self):
  766. # Test GC-optimization of dict literals
  767. x, y, z, w = 1.5, "a", (1, None), []
  768. self._not_tracked({})
  769. self._not_tracked({x:(), y:x, z:1})
  770. self._not_tracked({1: "a", "b": 2})
  771. self._not_tracked({1: 2, (None, True, False, ()): int})
  772. self._not_tracked({1: object()})
  773. # Dicts with mutable elements are always tracked, even if those
  774. # elements are not tracked right now.
  775. self._tracked({1: []})
  776. self._tracked({1: ([],)})
  777. self._tracked({1: {}})
  778. self._tracked({1: set()})
  779. @support.cpython_only
  780. def test_track_dynamic(self):
  781. # Test GC-optimization of dynamically-created dicts
  782. class MyObject(object):
  783. pass
  784. x, y, z, w, o = 1.5, "a", (1, object()), [], MyObject()
  785. d = dict()
  786. self._not_tracked(d)
  787. d[1] = "a"
  788. self._not_tracked(d)
  789. d[y] = 2
  790. self._not_tracked(d)
  791. d[z] = 3
  792. self._not_tracked(d)
  793. self._not_tracked(d.copy())
  794. d[4] = w
  795. self._tracked(d)
  796. self._tracked(d.copy())
  797. d[4] = None
  798. self._not_tracked(d)
  799. self._not_tracked(d.copy())
  800. # dd isn't tracked right now, but it may mutate and therefore d
  801. # which contains it must be tracked.
  802. d = dict()
  803. dd = dict()
  804. d[1] = dd
  805. self._not_tracked(dd)
  806. self._tracked(d)
  807. dd[1] = d
  808. self._tracked(dd)
  809. d = dict.fromkeys([x, y, z])
  810. self._not_tracked(d)
  811. dd = dict()
  812. dd.update(d)
  813. self._not_tracked(dd)
  814. d = dict.fromkeys([x, y, z, o])
  815. self._tracked(d)
  816. dd = dict()
  817. dd.update(d)
  818. self._tracked(dd)
  819. d = dict(x=x, y=y, z=z)
  820. self._not_tracked(d)
  821. d = dict(x=x, y=y, z=z, w=w)
  822. self._tracked(d)
  823. d = dict()
  824. d.update(x=x, y=y, z=z)
  825. self._not_tracked(d)
  826. d.update(w=w)
  827. self._tracked(d)
  828. d = dict([(x, y), (z, 1)])
  829. self._not_tracked(d)
  830. d = dict([(x, y), (z, w)])
  831. self._tracked(d)
  832. d = dict()
  833. d.update([(x, y), (z, 1)])
  834. self._not_tracked(d)
  835. d.update([(x, y), (z, w)])
  836. self._tracked(d)
  837. @support.cpython_only
  838. def test_track_subtypes(self):
  839. # Dict subtypes are always tracked
  840. class MyDict(dict):
  841. pass
  842. self._tracked(MyDict())
  843. def make_shared_key_dict(self, n):
  844. class C:
  845. pass
  846. dicts = []
  847. for i in range(n):
  848. a = C()
  849. a.x, a.y, a.z = 1, 2, 3
  850. dicts.append(a.__dict__)
  851. return dicts
  852. @support.cpython_only
  853. def test_splittable_setdefault(self):
  854. """split table must keep correct insertion
  855. order when attributes are adding using setdefault()"""
  856. a, b = self.make_shared_key_dict(2)
  857. a['a'] = 1
  858. size_a = sys.getsizeof(a)
  859. a['b'] = 2
  860. b.setdefault('b', 2)
  861. size_b = sys.getsizeof(b)
  862. b['a'] = 1
  863. self.assertEqual(list(a), ['x', 'y', 'z', 'a', 'b'])
  864. self.assertEqual(list(b), ['x', 'y', 'z', 'b', 'a'])
  865. @support.cpython_only
  866. def test_splittable_del(self):
  867. """split table must be combined when del d[k]"""
  868. a, b = self.make_shared_key_dict(2)
  869. orig_size = sys.getsizeof(a)
  870. del a['y'] # split table is combined
  871. with self.assertRaises(KeyError):
  872. del a['y']
  873. self.assertEqual(list(a), ['x', 'z'])
  874. self.assertEqual(list(b), ['x', 'y', 'z'])
  875. # Two dicts have different insertion order.
  876. a['y'] = 42
  877. self.assertEqual(list(a), ['x', 'z', 'y'])
  878. self.assertEqual(list(b), ['x', 'y', 'z'])
  879. @support.cpython_only
  880. def test_splittable_pop(self):
  881. a, b = self.make_shared_key_dict(2)
  882. a.pop('y')
  883. with self.assertRaises(KeyError):
  884. a.pop('y')
  885. self.assertEqual(list(a), ['x', 'z'])
  886. self.assertEqual(list(b), ['x', 'y', 'z'])
  887. # Two dicts have different insertion order.
  888. a['y'] = 42
  889. self.assertEqual(list(a), ['x', 'z', 'y'])
  890. self.assertEqual(list(b), ['x', 'y', 'z'])
  891. @support.cpython_only
  892. def test_splittable_pop_pending(self):
  893. """pop a pending key in a split table should not crash"""
  894. a, b = self.make_shared_key_dict(2)
  895. a['a'] = 4
  896. with self.assertRaises(KeyError):
  897. b.pop('a')
  898. @support.cpython_only
  899. def test_splittable_popitem(self):
  900. """split table must be combined when d.popitem()"""
  901. a, b = self.make_shared_key_dict(2)
  902. orig_size = sys.getsizeof(a)
  903. item = a.popitem() # split table is combined
  904. self.assertEqual(item, ('z', 3))
  905. with self.assertRaises(KeyError):
  906. del a['z']
  907. self.assertGreater(sys.getsizeof(a), orig_size)
  908. self.assertEqual(list(a), ['x', 'y'])
  909. self.assertEqual(list(b), ['x', 'y', 'z'])
  910. @support.cpython_only
  911. def test_splittable_update(self):
  912. """dict.update(other) must preserve order in other."""
  913. class C:
  914. def __init__(self, order):
  915. if order:
  916. self.a, self.b, self.c = 1, 2, 3
  917. else:
  918. self.c, self.b, self.a = 1, 2, 3
  919. o = C(True)
  920. o = C(False) # o.__dict__ has reversed order.
  921. self.assertEqual(list(o.__dict__), ["c", "b", "a"])
  922. d = {}
  923. d.update(o.__dict__)
  924. self.assertEqual(list(d), ["c", "b", "a"])
  925. def test_iterator_pickling(self):
  926. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  927. data = {1:"a", 2:"b", 3:"c"}
  928. it = iter(data)
  929. d = pickle.dumps(it, proto)
  930. it = pickle.loads(d)
  931. self.assertEqual(list(it), list(data))
  932. it = pickle.loads(d)
  933. try:
  934. drop = next(it)
  935. except StopIteration:
  936. continue
  937. d = pickle.dumps(it, proto)
  938. it = pickle.loads(d)
  939. del data[drop]
  940. self.assertEqual(list(it), list(data))
  941. def test_itemiterator_pickling(self):
  942. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  943. data = {1:"a", 2:"b", 3:"c"}
  944. # dictviews aren't picklable, only their iterators
  945. itorg = iter(data.items())
  946. d = pickle.dumps(itorg, proto)
  947. it = pickle.loads(d)
  948. # note that the type of the unpickled iterator
  949. # is not necessarily the same as the original. It is
  950. # merely an object supporting the iterator protocol, yielding
  951. # the same objects as the original one.
  952. # self.assertEqual(type(itorg), type(it))
  953. self.assertIsInstance(it, collections.abc.Iterator)
  954. self.assertEqual(dict(it), data)
  955. it = pickle.loads(d)
  956. drop = next(it)
  957. d = pickle.dumps(it, proto)
  958. it = pickle.loads(d)
  959. del data[drop[0]]
  960. self.assertEqual(dict(it), data)
  961. def test_valuesiterator_pickling(self):
  962. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  963. data = {1:"a", 2:"b", 3:"c"}
  964. # data.values() isn't picklable, only its iterator
  965. it = iter(data.values())
  966. d = pickle.dumps(it, proto)
  967. it = pickle.loads(d)
  968. self.assertEqual(list(it), list(data.values()))
  969. it = pickle.loads(d)
  970. drop = next(it)
  971. d = pickle.dumps(it, proto)
  972. it = pickle.loads(d)
  973. values = list(it) + [drop]
  974. self.assertEqual(sorted(values), sorted(list(data.values())))
  975. def test_reverseiterator_pickling(self):
  976. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  977. data = {1:"a", 2:"b", 3:"c"}
  978. it = reversed(data)
  979. d = pickle.dumps(it, proto)
  980. it = pickle.loads(d)
  981. self.assertEqual(list(it), list(reversed(data)))
  982. it = pickle.loads(d)
  983. try:
  984. drop = next(it)
  985. except StopIteration:
  986. continue
  987. d = pickle.dumps(it, proto)
  988. it = pickle.loads(d)
  989. del data[drop]
  990. self.assertEqual(list(it), list(reversed(data)))
  991. def test_reverseitemiterator_pickling(self):
  992. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  993. data = {1:"a", 2:"b", 3:"c"}
  994. # dictviews aren't picklable, only their iterators
  995. itorg = reversed(data.items())
  996. d = pickle.dumps(itorg, proto)
  997. it = pickle.loads(d)
  998. # note that the type of the unpickled iterator
  999. # is not necessarily the same as the original. It is
  1000. # merely an object supporting the iterator protocol, yielding
  1001. # the same objects as the original one.
  1002. # self.assertEqual(type(itorg), type(it))
  1003. self.assertIsInstance(it, collections.abc.Iterator)
  1004. self.assertEqual(dict(it), data)
  1005. it = pickle.loads(d)
  1006. drop = next(it)
  1007. d = pickle.dumps(it, proto)
  1008. it = pickle.loads(d)
  1009. del data[drop[0]]
  1010. self.assertEqual(dict(it), data)
  1011. def test_reversevaluesiterator_pickling(self):
  1012. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1013. data = {1:"a", 2:"b", 3:"c"}
  1014. # data.values() isn't picklable, only its iterator
  1015. it = reversed(data.values())
  1016. d = pickle.dumps(it, proto)
  1017. it = pickle.loads(d)
  1018. self.assertEqual(list(it), list(reversed(data.values())))
  1019. it = pickle.loads(d)
  1020. drop = next(it)
  1021. d = pickle.dumps(it, proto)
  1022. it = pickle.loads(d)
  1023. values = list(it) + [drop]
  1024. self.assertEqual(sorted(values), sorted(data.values()))
  1025. def test_instance_dict_getattr_str_subclass(self):
  1026. class Foo:
  1027. def __init__(self, msg):
  1028. self.msg = msg
  1029. f = Foo('123')
  1030. class _str(str):
  1031. pass
  1032. self.assertEqual(f.msg, getattr(f, _str('msg')))
  1033. self.assertEqual(f.msg, f.__dict__[_str('msg')])
  1034. def test_object_set_item_single_instance_non_str_key(self):
  1035. class Foo: pass
  1036. f = Foo()
  1037. f.__dict__[1] = 1
  1038. f.a = 'a'
  1039. self.assertEqual(f.__dict__, {1:1, 'a':'a'})
  1040. def check_reentrant_insertion(self, mutate):
  1041. # This object will trigger mutation of the dict when replaced
  1042. # by another value. Note this relies on refcounting: the test
  1043. # won't achieve its purpose on fully-GCed Python implementations.
  1044. class Mutating:
  1045. def __del__(self):
  1046. mutate(d)
  1047. d = {k: Mutating() for k in 'abcdefghijklmnopqr'}
  1048. for k in list(d):
  1049. d[k] = k
  1050. def test_reentrant_insertion(self):
  1051. # Reentrant insertion shouldn't crash (see issue #22653)
  1052. def mutate(d):
  1053. d['b'] = 5
  1054. self.check_reentrant_insertion(mutate)
  1055. def mutate(d):
  1056. d.update(self.__dict__)
  1057. d.clear()
  1058. self.check_reentrant_insertion(mutate)
  1059. def mutate(d):
  1060. while d:
  1061. d.popitem()
  1062. self.check_reentrant_insertion(mutate)
  1063. def test_merge_and_mutate(self):
  1064. class X:
  1065. def __hash__(self):
  1066. return 0
  1067. def __eq__(self, o):
  1068. other.clear()
  1069. return False
  1070. l = [(i,0) for i in range(1, 1337)]
  1071. other = dict(l)
  1072. other[X()] = 0
  1073. d = {X(): 0, 1: 1}
  1074. self.assertRaises(RuntimeError, d.update, other)
  1075. def test_free_after_iterating(self):
  1076. support.check_free_after_iterating(self, iter, dict)
  1077. support.check_free_after_iterating(self, lambda d: iter(d.keys()), dict)
  1078. support.check_free_after_iterating(self, lambda d: iter(d.values()), dict)
  1079. support.check_free_after_iterating(self, lambda d: iter(d.items()), dict)
  1080. def test_equal_operator_modifying_operand(self):
  1081. # test fix for seg fault reported in bpo-27945 part 3.
  1082. class X():
  1083. def __del__(self):
  1084. dict_b.clear()
  1085. def __eq__(self, other):
  1086. dict_a.clear()
  1087. return True
  1088. def __hash__(self):
  1089. return 13
  1090. dict_a = {X(): 0}
  1091. dict_b = {X(): X()}
  1092. self.assertTrue(dict_a == dict_b)
  1093. # test fix for seg fault reported in bpo-38588 part 1.
  1094. class Y:
  1095. def __eq__(self, other):
  1096. dict_d.clear()
  1097. return True
  1098. dict_c = {0: Y()}
  1099. dict_d = {0: set()}
  1100. self.assertTrue(dict_c == dict_d)
  1101. def test_fromkeys_operator_modifying_dict_operand(self):
  1102. # test fix for seg fault reported in issue 27945 part 4a.
  1103. class X(int):
  1104. def __hash__(self):
  1105. return 13
  1106. def __eq__(self, other):
  1107. if len(d) > 1:
  1108. d.clear()
  1109. return False
  1110. d = {} # this is required to exist so that d can be constructed!
  1111. d = {X(1): 1, X(2): 2}
  1112. try:
  1113. dict.fromkeys(d) # shouldn't crash
  1114. except RuntimeError: # implementation defined
  1115. pass
  1116. def test_fromkeys_operator_modifying_set_operand(self):
  1117. # test fix for seg fault reported in issue 27945 part 4b.
  1118. class X(int):
  1119. def __hash__(self):
  1120. return 13
  1121. def __eq__(self, other):
  1122. if len(d) > 1:
  1123. d.clear()
  1124. return False
  1125. d = {} # this is required to exist so that d can be constructed!
  1126. d = {X(1), X(2)}
  1127. try:
  1128. dict.fromkeys(d) # shouldn't crash
  1129. except RuntimeError: # implementation defined
  1130. pass
  1131. def test_dictitems_contains_use_after_free(self):
  1132. class X:
  1133. def __eq__(self, other):
  1134. d.clear()
  1135. return NotImplemented
  1136. d = {0: set()}
  1137. (0, X()) in d.items()
  1138. def test_dict_contain_use_after_free(self):
  1139. # bpo-40489
  1140. class S(str):
  1141. def __eq__(self, other):
  1142. d.clear()
  1143. return NotImplemented
  1144. def __hash__(self):
  1145. return hash('test')
  1146. d = {S(): 'value'}
  1147. self.assertFalse('test' in d)
  1148. def test_init_use_after_free(self):
  1149. class X:
  1150. def __hash__(self):
  1151. pair[:] = []
  1152. return 13
  1153. pair = [X(), 123]
  1154. dict([pair])
  1155. def test_oob_indexing_dictiter_iternextitem(self):
  1156. class X(int):
  1157. def __del__(self):
  1158. d.clear()
  1159. d = {i: X(i) for i in range(8)}
  1160. def iter_and_mutate():
  1161. for result in d.items():
  1162. if result[0] == 2:
  1163. d[2] = None # free d[2] --> X(2).__del__ was called
  1164. self.assertRaises(RuntimeError, iter_and_mutate)
  1165. def test_reversed(self):
  1166. d = {"a": 1, "b": 2, "foo": 0, "c": 3, "d": 4}
  1167. del d["foo"]
  1168. r = reversed(d)
  1169. self.assertEqual(list(r), list('dcba'))
  1170. self.assertRaises(StopIteration, next, r)
  1171. def test_reverse_iterator_for_empty_dict(self):
  1172. # bpo-38525: reversed iterator should work properly
  1173. # empty dict is directly used for reference count test
  1174. self.assertEqual(list(reversed({})), [])
  1175. self.assertEqual(list(reversed({}.items())), [])
  1176. self.assertEqual(list(reversed({}.values())), [])
  1177. self.assertEqual(list(reversed({}.keys())), [])
  1178. # dict() and {} don't trigger the same code path
  1179. self.assertEqual(list(reversed(dict())), [])
  1180. self.assertEqual(list(reversed(dict().items())), [])
  1181. self.assertEqual(list(reversed(dict().values())), [])
  1182. self.assertEqual(list(reversed(dict().keys())), [])
  1183. def test_reverse_iterator_for_shared_shared_dicts(self):
  1184. class A:
  1185. def __init__(self, x, y):
  1186. if x: self.x = x
  1187. if y: self.y = y
  1188. self.assertEqual(list(reversed(A(1, 2).__dict__)), ['y', 'x'])
  1189. self.assertEqual(list(reversed(A(1, 0).__dict__)), ['x'])
  1190. self.assertEqual(list(reversed(A(0, 1).__dict__)), ['y'])
  1191. def test_dict_copy_order(self):
  1192. # bpo-34320
  1193. od = collections.OrderedDict([('a', 1), ('b', 2)])
  1194. od.move_to_end('a')
  1195. expected = list(od.items())
  1196. copy = dict(od)
  1197. self.assertEqual(list(copy.items()), expected)
  1198. # dict subclass doesn't override __iter__
  1199. class CustomDict(dict):
  1200. pass
  1201. pairs = [('a', 1), ('b', 2), ('c', 3)]
  1202. d = CustomDict(pairs)
  1203. self.assertEqual(pairs, list(dict(d).items()))
  1204. class CustomReversedDict(dict):
  1205. def keys(self):
  1206. return reversed(list(dict.keys(self)))
  1207. __iter__ = keys
  1208. def items(self):
  1209. return reversed(dict.items(self))
  1210. d = CustomReversedDict(pairs)
  1211. self.assertEqual(pairs[::-1], list(dict(d).items()))
  1212. @support.cpython_only
  1213. def test_dict_items_result_gc(self):
  1214. # bpo-42536: dict.items's tuple-reuse speed trick breaks the GC's
  1215. # assumptions about what can be untracked. Make sure we re-track result
  1216. # tuples whenever we reuse them.
  1217. it = iter({None: []}.items())
  1218. gc.collect()
  1219. # That GC collection probably untracked the recycled internal result
  1220. # tuple, which is initialized to (None, None). Make sure it's re-tracked
  1221. # when it's mutated and returned from __next__:
  1222. self.assertTrue(gc.is_tracked(next(it)))
  1223. @support.cpython_only
  1224. def test_dict_items_result_gc_reversed(self):
  1225. # Same as test_dict_items_result_gc above, but reversed.
  1226. it = reversed({None: []}.items())
  1227. gc.collect()
  1228. self.assertTrue(gc.is_tracked(next(it)))
  1229. def test_str_nonstr(self):
  1230. # cpython uses a different lookup function if the dict only contains
  1231. # `str` keys. Make sure the unoptimized path is used when a non-`str`
  1232. # key appears.
  1233. class StrSub(str):
  1234. pass
  1235. eq_count = 0
  1236. # This class compares equal to the string 'key3'
  1237. class Key3:
  1238. def __hash__(self):
  1239. return hash('key3')
  1240. def __eq__(self, other):
  1241. nonlocal eq_count
  1242. if isinstance(other, Key3) or isinstance(other, str) and other == 'key3':
  1243. eq_count += 1
  1244. return True
  1245. return False
  1246. key3_1 = StrSub('key3')
  1247. key3_2 = Key3()
  1248. key3_3 = Key3()
  1249. dicts = []
  1250. # Create dicts of the form `{'key1': 42, 'key2': 43, key3: 44}` in a
  1251. # bunch of different ways. In all cases, `key3` is not of type `str`.
  1252. # `key3_1` is a `str` subclass and `key3_2` is a completely unrelated
  1253. # type.
  1254. for key3 in (key3_1, key3_2):
  1255. # A literal
  1256. dicts.append({'key1': 42, 'key2': 43, key3: 44})
  1257. # key3 inserted via `dict.__setitem__`
  1258. d = {'key1': 42, 'key2': 43}
  1259. d[key3] = 44
  1260. dicts.append(d)
  1261. # key3 inserted via `dict.setdefault`
  1262. d = {'key1': 42, 'key2': 43}
  1263. self.assertEqual(d.setdefault(key3, 44), 44)
  1264. dicts.append(d)
  1265. # key3 inserted via `dict.update`
  1266. d = {'key1': 42, 'key2': 43}
  1267. d.update({key3: 44})
  1268. dicts.append(d)
  1269. # key3 inserted via `dict.__ior__`
  1270. d = {'key1': 42, 'key2': 43}
  1271. d |= {key3: 44}
  1272. dicts.append(d)
  1273. # `dict(iterable)`
  1274. def make_pairs():
  1275. yield ('key1', 42)
  1276. yield ('key2', 43)
  1277. yield (key3, 44)
  1278. d = dict(make_pairs())
  1279. dicts.append(d)
  1280. # `dict.copy`
  1281. d = d.copy()
  1282. dicts.append(d)
  1283. # dict comprehension
  1284. d = {key: 42 + i for i,key in enumerate(['key1', 'key2', key3])}
  1285. dicts.append(d)
  1286. for d in dicts:
  1287. with self.subTest(d=d):
  1288. self.assertEqual(d.get('key1'), 42)
  1289. # Try to make an object that is of type `str` and is equal to
  1290. # `'key1'`, but (at least on cpython) is a different object.
  1291. noninterned_key1 = 'ke'
  1292. noninterned_key1 += 'y1'
  1293. if support.check_impl_detail(cpython=True):
  1294. # suppress a SyntaxWarning
  1295. interned_key1 = 'key1'
  1296. self.assertFalse(noninterned_key1 is interned_key1)
  1297. self.assertEqual(d.get(noninterned_key1), 42)
  1298. self.assertEqual(d.get('key3'), 44)
  1299. self.assertEqual(d.get(key3_1), 44)
  1300. self.assertEqual(d.get(key3_2), 44)
  1301. # `key3_3` itself is definitely not a dict key, so make sure
  1302. # that `__eq__` gets called.
  1303. #
  1304. # Note that this might not hold for `key3_1` and `key3_2`
  1305. # because they might be the same object as one of the dict keys,
  1306. # in which case implementations are allowed to skip the call to
  1307. # `__eq__`.
  1308. eq_count = 0
  1309. self.assertEqual(d.get(key3_3), 44)
  1310. self.assertGreaterEqual(eq_count, 1)
  1311. class CAPITest(unittest.TestCase):
  1312. # Test _PyDict_GetItem_KnownHash()
  1313. @support.cpython_only
  1314. def test_getitem_knownhash(self):
  1315. _testcapi = import_helper.import_module('_testcapi')
  1316. dict_getitem_knownhash = _testcapi.dict_getitem_knownhash
  1317. d = {'x': 1, 'y': 2, 'z': 3}
  1318. self.assertEqual(dict_getitem_knownhash(d, 'x', hash('x')), 1)
  1319. self.assertEqual(dict_getitem_knownhash(d, 'y', hash('y')), 2)
  1320. self.assertEqual(dict_getitem_knownhash(d, 'z', hash('z')), 3)
  1321. # not a dict
  1322. self.assertRaises(SystemError, dict_getitem_knownhash, [], 1, hash(1))
  1323. # key does not exist
  1324. self.assertRaises(KeyError, dict_getitem_knownhash, {}, 1, hash(1))
  1325. class Exc(Exception): pass
  1326. class BadEq:
  1327. def __eq__(self, other):
  1328. raise Exc
  1329. def __hash__(self):
  1330. return 7
  1331. k1, k2 = BadEq(), BadEq()
  1332. d = {k1: 1}
  1333. self.assertEqual(dict_getitem_knownhash(d, k1, hash(k1)), 1)
  1334. self.assertRaises(Exc, dict_getitem_knownhash, d, k2, hash(k2))
  1335. from test import mapping_tests
  1336. class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
  1337. type2test = dict
  1338. class Dict(dict):
  1339. pass
  1340. class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
  1341. type2test = Dict
  1342. if __name__ == "__main__":
  1343. unittest.main()