Skip to content

Commit f0a88e2

Browse files
authored
gh-133703: dict: fix calculate_log2_keysize() (GH-133809)
(cherry picked from commit 92337f6)
1 parent 26b6ab4 commit f0a88e2

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

Lib/test/test_dict.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,10 +1120,8 @@ class C:
11201120
a = C()
11211121
a.x = 1
11221122
d = a.__dict__
1123-
before_resize = sys.getsizeof(d)
11241123
d[2] = 2 # split table is resized to a generic combined table
11251124

1126-
self.assertGreater(sys.getsizeof(d), before_resize)
11271125
self.assertEqual(list(d), ['x', 2])
11281126

11291127
def test_iterator_pickling(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix hashtable in dict can be bigger than intended in some situations.

Objects/dictobject.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,13 +580,13 @@ static inline uint8_t
580580
calculate_log2_keysize(Py_ssize_t minsize)
581581
{
582582
#if SIZEOF_LONG == SIZEOF_SIZE_T
583-
minsize = (minsize | PyDict_MINSIZE) - 1;
584-
return _Py_bit_length(minsize | (PyDict_MINSIZE-1));
583+
minsize = Py_MAX(minsize, PyDict_MINSIZE);
584+
return _Py_bit_length(minsize - 1);
585585
#elif defined(_MSC_VER)
586-
// On 64bit Windows, sizeof(long) == 4.
587-
minsize = (minsize | PyDict_MINSIZE) - 1;
586+
// On 64bit Windows, sizeof(long) == 4. We cannot use _Py_bit_length.
587+
minsize = Py_MAX(minsize, PyDict_MINSIZE);
588588
unsigned long msb;
589-
_BitScanReverse64(&msb, (uint64_t)minsize);
589+
_BitScanReverse64(&msb, (uint64_t)minsize - 1);
590590
return (uint8_t)(msb + 1);
591591
#else
592592
uint8_t log2_size;

0 commit comments

Comments
 (0)