From 29de60e8968a6557ea6c36beb5bfce113e373250 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Thu, 19 Feb 2026 22:57:48 +0000 Subject: [PATCH] Relax over-strict alignment test on small arrays This alignment test was introduced in gh-140557 with the intention of preventing empty-allocation optimisations from introducing odd-aligned pointers (as had previously been observed in such an optimisation for the empty `bytearray`, used in the Pickle v5 protocol). The test was over-specified, however, and compared to the maximum platform alignment, even though the requirements on allocators in general is only that the allocation is aligned for all data types that would in the requested allocation. This test could fail on 32-bit platforms using `mimalloc` for single-byte array elements (e.g. 'B'), as the tiny allocation region only enforced 4-byte alignment, not the 8-byte maximum platform alignment of the `double` type. --- Lib/test/test_buffer.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py index ab65a44bda6e7e..87c981b7a553d1 100644 --- a/Lib/test/test_buffer.py +++ b/Lib/test/test_buffer.py @@ -4465,9 +4465,9 @@ def test_bytearray_alignment(self): @support.cpython_only @unittest.skipIf(_testcapi is None, "requires _testcapi") def test_array_alignment(self): - # gh-140557: pointer alignment of buffers including empty allocation - # should match the maximum array alignment. - align = max(struct.calcsize(fmt) for fmt in ARRAY) + # gh-140557: pointer alignment of empty buffers should be at least + # to `size_t`. + align = struct.calcsize("N") cases = [array.array(fmt) for fmt in ARRAY] # Empty arrays self.assertEqual( @@ -4476,9 +4476,11 @@ def test_array_alignment(self): ) for case in cases: case.append(0) - # Allocated arrays + # Allocated arrays. The minimum alignment is now governed by regular + # allocator rules; aligned for any type that fits in the allocation. self.assertEqual( - [_testcapi.buffer_pointer_as_int(case) % align for case in cases], + [_testcapi.buffer_pointer_as_int(case) % case.itemsize + for case in cases], [0] * len(cases), )