Skip to content

Commit cbf64e8

Browse files
gh-144725: Relax over-strict alignment test on arrays (#157530)
* gh-144725: Relax over-strict alignment test on arrays Co-authored-by: Jake Lishman <jake@binhbar.com>
1 parent 69160f2 commit cbf64e8

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

‎Lib/test/test_buffer.py‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4519,24 +4519,18 @@ def test_bytearray_alignment(self):
45194519
@support.cpython_only
45204520
@unittest.skipIf(_testcapi is None, "requires _testcapi")
45214521
def test_array_alignment(self):
4522-
# gh-140557: pointer alignment of buffers including empty allocation
4523-
# should match the maximum array alignment.
4524-
formats = [fmt for fmt in ARRAY
4525-
if struct.calcsize(fmt) <= struct.calcsize('P')]
4526-
align = max(struct.calcsize(fmt) for fmt in formats)
4527-
cases = [array.array(fmt) for fmt in formats]
4528-
# Empty arrays
4529-
self.assertEqual(
4530-
[_testcapi.buffer_pointer_as_int(case) % align for case in cases],
4531-
[0] * len(cases),
4532-
)
4533-
for case in cases:
4534-
case.append(0)
4535-
# Allocated arrays
4536-
self.assertEqual(
4537-
[_testcapi.buffer_pointer_as_int(case) % align for case in cases],
4538-
[0] * len(cases),
4539-
)
4522+
# gh-140557: buffer pointers, including for empty arrays, must be
4523+
# aligned to at least the alignment of the element type.
4524+
for fmt in ARRAY:
4525+
with self.subTest(fmt=fmt):
4526+
# Alignment can differ from size, for example 'Zd' on x86-64
4527+
# has size 16 and alignment 8. Have calcsize add a single byte
4528+
# then pad until the alignment of the type but include zero
4529+
# instances. B = 1 byte, 0{fmt} = zero instances of fmt.
4530+
align = struct.calcsize(f"B0{fmt}")
4531+
for case in (array.array(fmt), array.array(fmt, [0])):
4532+
ptr = _testcapi.buffer_pointer_as_int(case)
4533+
self.assertEqual(ptr % align, 0)
45404534

45414535
@support.cpython_only
45424536
def test_pybuffer_size_from_format(self):

0 commit comments

Comments
 (0)