MCPcopy Index your code
hub / github.com/python/cpython / test_dict_popstring

Method test_dict_popstring

Lib/test/test_capi/test_dict.py:559–610  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

557 # CRASHES dict_pop({"a": 1}, NULL)
558
559 def test_dict_popstring(self):
560 # Test PyDict_PopString()
561 dict_popstring = _testcapi.dict_popstring
562 dict_popstring_null = _testcapi.dict_popstring_null
563
564 for dict_type in DICT_TYPES:
565 # key present, get removed value
566 mydict = dict_type({"key": "value", "key2": "value2"})
567 self.assertEqual(dict_popstring(mydict, "key"), (1, "value"))
568 self.assertEqual(mydict, {"key2": "value2"})
569 self.assertEqual(dict_popstring(mydict, "key2"), (1, "value2"))
570 self.assertEqual(mydict, {})
571
572 # key present, ignore removed value
573 mydict = dict_type({"key": "value", "key2": "value2"})
574 self.assertEqual(dict_popstring_null(mydict, "key"), 1)
575 self.assertEqual(mydict, {"key2": "value2"})
576 self.assertEqual(dict_popstring_null(mydict, "key2"), 1)
577 self.assertEqual(mydict, {})
578
579 # key missing; empty dict has a fast path
580 mydict = dict_type()
581 self.assertEqual(dict_popstring(mydict, "key"), (0, NULL))
582 self.assertEqual(dict_popstring_null(mydict, "key"), 0)
583 mydict = dict_type({"a": 1})
584 self.assertEqual(dict_popstring(mydict, "key"), (0, NULL))
585 self.assertEqual(dict_popstring_null(mydict, "key"), 0)
586
587 # non-ASCII key
588 non_ascii = '\U0001f40d'
589 dct = dict_type({'\U0001f40d': 123})
590 self.assertEqual(dict_popstring(dct, '\U0001f40d'.encode()), (1, 123))
591 dct = dict_type({'\U0001f40d': 123})
592 self.assertEqual(dict_popstring_null(dct, '\U0001f40d'.encode()), 1)
593
594 # key error
595 mydict = dict_type({1: 2})
596 self.assertRaises(UnicodeDecodeError, dict_popstring, mydict, INVALID_UTF8)
597 self.assertRaises(UnicodeDecodeError, dict_popstring_null, mydict, INVALID_UTF8)
598
599 # wrong dict type
600 for test_type in FROZENDICT_TYPES:
601 with self.frozendict_does_not_support('deletion'):
602 dict_popstring(test_type(), "key")
603 for test_type in MAPPING_TYPES + OTHER_TYPES:
604 not_dict = test_type()
605 self.assertRaises(SystemError, dict_popstring, not_dict, "key")
606 self.assertRaises(SystemError, dict_popstring_null, not_dict, "key")
607
608 # CRASHES dict_popstring(NULL, "key")
609 # CRASHES dict_popstring({}, NULL)
610 # CRASHES dict_popstring({"a": 1}, NULL)
611
612 def test_frozendict_check(self):
613 # Test PyFrozenDict_Check()

Callers

nothing calls this directly

Calls 4

assertEqualMethod · 0.45
encodeMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected