Skip to content

Commit 5c9f0ae

Browse files
gh-133703: dict: fix calculate_log2_keysize() (GH-133809)
(cherry picked from commit 92337f6) Co-authored-by: Inada Naoki <[email protected]>
1 parent 2b761d1 commit 5c9f0ae

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
@@ -1039,10 +1039,8 @@ class C:
10391039
a = C()
10401040
a.x = 1
10411041
d = a.__dict__
1042-
before_resize = sys.getsizeof(d)
10431042
d[2] = 2 # split table is resized to a generic combined table
10441043

1045-
self.assertGreater(sys.getsizeof(d), before_resize)
10461044
self.assertEqual(list(d), ['x', 2])
10471045

10481046
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
@@ -547,13 +547,13 @@ static inline uint8_t
547547
calculate_log2_keysize(Py_ssize_t minsize)
548548
{
549549
#if SIZEOF_LONG == SIZEOF_SIZE_T
550-
minsize = (minsize | PyDict_MINSIZE) - 1;
551-
return _Py_bit_length(minsize | (PyDict_MINSIZE-1));
550+
minsize = Py_MAX(minsize, PyDict_MINSIZE);
551+
return _Py_bit_length(minsize - 1);
552552
#elif defined(_MSC_VER)
553-
// On 64bit Windows, sizeof(long) == 4.
554-
minsize = (minsize | PyDict_MINSIZE) - 1;
553+
// On 64bit Windows, sizeof(long) == 4. We cannot use _Py_bit_length.
554+
minsize = Py_MAX(minsize, PyDict_MINSIZE);
555555
unsigned long msb;
556-
_BitScanReverse64(&msb, (uint64_t)minsize);
556+
_BitScanReverse64(&msb, (uint64_t)minsize - 1);
557557
return (uint8_t)(msb + 1);
558558
#else
559559
uint8_t log2_size;

0 commit comments

Comments
 (0)