https://github.com/python/cpython/commit/cbf64e81b668c2bf2b8463afb4bcff1771998d1a
commit: cbf64e81b668c2bf2b8463afb4bcff1771998d1a
branch: main
author: Cody Maloney <[email protected]>
committer: DinoV <[email protected]>
date: 2026-10-21T11:46:56-07:00
summary:

gh-144725: Relax over-strict alignment test on arrays (#157530)

* gh-144725: Relax over-strict alignment test on arrays

Co-authored-by: Jake Lishman <[email protected]>

files:
M Lib/test/test_buffer.py

diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py
index 579e680448e94e..f42a71cea319b6 100644
--- a/Lib/test/test_buffer.py
+++ b/Lib/test/test_buffer.py
@@ -4519,24 +4519,18 @@ 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.
-        formats = [fmt for fmt in ARRAY
-                   if struct.calcsize(fmt) <= struct.calcsize('P')]
-        align = max(struct.calcsize(fmt) for fmt in formats)
-        cases = [array.array(fmt) for fmt in formats]
-        # Empty arrays
-        self.assertEqual(
-            [_testcapi.buffer_pointer_as_int(case) % align for case in cases],
-            [0] * len(cases),
-        )
-        for case in cases:
-            case.append(0)
-        # Allocated arrays
-        self.assertEqual(
-            [_testcapi.buffer_pointer_as_int(case) % align for case in cases],
-            [0] * len(cases),
-        )
+        # gh-140557: buffer pointers, including for empty arrays, must be
+        # aligned to at least the alignment of the element type.
+        for fmt in ARRAY:
+            with self.subTest(fmt=fmt):
+                # Alignment can differ from size, for example 'Zd' on x86-64
+                # has size 16 and alignment 8. Have calcsize add a single byte
+                # then pad until the alignment of the type but include zero
+                # instances. B = 1 byte, 0{fmt} = zero instances of fmt.
+                align = struct.calcsize(f"B0{fmt}")
+                for case in (array.array(fmt), array.array(fmt, [0])):
+                    ptr = _testcapi.buffer_pointer_as_int(case)
+                    self.assertEqual(ptr % align, 0)
 
     @support.cpython_only
     def test_pybuffer_size_from_format(self):

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to