Debian Codegraph @

Lib/test/test_context.py

raw
1 import sys
2 import collections.abc
3 import concurrent.futures
4 import contextvars
5 import functools
6 import gc
7 import random
8 import time
9 import unittest
10 import weakref
11 from test import support
12 from test.support import threading_helper
13
14 try:
15 from _testinternalcapi import hamt
16 except ImportError:
17 hamt = None
18
19
20 def isolated_context(func):
21 """Needed to make reftracking test mode work."""
22 @functools.wraps(func)
23 def wrapper(*args, **kwargs):
24 ctx = contextvars.Context()
25 return ctx.run(func, *args, **kwargs)
26 return wrapper
27
28
29 class ContextTest(unittest.TestCase):
30 def test_context_var_new_1(self):
31 with self.assertRaisesRegex(TypeError, 'takes exactly 1'):
32 contextvars.ContextVar()
33
34 with self.assertRaisesRegex(TypeError, 'must be a str'):
35 contextvars.ContextVar(1)
36
37 c = contextvars.ContextVar('aaa')
38 self.assertEqual(c.name, 'aaa')
39
40 with self.assertRaises(AttributeError):
41 c.name = 'bbb'
42
43 self.assertNotEqual(hash(c), hash('aaa'))
44
45 @isolated_context
46 def test_context_var_repr_1(self):
47 c = contextvars.ContextVar('a')
48 self.assertIn('a', repr(c))
49
50 c = contextvars.ContextVar('a', default=123)
51 self.assertIn('123', repr(c))
52
53 lst = []
54 c = contextvars.ContextVar('a', default=lst)
55 lst.append(c)
56 self.assertIn('...', repr(c))
57 self.assertIn('...', repr(lst))
58
59 t = c.set(1)
60 self.assertIn(repr(c), repr(t))
61 self.assertNotIn(' used ', repr(t))
62 c.reset(t)
63 self.assertIn(' used ', repr(t))
64
65 @isolated_context
66 def test_token_repr_1(self):
67 c = contextvars.ContextVar('a')
68 tok = c.set(1)
69 self.assertRegex(repr(tok),
70 r"^<Token var=<ContextVar name='a' "
71 r"at 0x[0-9a-fA-F]+> at 0x[0-9a-fA-F]+>$")
72
73 def test_context_subclassing_1(self):
74 with self.assertRaisesRegex(TypeError, 'not an acceptable base type'):
75 class MyContextVar(contextvars.ContextVar):
76 # Potentially we might want ContextVars to be subclassable.
77 pass
78
79 with self.assertRaisesRegex(TypeError, 'not an acceptable base type'):
80 class MyContext(contextvars.Context):
81 pass
82
83 with self.assertRaisesRegex(TypeError, 'not an acceptable base type'):
84 class MyToken(contextvars.Token):
85 pass
86
87 def test_context_new_1(self):
88 with self.assertRaisesRegex(TypeError, 'any arguments'):
89 contextvars.Context(1)
90 with self.assertRaisesRegex(TypeError, 'any arguments'):
91 contextvars.Context(1, a=1)
92 with self.assertRaisesRegex(TypeError, 'any arguments'):
93 contextvars.Context(a=1)
94 contextvars.Context(**{})
95
96 def test_context_new_unhashable_str_subclass(self):
97 # gh-132002: it used to crash on unhashable str subtypes.
98 class weird_str(str):
99 def __eq__(self, other):
100 pass
101
102 with self.assertRaisesRegex(TypeError, 'unhashable type'):
103 contextvars.ContextVar(weird_str())
104
105 def test_context_typerrors_1(self):
106 ctx = contextvars.Context()
107
108 with self.assertRaisesRegex(TypeError, 'ContextVar key was expected'):
109 ctx[1]
110 with self.assertRaisesRegex(TypeError, 'ContextVar key was expected'):
111 1 in ctx
112 with self.assertRaisesRegex(TypeError, 'ContextVar key was expected'):
113 ctx.get(1)
114
115 def test_context_get_context_1(self):
116 ctx = contextvars.copy_context()
117 self.assertIsInstance(ctx, contextvars.Context)
118
119 def test_context_run_1(self):
120 ctx = contextvars.Context()
121
122 with self.assertRaisesRegex(TypeError, 'missing 1 required'):
123 ctx.run()
124
125 def test_context_run_2(self):
126 ctx = contextvars.Context()
127
128 def func(*args, **kwargs):
129 kwargs['spam'] = 'foo'
130 args += ('bar',)
131 return args, kwargs
132
133 for f in (func, functools.partial(func)):
134 # partial doesn't support FASTCALL
135
136 self.assertEqual(ctx.run(f), (('bar',), {'spam': 'foo'}))
137 self.assertEqual(ctx.run(f, 1), ((1, 'bar'), {'spam': 'foo'}))
138
139 self.assertEqual(
140 ctx.run(f, a=2),
141 (('bar',), {'a': 2, 'spam': 'foo'}))
142
143 self.assertEqual(
144 ctx.run(f, 11, a=2),
145 ((11, 'bar'), {'a': 2, 'spam': 'foo'}))
146
147 a = {}
148 self.assertEqual(
149 ctx.run(f, 11, **a),
150 ((11, 'bar'), {'spam': 'foo'}))
151 self.assertEqual(a, {})
152
153 def test_context_run_3(self):
154 ctx = contextvars.Context()
155
156 def func(*args, **kwargs):
157 1 / 0
158
159 with self.assertRaises(ZeroDivisionError):
160 ctx.run(func)
161 with self.assertRaises(ZeroDivisionError):
162 ctx.run(func, 1, 2)
163 with self.assertRaises(ZeroDivisionError):
164 ctx.run(func, 1, 2, a=123)
165
166 @isolated_context
167 def test_context_run_4(self):
168 ctx1 = contextvars.Context()
169 ctx2 = contextvars.Context()
170 var = contextvars.ContextVar('var')
171
172 def func2():
173 self.assertIsNone(var.get(None))
174
175 def func1():
176 self.assertIsNone(var.get(None))
177 var.set('spam')
178 ctx2.run(func2)
179 self.assertEqual(var.get(None), 'spam')
180
181 cur = contextvars.copy_context()
182 self.assertEqual(len(cur), 1)
183 self.assertEqual(cur[var], 'spam')
184 return cur
185
186 returned_ctx = ctx1.run(func1)
187 self.assertEqual(ctx1, returned_ctx)
188 self.assertEqual(returned_ctx[var], 'spam')
189 self.assertIn(var, returned_ctx)
190
191 def test_context_run_5(self):
192 ctx = contextvars.Context()
193 var = contextvars.ContextVar('var')
194
195 def func():
196 self.assertIsNone(var.get(None))
197 var.set('spam')
198 1 / 0
199
200 with self.assertRaises(ZeroDivisionError):
201 ctx.run(func)
202
203 self.assertIsNone(var.get(None))
204
205 def test_context_run_6(self):
206 ctx = contextvars.Context()
207 c = contextvars.ContextVar('a', default=0)
208
209 def fun():
210 self.assertEqual(c.get(), 0)
211 self.assertIsNone(ctx.get(c))
212
213 c.set(42)
214 self.assertEqual(c.get(), 42)
215 self.assertEqual(ctx.get(c), 42)
216
217 ctx.run(fun)
218
219 def test_context_run_7(self):
220 ctx = contextvars.Context()
221
222 def fun():
223 with self.assertRaisesRegex(RuntimeError, 'is already entered'):
224 ctx.run(fun)
225
226 ctx.run(fun)
227
228 @isolated_context
229 def test_context_getset_1(self):
230 c = contextvars.ContextVar('c')
231 with self.assertRaises(LookupError):
232 c.get()
233
234 self.assertIsNone(c.get(None))
235
236 t0 = c.set(42)
237 self.assertEqual(c.get(), 42)
238 self.assertEqual(c.get(None), 42)
239 self.assertIs(t0.old_value, t0.MISSING)
240 self.assertIs(t0.old_value, contextvars.Token.MISSING)
241 self.assertIs(t0.var, c)
242
243 t = c.set('spam')
244 self.assertEqual(c.get(), 'spam')
245 self.assertEqual(c.get(None), 'spam')
246 self.assertEqual(t.old_value, 42)
247 c.reset(t)
248
249 self.assertEqual(c.get(), 42)
250 self.assertEqual(c.get(None), 42)
251
252 c.set('spam2')
253 with self.assertRaisesRegex(RuntimeError, 'has already been used'):
254 c.reset(t)
255 self.assertEqual(c.get(), 'spam2')
256
257 ctx1 = contextvars.copy_context()
258 self.assertIn(c, ctx1)
259
260 c.reset(t0)
261 with self.assertRaisesRegex(RuntimeError, 'has already been used'):
262 c.reset(t0)
263 self.assertIsNone(c.get(None))
264
265 self.assertIn(c, ctx1)
266 self.assertEqual(ctx1[c], 'spam2')
267 self.assertEqual(ctx1.get(c, 'aa'), 'spam2')
268 self.assertEqual(len(ctx1), 1)
269 self.assertEqual(list(ctx1.items()), [(c, 'spam2')])
270 self.assertEqual(list(ctx1.values()), ['spam2'])
271 self.assertEqual(list(ctx1.keys()), [c])
272 self.assertEqual(list(ctx1), [c])
273
274 ctx2 = contextvars.copy_context()
275 self.assertNotIn(c, ctx2)
276 with self.assertRaises(KeyError):
277 ctx2[c]
278 self.assertEqual(ctx2.get(c, 'aa'), 'aa')
279 self.assertEqual(len(ctx2), 0)
280 self.assertEqual(list(ctx2), [])
281
282 @isolated_context
283 def test_context_getset_2(self):
284 v1 = contextvars.ContextVar('v1')
285 v2 = contextvars.ContextVar('v2')
286
287 t1 = v1.set(42)
288 with self.assertRaisesRegex(ValueError, 'by a different'):
289 v2.reset(t1)
290
291 @isolated_context
292 def test_context_getset_3(self):
293 c = contextvars.ContextVar('c', default=42)
294 ctx = contextvars.Context()
295
296 def fun():
297 self.assertEqual(c.get(), 42)
298 with self.assertRaises(KeyError):
299 ctx[c]
300 self.assertIsNone(ctx.get(c))
301 self.assertEqual(ctx.get(c, 'spam'), 'spam')
302 self.assertNotIn(c, ctx)
303 self.assertEqual(list(ctx.keys()), [])
304
305 t = c.set(1)
306 self.assertEqual(list(ctx.keys()), [c])
307 self.assertEqual(ctx[c], 1)
308
309 c.reset(t)
310 self.assertEqual(list(ctx.keys()), [])
311 with self.assertRaises(KeyError):
312 ctx[c]
313
314 ctx.run(fun)
315
316 @isolated_context
317 def test_context_getset_4(self):
318 c = contextvars.ContextVar('c', default=42)
319 ctx = contextvars.Context()
320
321 tok = ctx.run(c.set, 1)
322
323 with self.assertRaisesRegex(ValueError, 'different Context'):
324 c.reset(tok)
325
326 @isolated_context
327 def test_context_getset_5(self):
328 c = contextvars.ContextVar('c', default=42)
329 c.set([])
330
331 def fun():
332 c.set([])
333 c.get().append(42)
334 self.assertEqual(c.get(), [42])
335
336 contextvars.copy_context().run(fun)
337 self.assertEqual(c.get(), [])
338
339 def test_context_copy_1(self):
340 ctx1 = contextvars.Context()
341 c = contextvars.ContextVar('c', default=42)
342
343 def ctx1_fun():
344 c.set(10)
345
346 ctx2 = ctx1.copy()
347 self.assertEqual(ctx2[c], 10)
348
349 c.set(20)
350 self.assertEqual(ctx1[c], 20)
351 self.assertEqual(ctx2[c], 10)
352
353 ctx2.run(ctx2_fun)
354 self.assertEqual(ctx1[c], 20)
355 self.assertEqual(ctx2[c], 30)
356
357 def ctx2_fun():
358 self.assertEqual(c.get(), 10)
359 c.set(30)
360 self.assertEqual(c.get(), 30)
361
362 ctx1.run(ctx1_fun)
363
364 def test_context_isinstance(self):
365 ctx = contextvars.Context()
366 self.assertIsInstance(ctx, collections.abc.Mapping)
367 self.assertTrue(issubclass(contextvars.Context, collections.abc.Mapping))
368
369 mapping_methods = (
370 '__contains__', '__eq__', '__getitem__', '__iter__', '__len__',
371 '__ne__', 'get', 'items', 'keys', 'values',
372 )
373 for name in mapping_methods:
374 with self.subTest(name=name):
375 self.assertTrue(callable(getattr(ctx, name)))
376
377 @isolated_context
378 @threading_helper.requires_working_threading()
379 def test_context_threads_1(self):
380 cvar = contextvars.ContextVar('cvar')
381
382 def sub(num):
383 for i in range(10):
384 cvar.set(num + i)
385 time.sleep(random.uniform(0.001, 0.05))
386 self.assertEqual(cvar.get(), num + i)
387 return num
388
389 tp = concurrent.futures.ThreadPoolExecutor(max_workers=10)
390 try:
391 results = list(tp.map(sub, range(10)))
392 finally:
393 tp.shutdown()
394 self.assertEqual(results, list(range(10)))
395
396 @isolated_context
397 @threading_helper.requires_working_threading()
398 def test_context_thread_inherit(self):
399 import threading
400
401 cvar = contextvars.ContextVar('cvar')
402
403 def run_context_none():
404 if sys.flags.thread_inherit_context:
405 expected = 1
406 else:
407 expected = None
408 self.assertEqual(cvar.get(None), expected)
409
410 # By default, context is inherited based on the
411 # sys.flags.thread_inherit_context option.
412 cvar.set(1)
413 thread = threading.Thread(target=run_context_none)
414 thread.start()
415 thread.join()
416
417 # Passing 'None' explicitly should have same behaviour as not
418 # passing parameter.
419 thread = threading.Thread(target=run_context_none, context=None)
420 thread.start()
421 thread.join()
422
423 # An explicit Context value can also be passed
424 custom_ctx = contextvars.Context()
425 custom_var = None
426
427 def setup_context():
428 nonlocal custom_var
429 custom_var = contextvars.ContextVar('custom')
430 custom_var.set(2)
431
432 custom_ctx.run(setup_context)
433
434 def run_custom():
435 self.assertEqual(custom_var.get(), 2)
436
437 thread = threading.Thread(target=run_custom, context=custom_ctx)
438 thread.start()
439 thread.join()
440
441 # You can also pass a new Context() object to start with an empty context
442 def run_empty():
443 with self.assertRaises(LookupError):
444 cvar.get()
445
446 thread = threading.Thread(target=run_empty, context=contextvars.Context())
447 thread.start()
448 thread.join()
449
450 def test_token_contextmanager_with_default(self):
451 ctx = contextvars.Context()
452 c = contextvars.ContextVar('c', default=42)
453
454 def fun():
455 with c.set(36):
456 self.assertEqual(c.get(), 36)
457
458 self.assertEqual(c.get(), 42)
459
460 ctx.run(fun)
461
462 def test_token_contextmanager_without_default(self):
463 ctx = contextvars.Context()
464 c = contextvars.ContextVar('c')
465
466 def fun():
467 with c.set(36):
468 self.assertEqual(c.get(), 36)
469
470 with self.assertRaisesRegex(LookupError, "<ContextVar name='c'"):
471 c.get()
472
473 ctx.run(fun)
474
475 def test_token_contextmanager_on_exception(self):
476 ctx = contextvars.Context()
477 c = contextvars.ContextVar('c', default=42)
478
479 def fun():
480 with c.set(36):
481 self.assertEqual(c.get(), 36)
482 raise ValueError("custom exception")
483
484 self.assertEqual(c.get(), 42)
485
486 with self.assertRaisesRegex(ValueError, "custom exception"):
487 ctx.run(fun)
488
489 def test_token_contextmanager_reentrant(self):
490 ctx = contextvars.Context()
491 c = contextvars.ContextVar('c', default=42)
492
493 def fun():
494 token = c.set(36)
495 with self.assertRaisesRegex(
496 RuntimeError,
497 "<Token .+ has already been used once"
498 ):
499 with token:
500 with token:
501 self.assertEqual(c.get(), 36)
502
503 self.assertEqual(c.get(), 42)
504
505 ctx.run(fun)
506
507 def test_token_contextmanager_multiple_c_set(self):
508 ctx = contextvars.Context()
509 c = contextvars.ContextVar('c', default=42)
510
511 def fun():
512 with c.set(36):
513 self.assertEqual(c.get(), 36)
514 c.set(24)
515 self.assertEqual(c.get(), 24)
516 c.set(12)
517 self.assertEqual(c.get(), 12)
518
519 self.assertEqual(c.get(), 42)
520
521 ctx.run(fun)
522
523 def test_token_contextmanager_with_explicit_reset_the_same_token(self):
524 ctx = contextvars.Context()
525 c = contextvars.ContextVar('c', default=42)
526
527 def fun():
528 with self.assertRaisesRegex(
529 RuntimeError,
530 "<Token .+ has already been used once"
531 ):
532 with c.set(36) as token:
533 self.assertEqual(c.get(), 36)
534 c.reset(token)
535
536 self.assertEqual(c.get(), 42)
537
538 self.assertEqual(c.get(), 42)
539
540 ctx.run(fun)
541
542 def test_token_contextmanager_with_explicit_reset_another_token(self):
543 ctx = contextvars.Context()
544 c = contextvars.ContextVar('c', default=42)
545
546 def fun():
547 with c.set(36):
548 self.assertEqual(c.get(), 36)
549
550 token = c.set(24)
551 self.assertEqual(c.get(), 24)
552 c.reset(token)
553 self.assertEqual(c.get(), 36)
554
555 self.assertEqual(c.get(), 42)
556
557 ctx.run(fun)
558
559 def test_context_eq_reentrant_contextvar_set(self):
560 var = contextvars.ContextVar("v")
561 ctx1 = contextvars.Context()
562 ctx2 = contextvars.Context()
563
564 class ReentrantEq:
565 def __eq__(self, other):
566 ctx1.run(lambda: var.set(object()))
567 return True
568
569 ctx1.run(var.set, ReentrantEq())
570 ctx2.run(var.set, object())
571 ctx1 == ctx2
572
573 def test_context_eq_reentrant_contextvar_set_in_hash(self):
574 var = contextvars.ContextVar("v")
575 ctx1 = contextvars.Context()
576 ctx2 = contextvars.Context()
577
578 class ReentrantHash:
579 def __hash__(self):
580 ctx1.run(lambda: var.set(object()))
581 return 0
582 def __eq__(self, other):
583 return isinstance(other, ReentrantHash)
584
585 ctx1.run(var.set, ReentrantHash())
586 ctx2.run(var.set, ReentrantHash())
587 ctx1 == ctx2
588
589
590 # HAMT Tests
591
592
593 class HashKey:
594 _crasher = None
595
596 def __init__(self, hash, name, *, error_on_eq_to=None):
597 assert hash != -1
598 self.name = name
599 self.hash = hash
600 self.error_on_eq_to = error_on_eq_to
601
602 def __repr__(self):
603 return f'<Key name:{self.name} hash:{self.hash}>'
604
605 def __hash__(self):
606 if self._crasher is not None and self._crasher.error_on_hash:
607 raise HashingError
608
609 return self.hash
610
611 def __eq__(self, other):
612 if not isinstance(other, HashKey):
613 return NotImplemented
614
615 if self._crasher is not None and self._crasher.error_on_eq:
616 raise EqError
617
618 if self.error_on_eq_to is not None and self.error_on_eq_to is other:
619 raise ValueError(f'cannot compare {self!r} to {other!r}')
620 if other.error_on_eq_to is not None and other.error_on_eq_to is self:
621 raise ValueError(f'cannot compare {other!r} to {self!r}')
622
623 return (self.name, self.hash) == (other.name, other.hash)
624
625
626 class KeyStr(str):
627 def __hash__(self):
628 if HashKey._crasher is not None and HashKey._crasher.error_on_hash:
629 raise HashingError
630 return super().__hash__()
631
632 def __eq__(self, other):
633 if HashKey._crasher is not None and HashKey._crasher.error_on_eq:
634 raise EqError
635 return super().__eq__(other)
636
637
638 class HaskKeyCrasher:
639 def __init__(self, *, error_on_hash=False, error_on_eq=False):
640 self.error_on_hash = error_on_hash
641 self.error_on_eq = error_on_eq
642
643 def __enter__(self):
644 if HashKey._crasher is not None:
645 raise RuntimeError('cannot nest crashers')
646 HashKey._crasher = self
647
648 def __exit__(self, *exc):
649 HashKey._crasher = None
650
651
652 class HashingError(Exception):
653 pass
654
655
656 class EqError(Exception):
657 pass
658
659
660 @unittest.skipIf(hamt is None, '_testinternalcapi.hamt() not available')
661 class HamtTest(unittest.TestCase):
662
663 def test_hashkey_helper_1(self):
664 k1 = HashKey(10, 'aaa')
665 k2 = HashKey(10, 'bbb')
666
667 self.assertNotEqual(k1, k2)
668 self.assertEqual(hash(k1), hash(k2))
669
670 d = dict()
671 d[k1] = 'a'
672 d[k2] = 'b'
673
674 self.assertEqual(d[k1], 'a')
675 self.assertEqual(d[k2], 'b')
676
677 def test_hamt_basics_1(self):
678 h = hamt()
679 h = None # NoQA
680
681 def test_hamt_basics_2(self):
682 h = hamt()
683 self.assertEqual(len(h), 0)
684
685 h2 = h.set('a', 'b')
686 self.assertIsNot(h, h2)
687 self.assertEqual(len(h), 0)
688 self.assertEqual(len(h2), 1)
689
690 self.assertIsNone(h.get('a'))
691 self.assertEqual(h.get('a', 42), 42)
692
693 self.assertEqual(h2.get('a'), 'b')
694
695 h3 = h2.set('b', 10)
696 self.assertIsNot(h2, h3)
697 self.assertEqual(len(h), 0)
698 self.assertEqual(len(h2), 1)
699 self.assertEqual(len(h3), 2)
700 self.assertEqual(h3.get('a'), 'b')
701 self.assertEqual(h3.get('b'), 10)
702
703 self.assertIsNone(h.get('b'))
704 self.assertIsNone(h2.get('b'))
705
706 self.assertIsNone(h.get('a'))
707 self.assertEqual(h2.get('a'), 'b')
708
709 h = h2 = h3 = None
710
711 def test_hamt_basics_3(self):
712 h = hamt()
713 o = object()
714 h1 = h.set('1', o)
715 h2 = h1.set('1', o)
716 self.assertIs(h1, h2)
717
718 def test_hamt_basics_4(self):
719 h = hamt()
720 h1 = h.set('key', [])
721 h2 = h1.set('key', [])
722 self.assertIsNot(h1, h2)
723 self.assertEqual(len(h1), 1)
724 self.assertEqual(len(h2), 1)
725 self.assertIsNot(h1.get('key'), h2.get('key'))
726
727 def test_hamt_collision_1(self):
728 k1 = HashKey(10, 'aaa')
729 k2 = HashKey(10, 'bbb')
730 k3 = HashKey(10, 'ccc')
731
732 h = hamt()
733 h2 = h.set(k1, 'a')
734 h3 = h2.set(k2, 'b')
735
736 self.assertEqual(h.get(k1), None)
737 self.assertEqual(h.get(k2), None)
738
739 self.assertEqual(h2.get(k1), 'a')
740 self.assertEqual(h2.get(k2), None)
741
742 self.assertEqual(h3.get(k1), 'a')
743 self.assertEqual(h3.get(k2), 'b')
744
745 h4 = h3.set(k2, 'cc')
746 h5 = h4.set(k3, 'aa')
747
748 self.assertEqual(h3.get(k1), 'a')
749 self.assertEqual(h3.get(k2), 'b')
750 self.assertEqual(h4.get(k1), 'a')
751 self.assertEqual(h4.get(k2), 'cc')
752 self.assertEqual(h4.get(k3), None)
753 self.assertEqual(h5.get(k1), 'a')
754 self.assertEqual(h5.get(k2), 'cc')
755 self.assertEqual(h5.get(k2), 'cc')
756 self.assertEqual(h5.get(k3), 'aa')
757
758 self.assertEqual(len(h), 0)
759 self.assertEqual(len(h2), 1)
760 self.assertEqual(len(h3), 2)
761 self.assertEqual(len(h4), 2)
762 self.assertEqual(len(h5), 3)
763
764 def test_hamt_collision_3(self):
765 # Test that iteration works with the deepest tree possible.
766 # https://github.com/python/cpython/issues/93065
767
768 C = HashKey(0b10000000_00000000_00000000_00000000, 'C')
769 D = HashKey(0b10000000_00000000_00000000_00000000, 'D')
770
771 E = HashKey(0b00000000_00000000_00000000_00000000, 'E')
772
773 h = hamt()
774 h = h.set(C, 'C')
775 h = h.set(D, 'D')
776 h = h.set(E, 'E')
777
778 # BitmapNode(size=2 count=1 bitmap=0b1):
779 # NULL:
780 # BitmapNode(size=2 count=1 bitmap=0b1):
781 # NULL:
782 # BitmapNode(size=2 count=1 bitmap=0b1):
783 # NULL:
784 # BitmapNode(size=2 count=1 bitmap=0b1):
785 # NULL:
786 # BitmapNode(size=2 count=1 bitmap=0b1):
787 # NULL:
788 # BitmapNode(size=2 count=1 bitmap=0b1):
789 # NULL:
790 # BitmapNode(size=4 count=2 bitmap=0b101):
791 # <Key name:E hash:0>: 'E'
792 # NULL:
793 # CollisionNode(size=4 id=0x107a24520):
794 # <Key name:C hash:2147483648>: 'C'
795 # <Key name:D hash:2147483648>: 'D'
796
797 self.assertEqual({k.name for k in h.keys()}, {'C', 'D', 'E'})
798
799 @support.requires_resource('cpu')
800 def test_hamt_stress(self):
801 COLLECTION_SIZE = 7000
802 TEST_ITERS_EVERY = 647
803 CRASH_HASH_EVERY = 97
804 CRASH_EQ_EVERY = 11
805 RUN_XTIMES = 3
806
807 for _ in range(RUN_XTIMES):
808 h = hamt()
809 d = dict()
810
811 for i in range(COLLECTION_SIZE):
812 key = KeyStr(i)
813
814 if not (i % CRASH_HASH_EVERY):
815 with HaskKeyCrasher(error_on_hash=True):
816 with self.assertRaises(HashingError):
817 h.set(key, i)
818
819 h = h.set(key, i)
820
821 if not (i % CRASH_EQ_EVERY):
822 with HaskKeyCrasher(error_on_eq=True):
823 with self.assertRaises(EqError):
824 h.get(KeyStr(i)) # really trigger __eq__
825
826 d[key] = i
827 self.assertEqual(len(d), len(h))
828
829 if not (i % TEST_ITERS_EVERY):
830 self.assertEqual(set(h.items()), set(d.items()))
831 self.assertEqual(len(h.items()), len(d.items()))
832
833 self.assertEqual(len(h), COLLECTION_SIZE)
834
835 for key in range(COLLECTION_SIZE):
836 self.assertEqual(h.get(KeyStr(key), 'not found'), key)
837
838 keys_to_delete = list(range(COLLECTION_SIZE))
839 random.shuffle(keys_to_delete)
840 for iter_i, i in enumerate(keys_to_delete):
841 key = KeyStr(i)
842
843 if not (iter_i % CRASH_HASH_EVERY):
844 with HaskKeyCrasher(error_on_hash=True):
845 with self.assertRaises(HashingError):
846 h.delete(key)
847
848 if not (iter_i % CRASH_EQ_EVERY):
849 with HaskKeyCrasher(error_on_eq=True):
850 with self.assertRaises(EqError):
851 h.delete(KeyStr(i))
852
853 h = h.delete(key)
854 self.assertEqual(h.get(key, 'not found'), 'not found')
855 del d[key]
856 self.assertEqual(len(d), len(h))
857
858 if iter_i == COLLECTION_SIZE // 2:
859 hm = h
860 dm = d.copy()
861
862 if not (iter_i % TEST_ITERS_EVERY):
863 self.assertEqual(set(h.keys()), set(d.keys()))
864 self.assertEqual(len(h.keys()), len(d.keys()))
865
866 self.assertEqual(len(d), 0)
867 self.assertEqual(len(h), 0)
868
869 # ============
870
871 for key in dm:
872 self.assertEqual(hm.get(str(key)), dm[key])
873 self.assertEqual(len(dm), len(hm))
874
875 for i, key in enumerate(keys_to_delete):
876 hm = hm.delete(str(key))
877 self.assertEqual(hm.get(str(key), 'not found'), 'not found')
878 dm.pop(str(key), None)
879 self.assertEqual(len(d), len(h))
880
881 if not (i % TEST_ITERS_EVERY):
882 self.assertEqual(set(h.values()), set(d.values()))
883 self.assertEqual(len(h.values()), len(d.values()))
884
885 self.assertEqual(len(d), 0)
886 self.assertEqual(len(h), 0)
887 self.assertEqual(list(h.items()), [])
888
889 def test_hamt_delete_1(self):
890 A = HashKey(100, 'A')
891 B = HashKey(101, 'B')
892 C = HashKey(102, 'C')
893 D = HashKey(103, 'D')
894 E = HashKey(104, 'E')
895 Z = HashKey(-100, 'Z')
896
897 Er = HashKey(103, 'Er', error_on_eq_to=D)
898
899 h = hamt()
900 h = h.set(A, 'a')
901 h = h.set(B, 'b')
902 h = h.set(C, 'c')
903 h = h.set(D, 'd')
904 h = h.set(E, 'e')
905
906 orig_len = len(h)
907
908 # BitmapNode(size=10 bitmap=0b111110000 id=0x10eadc618):
909 # <Key name:A hash:100>: 'a'
910 # <Key name:B hash:101>: 'b'
911 # <Key name:C hash:102>: 'c'
912 # <Key name:D hash:103>: 'd'
913 # <Key name:E hash:104>: 'e'
914
915 h = h.delete(C)
916 self.assertEqual(len(h), orig_len - 1)
917
918 with self.assertRaisesRegex(ValueError, 'cannot compare'):
919 h.delete(Er)
920
921 h = h.delete(D)
922 self.assertEqual(len(h), orig_len - 2)
923
924 h2 = h.delete(Z)
925 self.assertIs(h2, h)
926
927 h = h.delete(A)
928 self.assertEqual(len(h), orig_len - 3)
929
930 self.assertEqual(h.get(A, 42), 42)
931 self.assertEqual(h.get(B), 'b')
932 self.assertEqual(h.get(E), 'e')
933
934 def test_hamt_delete_2(self):
935 A = HashKey(100, 'A')
936 B = HashKey(201001, 'B')
937 C = HashKey(101001, 'C')
938 D = HashKey(103, 'D')
939 E = HashKey(104, 'E')
940 Z = HashKey(-100, 'Z')
941
942 Er = HashKey(201001, 'Er', error_on_eq_to=B)
943
944 h = hamt()
945 h = h.set(A, 'a')
946 h = h.set(B, 'b')
947 h = h.set(C, 'c')
948 h = h.set(D, 'd')
949 h = h.set(E, 'e')
950
951 orig_len = len(h)
952
953 # BitmapNode(size=8 bitmap=0b1110010000):
954 # <Key name:A hash:100>: 'a'
955 # <Key name:D hash:103>: 'd'
956 # <Key name:E hash:104>: 'e'
957 # NULL:
958 # BitmapNode(size=4 bitmap=0b100000000001000000000):
959 # <Key name:B hash:201001>: 'b'
960 # <Key name:C hash:101001>: 'c'
961
962 with self.assertRaisesRegex(ValueError, 'cannot compare'):
963 h.delete(Er)
964
965 h = h.delete(Z)
966 self.assertEqual(len(h), orig_len)
967
968 h = h.delete(C)
969 self.assertEqual(len(h), orig_len - 1)
970
971 h = h.delete(B)
972 self.assertEqual(len(h), orig_len - 2)
973
974 h = h.delete(A)
975 self.assertEqual(len(h), orig_len - 3)
976
977 self.assertEqual(h.get(D), 'd')
978 self.assertEqual(h.get(E), 'e')
979
980 h = h.delete(A)
981 h = h.delete(B)
982 h = h.delete(D)
983 h = h.delete(E)
984 self.assertEqual(len(h), 0)
985
986 def test_hamt_delete_3(self):
987 A = HashKey(100, 'A')
988 B = HashKey(101, 'B')
989 C = HashKey(100100, 'C')
990 D = HashKey(100100, 'D')
991 E = HashKey(104, 'E')
992
993 h = hamt()
994 h = h.set(A, 'a')
995 h = h.set(B, 'b')
996 h = h.set(C, 'c')
997 h = h.set(D, 'd')
998 h = h.set(E, 'e')
999
1000 orig_len = len(h)
1001
1002 # BitmapNode(size=6 bitmap=0b100110000):
1003 # NULL:
1004 # BitmapNode(size=4 bitmap=0b1000000000000000000001000):
1005 # <Key name:A hash:100>: 'a'
1006 # NULL:
1007 # CollisionNode(size=4 id=0x108572410):
1008 # <Key name:C hash:100100>: 'c'
1009 # <Key name:D hash:100100>: 'd'
1010 # <Key name:B hash:101>: 'b'
1011 # <Key name:E hash:104>: 'e'
1012
1013 h = h.delete(A)
1014 self.assertEqual(len(h), orig_len - 1)
1015
1016 h = h.delete(E)
1017 self.assertEqual(len(h), orig_len - 2)
1018
1019 self.assertEqual(h.get(C), 'c')
1020 self.assertEqual(h.get(B), 'b')
1021
1022 def test_hamt_delete_4(self):
1023 A = HashKey(100, 'A')
1024 B = HashKey(101, 'B')
1025 C = HashKey(100100, 'C')
1026 D = HashKey(100100, 'D')
1027 E = HashKey(100100, 'E')
1028
1029 h = hamt()
1030 h = h.set(A, 'a')
1031 h = h.set(B, 'b')
1032 h = h.set(C, 'c')
1033 h = h.set(D, 'd')
1034 h = h.set(E, 'e')
1035
1036 orig_len = len(h)
1037
1038 # BitmapNode(size=4 bitmap=0b110000):
1039 # NULL:
1040 # BitmapNode(size=4 bitmap=0b1000000000000000000001000):
1041 # <Key name:A hash:100>: 'a'
1042 # NULL:
1043 # CollisionNode(size=6 id=0x10515ef30):
1044 # <Key name:C hash:100100>: 'c'
1045 # <Key name:D hash:100100>: 'd'
1046 # <Key name:E hash:100100>: 'e'
1047 # <Key name:B hash:101>: 'b'
1048
1049 h = h.delete(D)
1050 self.assertEqual(len(h), orig_len - 1)
1051
1052 h = h.delete(E)
1053 self.assertEqual(len(h), orig_len - 2)
1054
1055 h = h.delete(C)
1056 self.assertEqual(len(h), orig_len - 3)
1057
1058 h = h.delete(A)
1059 self.assertEqual(len(h), orig_len - 4)
1060
1061 h = h.delete(B)
1062 self.assertEqual(len(h), 0)
1063
1064 def test_hamt_delete_5(self):
1065 h = hamt()
1066
1067 keys = []
1068 for i in range(17):
1069 key = HashKey(i, str(i))
1070 keys.append(key)
1071 h = h.set(key, f'val-{i}')
1072
1073 collision_key16 = HashKey(16, '18')
1074 h = h.set(collision_key16, 'collision')
1075
1076 # ArrayNode(id=0x10f8b9318):
1077 # 0::
1078 # BitmapNode(size=2 count=1 bitmap=0b1):
1079 # <Key name:0 hash:0>: 'val-0'
1080 #
1081 # ... 14 more BitmapNodes ...
1082 #
1083 # 15::
1084 # BitmapNode(size=2 count=1 bitmap=0b1):
1085 # <Key name:15 hash:15>: 'val-15'
1086 #
1087 # 16::
1088 # BitmapNode(size=2 count=1 bitmap=0b1):
1089 # NULL:
1090 # CollisionNode(size=4 id=0x10f2f5af8):
1091 # <Key name:16 hash:16>: 'val-16'
1092 # <Key name:18 hash:16>: 'collision'
1093
1094 self.assertEqual(len(h), 18)
1095
1096 h = h.delete(keys[2])
1097 self.assertEqual(len(h), 17)
1098
1099 h = h.delete(collision_key16)
1100 self.assertEqual(len(h), 16)
1101 h = h.delete(keys[16])
1102 self.assertEqual(len(h), 15)
1103
1104 h = h.delete(keys[1])
1105 self.assertEqual(len(h), 14)
1106 h = h.delete(keys[1])
1107 self.assertEqual(len(h), 14)
1108
1109 for key in keys:
1110 h = h.delete(key)
1111 self.assertEqual(len(h), 0)
1112
1113 def test_hamt_items_1(self):
1114 A = HashKey(100, 'A')
1115 B = HashKey(201001, 'B')
1116 C = HashKey(101001, 'C')
1117 D = HashKey(103, 'D')
1118 E = HashKey(104, 'E')
1119 F = HashKey(110, 'F')
1120
1121 h = hamt()
1122 h = h.set(A, 'a')
1123 h = h.set(B, 'b')
1124 h = h.set(C, 'c')
1125 h = h.set(D, 'd')
1126 h = h.set(E, 'e')
1127 h = h.set(F, 'f')
1128
1129 it = h.items()
1130 self.assertEqual(
1131 set(list(it)),
1132 {(A, 'a'), (B, 'b'), (C, 'c'), (D, 'd'), (E, 'e'), (F, 'f')})
1133
1134 def test_hamt_items_2(self):
1135 A = HashKey(100, 'A')
1136 B = HashKey(101, 'B')
1137 C = HashKey(100100, 'C')
1138 D = HashKey(100100, 'D')
1139 E = HashKey(100100, 'E')
1140 F = HashKey(110, 'F')
1141
1142 h = hamt()
1143 h = h.set(A, 'a')
1144 h = h.set(B, 'b')
1145 h = h.set(C, 'c')
1146 h = h.set(D, 'd')
1147 h = h.set(E, 'e')
1148 h = h.set(F, 'f')
1149
1150 it = h.items()
1151 self.assertEqual(
1152 set(list(it)),
1153 {(A, 'a'), (B, 'b'), (C, 'c'), (D, 'd'), (E, 'e'), (F, 'f')})
1154
1155 def test_hamt_keys_1(self):
1156 A = HashKey(100, 'A')
1157 B = HashKey(101, 'B')
1158 C = HashKey(100100, 'C')
1159 D = HashKey(100100, 'D')
1160 E = HashKey(100100, 'E')
1161 F = HashKey(110, 'F')
1162
1163 h = hamt()
1164 h = h.set(A, 'a')
1165 h = h.set(B, 'b')
1166 h = h.set(C, 'c')
1167 h = h.set(D, 'd')
1168 h = h.set(E, 'e')
1169 h = h.set(F, 'f')
1170
1171 self.assertEqual(set(list(h.keys())), {A, B, C, D, E, F})
1172 self.assertEqual(set(list(h)), {A, B, C, D, E, F})
1173
1174 def test_hamt_items_3(self):
1175 h = hamt()
1176 self.assertEqual(len(h.items()), 0)
1177 self.assertEqual(list(h.items()), [])
1178
1179 def test_hamt_eq_1(self):
1180 A = HashKey(100, 'A')
1181 B = HashKey(101, 'B')
1182 C = HashKey(100100, 'C')
1183 D = HashKey(100100, 'D')
1184 E = HashKey(120, 'E')
1185
1186 h1 = hamt()
1187 h1 = h1.set(A, 'a')
1188 h1 = h1.set(B, 'b')
1189 h1 = h1.set(C, 'c')
1190 h1 = h1.set(D, 'd')
1191
1192 h2 = hamt()
1193 h2 = h2.set(A, 'a')
1194
1195 self.assertFalse(h1 == h2)
1196 self.assertTrue(h1 != h2)
1197
1198 h2 = h2.set(B, 'b')
1199 self.assertFalse(h1 == h2)
1200 self.assertTrue(h1 != h2)
1201
1202 h2 = h2.set(C, 'c')
1203 self.assertFalse(h1 == h2)
1204 self.assertTrue(h1 != h2)
1205
1206 h2 = h2.set(D, 'd2')
1207 self.assertFalse(h1 == h2)
1208 self.assertTrue(h1 != h2)
1209
1210 h2 = h2.set(D, 'd')
1211 self.assertTrue(h1 == h2)
1212 self.assertFalse(h1 != h2)
1213
1214 h2 = h2.set(E, 'e')
1215 self.assertFalse(h1 == h2)
1216 self.assertTrue(h1 != h2)
1217
1218 h2 = h2.delete(D)
1219 self.assertFalse(h1 == h2)
1220 self.assertTrue(h1 != h2)
1221
1222 h2 = h2.set(E, 'd')
1223 self.assertFalse(h1 == h2)
1224 self.assertTrue(h1 != h2)
1225
1226 def test_hamt_eq_2(self):
1227 A = HashKey(100, 'A')
1228 Er = HashKey(100, 'Er', error_on_eq_to=A)
1229
1230 h1 = hamt()
1231 h1 = h1.set(A, 'a')
1232
1233 h2 = hamt()
1234 h2 = h2.set(Er, 'a')
1235
1236 with self.assertRaisesRegex(ValueError, 'cannot compare'):
1237 h1 == h2
1238
1239 with self.assertRaisesRegex(ValueError, 'cannot compare'):
1240 h1 != h2
1241
1242 def test_hamt_gc_1(self):
1243 A = HashKey(100, 'A')
1244
1245 h = hamt()
1246 h = h.set(0, 0) # empty HAMT node is memoized in hamt.c
1247 ref = weakref.ref(h)
1248
1249 a = []
1250 a.append(a)
1251 a.append(h)
1252 b = []
1253 a.append(b)
1254 b.append(a)
1255 h = h.set(A, b)
1256
1257 del h, a, b
1258
1259 gc.collect()
1260 gc.collect()
1261 gc.collect()
1262
1263 self.assertIsNone(ref())
1264
1265 def test_hamt_gc_2(self):
1266 A = HashKey(100, 'A')
1267 B = HashKey(101, 'B')
1268
1269 h = hamt()
1270 h = h.set(A, 'a')
1271 h = h.set(A, h)
1272
1273 ref = weakref.ref(h)
1274 hi = h.items()
1275 next(hi)
1276
1277 del h, hi
1278
1279 gc.collect()
1280 gc.collect()
1281 gc.collect()
1282
1283 self.assertIsNone(ref())
1284
1285 def test_hamt_in_1(self):
1286 A = HashKey(100, 'A')
1287 AA = HashKey(100, 'A')
1288
1289 B = HashKey(101, 'B')
1290
1291 h = hamt()
1292 h = h.set(A, 1)
1293
1294 self.assertTrue(A in h)
1295 self.assertFalse(B in h)
1296
1297 with self.assertRaises(EqError):
1298 with HaskKeyCrasher(error_on_eq=True):
1299 AA in h
1300
1301 with self.assertRaises(HashingError):
1302 with HaskKeyCrasher(error_on_hash=True):
1303 AA in h
1304
1305 def test_hamt_getitem_1(self):
1306 A = HashKey(100, 'A')
1307 AA = HashKey(100, 'A')
1308
1309 B = HashKey(101, 'B')
1310
1311 h = hamt()
1312 h = h.set(A, 1)
1313
1314 self.assertEqual(h[A], 1)
1315 self.assertEqual(h[AA], 1)
1316
1317 with self.assertRaises(KeyError):
1318 h[B]
1319
1320 with self.assertRaises(EqError):
1321 with HaskKeyCrasher(error_on_eq=True):
1322 h[AA]
1323
1324 with self.assertRaises(HashingError):
1325 with HaskKeyCrasher(error_on_hash=True):
1326 h[AA]
1327
1328
1329 if __name__ == "__main__":
1330 unittest.main()