mirror of
https://github.com/python/cpython.git
synced 2025-09-10 18:58:35 +00:00
gh-133703: dict: fix calculate_log2_keysize() (GH-133809)
(cherry picked from commit 92337f666e
)
This commit is contained in:
parent
26b6ab49e4
commit
f0a88e2ce5
3 changed files with 6 additions and 7 deletions
|
@ -1120,10 +1120,8 @@ class DictTest(unittest.TestCase):
|
||||||
a = C()
|
a = C()
|
||||||
a.x = 1
|
a.x = 1
|
||||||
d = a.__dict__
|
d = a.__dict__
|
||||||
before_resize = sys.getsizeof(d)
|
|
||||||
d[2] = 2 # split table is resized to a generic combined table
|
d[2] = 2 # split table is resized to a generic combined table
|
||||||
|
|
||||||
self.assertGreater(sys.getsizeof(d), before_resize)
|
|
||||||
self.assertEqual(list(d), ['x', 2])
|
self.assertEqual(list(d), ['x', 2])
|
||||||
|
|
||||||
def test_iterator_pickling(self):
|
def test_iterator_pickling(self):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix hashtable in dict can be bigger than intended in some situations.
|
|
@ -580,13 +580,13 @@ static inline uint8_t
|
||||||
calculate_log2_keysize(Py_ssize_t minsize)
|
calculate_log2_keysize(Py_ssize_t minsize)
|
||||||
{
|
{
|
||||||
#if SIZEOF_LONG == SIZEOF_SIZE_T
|
#if SIZEOF_LONG == SIZEOF_SIZE_T
|
||||||
minsize = (minsize | PyDict_MINSIZE) - 1;
|
minsize = Py_MAX(minsize, PyDict_MINSIZE);
|
||||||
return _Py_bit_length(minsize | (PyDict_MINSIZE-1));
|
return _Py_bit_length(minsize - 1);
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER)
|
||||||
// On 64bit Windows, sizeof(long) == 4.
|
// On 64bit Windows, sizeof(long) == 4. We cannot use _Py_bit_length.
|
||||||
minsize = (minsize | PyDict_MINSIZE) - 1;
|
minsize = Py_MAX(minsize, PyDict_MINSIZE);
|
||||||
unsigned long msb;
|
unsigned long msb;
|
||||||
_BitScanReverse64(&msb, (uint64_t)minsize);
|
_BitScanReverse64(&msb, (uint64_t)minsize - 1);
|
||||||
return (uint8_t)(msb + 1);
|
return (uint8_t)(msb + 1);
|
||||||
#else
|
#else
|
||||||
uint8_t log2_size;
|
uint8_t log2_size;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue