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