Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-140557: Force alignment of emptybytearray andarray.array buffers#140559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
encukou merged 11 commits intopython:mainfromjakelishman:max-align-buffers
Jan 26, 2026
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
11 commits
Select commitHold shift + click to select a range
6b37e66
Force alignment of empty `bytearray` and `array.array` buffers
jakelishmanOct 24, 2025
1fea8e5
Add tests of buffer pointer alignment
jakelishmanOct 30, 2025
b7eed2b
Make NEWS more concise
jakelishmanOct 30, 2025
dd9e50b
Merge remote-tracking branch 'python/main' into max-align-buffers
jakelishmanOct 30, 2025
9d454de
Avoid `ctypes` import on unsupported platforms
jakelishmanOct 30, 2025
de09bbe
Merge remote-tracking branch 'python/main' into max-align-buffers
jakelishmanJan 6, 2026
ad966dc
Revert Python-space `ctypes` tests
jakelishmanJan 6, 2026
b036215
Add alignment tests with `_testcapi` helper
jakelishmanJan 6, 2026
2dec490
Slacken test to match 'size_t' only
jakelishmanJan 6, 2026
92a5fff
Fixup review comments
jakelishmanJan 7, 2026
d3ca57f
Require max observable alignment in array test
jakelishmanJan 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletionsLib/test/test_buffer.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4447,6 +4447,41 @@ def test_bytearray_release_buffer_read_flag(self):
with self.assertRaises(SystemError):
obj.__buffer__(inspect.BufferFlags.WRITE)

@support.cpython_only
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_bytearray_alignment(self):
# gh-140557: pointer alignment of buffers including empty allocation
# should be at least to `size_t`.
align = struct.calcsize("N")
cases = [
bytearray(),
bytearray(1),
bytearray(b"0123456789abcdef"),
bytearray(16),
]
ptrs = [_testcapi.buffer_pointer_as_int(array) for array in cases]
self.assertEqual([ptr % align for ptr in ptrs], [0]*len(ptrs))

@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)
cases = [array.array(fmt) for fmt in ARRAY]
# 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),
)

@support.cpython_only
def test_pybuffer_size_from_format(self):
# basic tests
Expand Down
1 change: 1 addition & 0 deletionsMisc/ACKS
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1151,6 +1151,7 @@ Per Lindqvist
Eric Lindvall
Gregor Lingl
Everett Lipman
Jake Lishman
Mirko Liss
Alexander Liu
Hui Liu
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
:class:`bytearray` buffers now have the same alignment
when empty as when allocated. Unaligned buffers can still be created by slicing.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
:class:`array.array` buffers now have the same alignment when empty as when
allocated. Unaligned buffers can still be created by slicing.
24 changes: 24 additions & 0 deletionsModules/_testcapi/buffer.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -98,6 +98,27 @@ static PyTypeObject testBufType = {
.tp_members = testbuf_members
};

/* Get the pointer from a buffer-supporting object as a PyLong.
*
* Used to test alignment properties. */
static PyObject *
buffer_pointer_as_int(PyObject *Py_UNUSED(module), PyObject *obj)
{
PyObject *out;
Py_buffer view;
if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) {
return NULL;
}
out = PyLong_FromVoidPtr(view.buf);
PyBuffer_Release(&view);
return out;
}

static PyMethodDef test_methods[] = {
{"buffer_pointer_as_int", buffer_pointer_as_int, METH_O},
{NULL},
};

int
_PyTestCapi_Init_Buffer(PyObject *m) {
if (PyType_Ready(&testBufType) < 0) {
Expand All@@ -106,6 +127,9 @@ _PyTestCapi_Init_Buffer(PyObject *m) {
if (PyModule_AddObjectRef(m, "testBuf", (PyObject *)&testBufType)) {
return -1;
}
if (PyModule_AddFunctions(m, test_methods) < 0) {
return -1;
}

return 0;
}
2 changes: 1 addition & 1 deletionModules/arraymodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2655,7 +2655,7 @@ array_ass_subscr(PyObject *op, PyObject *item, PyObject *value)
}
}

static constvoid *emptybuf = "";
static const_Py_ALIGNED_DEF(ALIGNOF_MAX_ALIGN_T, char)emptybuf[] = "";


static int
Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp