Skip to content

Commit 6b37e66

Browse files
committed
Force alignment of empty bytearray and array.array buffers
This ensures the buffers used by the empty `bytearray` and `array.array` are aligned the same as a pointer returned by the allocator. This is a more convenient default for interop with other languages that have stricter requirements of type-safe buffers (e.g. Rust's `&[T]` type) even when empty.
1 parent 44b6eea commit 6b37e66

File tree

4 files changed

+6
-2
lines changed

4 files changed

+6
-2
lines changed

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,7 @@ Per Lindqvist
11481148
Eric Lindvall
11491149
Gregor Lingl
11501150
Everett Lipman
1151+
Jake Lishman
11511152
Mirko Liss
11521153
Alexander Liu
11531154
Hui Liu
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The empty instances of :func:`bytearray` and :func:`array.array` now produce
2+
buffers that are aligned for any data type. This is a convenience for
3+
interoperation with other languages.

Modules/arraymodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2655,7 +2655,7 @@ array_ass_subscr(PyObject *op, PyObject *item, PyObject *value)
26552655
}
26562656
}
26572657

2658-
static const void *emptybuf = "";
2658+
static const _Py_ALIGNED_DEF(ALIGNOF_MAX_ALIGN_T, char) emptybuf[] = "";
26592659

26602660

26612661
static int

Objects/bytearrayobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
1818
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
1919

2020
/* For PyByteArray_AS_STRING(). */
21-
char _PyByteArray_empty_string[] = "";
21+
_Py_ALIGNED_DEF(ALIGNOF_MAX_ALIGN_T, char) _PyByteArray_empty_string[] = "";
2222

2323
/* Helpers */
2424

0 commit comments

Comments
 (0)