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

Method test_getitem_knownhash

Lib/test/test_dict.py:1771–1795  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1769 # Test _PyDict_GetItem_KnownHash()
1770 @support.cpython_only
1771 def test_getitem_knownhash(self):
1772 _testinternalcapi = import_helper.import_module('_testinternalcapi')
1773 dict_getitem_knownhash = _testinternalcapi.dict_getitem_knownhash
1774
1775 d = {'x': 1, 'y': 2, 'z': 3}
1776 self.assertEqual(dict_getitem_knownhash(d, 'x', hash('x')), 1)
1777 self.assertEqual(dict_getitem_knownhash(d, 'y', hash('y')), 2)
1778 self.assertEqual(dict_getitem_knownhash(d, 'z', hash('z')), 3)
1779
1780 # not a dict
1781 self.assertRaises(SystemError, dict_getitem_knownhash, [], 1, hash(1))
1782 # key does not exist
1783 self.assertRaises(KeyError, dict_getitem_knownhash, {}, 1, hash(1))
1784
1785 class Exc(Exception): pass
1786 class BadEq:
1787 def __eq__(self, other):
1788 raise Exc
1789 def __hash__(self):
1790 return 7
1791
1792 k1, k2 = BadEq(), BadEq()
1793 d = {k1: 1}
1794 self.assertEqual(dict_getitem_knownhash(d, k1, hash(k1)), 1)
1795 self.assertRaises(Exc, dict_getitem_knownhash, d, k2, hash(k2))
1796
1797
1798from test import mapping_tests

Callers

nothing calls this directly

Calls 4

BadEqClass · 0.70
import_moduleMethod · 0.45
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected