Skip to content

Commit 29de60e

Browse files
committed
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.
1 parent 4141f0a commit 29de60e

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Lib/test/test_buffer.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4465,9 +4465,9 @@ def test_bytearray_alignment(self):
44654465
@support.cpython_only
44664466
@unittest.skipIf(_testcapi is None, "requires _testcapi")
44674467
def test_array_alignment(self):
4468-
# gh-140557: pointer alignment of buffers including empty allocation
4469-
# should match the maximum array alignment.
4470-
align = max(struct.calcsize(fmt) for fmt in ARRAY)
4468+
# gh-140557: pointer alignment of empty buffers should be at least
4469+
# to `size_t`.
4470+
align = struct.calcsize("N")
44714471
cases = [array.array(fmt) for fmt in ARRAY]
44724472
# Empty arrays
44734473
self.assertEqual(
@@ -4476,9 +4476,11 @@ def test_array_alignment(self):
44764476
)
44774477
for case in cases:
44784478
case.append(0)
4479-
# Allocated arrays
4479+
# Allocated arrays. The minimum alignment is now governed by regular
4480+
# allocator rules; aligned for any type that fits in the allocation.
44804481
self.assertEqual(
4481-
[_testcapi.buffer_pointer_as_int(case) % align for case in cases],
4482+
[_testcapi.buffer_pointer_as_int(case) % case.itemsize
4483+
for case in cases],
44824484
[0] * len(cases),
44834485
)
44844486

0 commit comments

Comments
 (0)