(self)
| 504 | # CRASHES mergefromseq2(NULL, {}, 0) |
| 505 | |
| 506 | def test_dict_pop(self): |
| 507 | # Test PyDict_Pop() |
| 508 | dict_pop = _testcapi.dict_pop |
| 509 | dict_pop_null = _testcapi.dict_pop_null |
| 510 | |
| 511 | for dict_type in DICT_TYPES: |
| 512 | # key present, get removed value |
| 513 | mydict = dict_type({"key": "value", "key2": "value2"}) |
| 514 | self.assertEqual(dict_pop(mydict, "key"), (1, "value")) |
| 515 | self.assertEqual(mydict, {"key2": "value2"}) |
| 516 | self.assertEqual(dict_pop(mydict, "key2"), (1, "value2")) |
| 517 | self.assertEqual(mydict, {}) |
| 518 | |
| 519 | # key present, ignore removed value |
| 520 | mydict = dict_type({"key": "value", "key2": "value2"}) |
| 521 | self.assertEqual(dict_pop_null(mydict, "key"), 1) |
| 522 | self.assertEqual(mydict, {"key2": "value2"}) |
| 523 | self.assertEqual(dict_pop_null(mydict, "key2"), 1) |
| 524 | self.assertEqual(mydict, {}) |
| 525 | |
| 526 | # key missing, expect removed value; empty dict has a fast path |
| 527 | mydict = dict_type() |
| 528 | self.assertEqual(dict_pop(mydict, "key"), (0, NULL)) |
| 529 | mydict = dict_type({"a": 1}) |
| 530 | self.assertEqual(dict_pop(mydict, "key"), (0, NULL)) |
| 531 | |
| 532 | # key missing, ignored removed value; empty dict has a fast path |
| 533 | mydict = dict_type() |
| 534 | self.assertEqual(dict_pop_null(mydict, "key"), 0) |
| 535 | mydict = dict_type({"a": 1}) |
| 536 | self.assertEqual(dict_pop_null(mydict, "key"), 0) |
| 537 | |
| 538 | # key error; don't hash key if dict is empty |
| 539 | not_hashable_key = ["list"] |
| 540 | mydict = dict_type() |
| 541 | self.assertEqual(dict_pop(mydict, not_hashable_key), (0, NULL)) |
| 542 | dict_pop(mydict, NULL) # key is not checked if dict is empty |
| 543 | mydict = dict_type({'key': 1}) |
| 544 | with self.assertRaises(TypeError): |
| 545 | dict_pop(mydict, not_hashable_key) |
| 546 | |
| 547 | # wrong dict type |
| 548 | for test_type in FROZENDICT_TYPES: |
| 549 | with self.frozendict_does_not_support('deletion'): |
| 550 | dict_pop(test_type(), "key") |
| 551 | for test_type in MAPPING_TYPES + OTHER_TYPES: |
| 552 | not_dict = test_type() |
| 553 | self.assertRaises(SystemError, dict_pop, not_dict, "key") |
| 554 | self.assertRaises(SystemError, dict_pop_null, not_dict, "key") |
| 555 | |
| 556 | # CRASHES dict_pop(NULL, "key") |
| 557 | # CRASHES dict_pop({"a": 1}, NULL) |
| 558 | |
| 559 | def test_dict_popstring(self): |
| 560 | # Test PyDict_PopString() |
nothing calls this directly
no test coverage detected