| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1f55caf commit 19de10d
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4447,6 +4447,41 @@ def test_bytearray_release_buffer_read_flag(self): | |||
| 4447 | 4447 | with self.assertRaises(SystemError): | |
| 4448 | 4448 | obj.__buffer__(inspect.BufferFlags.WRITE) | |
| 4449 | 4449 | ||
| 4450 | + @support.cpython_only | ||
| 4451 | + @unittest.skipIf(_testcapi is None, "requires _testcapi") | ||
| 4452 | + def test_bytearray_alignment(self): | ||
| 4453 | + # gh-140557: pointer alignment of buffers including empty allocation | ||
| 4454 | + # should be at least to `size_t`. | ||
| 4455 | + align = struct.calcsize("N") | ||
| 4456 | + cases = [ | ||
| 4457 | + bytearray(), | ||
| 4458 | + bytearray(1), | ||
| 4459 | + bytearray(b"0123456789abcdef"), | ||
| 4460 | + bytearray(16), | ||
| 4461 | + ] | ||
| 4462 | + ptrs = [_testcapi.buffer_pointer_as_int(array) for array in cases] | ||
| 4463 | + self.assertEqual([ptr % align for ptr in ptrs], [0]*len(ptrs)) | ||
| 4464 | + | ||
| 4465 | + @support.cpython_only | ||
| 4466 | + @unittest.skipIf(_testcapi is None, "requires _testcapi") | ||
| 4467 | + 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) | ||
| 4471 | + cases = [array.array(fmt) for fmt in ARRAY] | ||
| 4472 | + # Empty arrays | ||
| 4473 | + self.assertEqual( | ||
| 4474 | + [_testcapi.buffer_pointer_as_int(case) % align for case in cases], | ||
| 4475 | + [0] * len(cases), | ||
| 4476 | + ) | ||
| 4477 | + for case in cases: | ||
| 4478 | + case.append(0) | ||
| 4479 | + # Allocated arrays | ||
| 4480 | + self.assertEqual( | ||
| 4481 | + [_testcapi.buffer_pointer_as_int(case) % align for case in cases], | ||
| 4482 | + [0] * len(cases), | ||
| 4483 | + ) | ||
| 4484 | + | ||
| 4450 | 4485 | @support.cpython_only | |
| 4451 | 4486 | def test_pybuffer_size_from_format(self): | |
| 4452 | 4487 | # basic tests | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1151,6 +1151,7 @@ Per Lindqvist | |||
| 1151 | 1151 | Eric Lindvall | |
| 1152 | 1152 | Gregor Lingl | |
| 1153 | 1153 | Everett Lipman | |
| 1154 | + Jake Lishman | ||
| 1154 | 1155 | Mirko Liss | |
| 1155 | 1156 | Alexander Liu | |
| 1156 | 1157 | Hui Liu | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + :class:`bytearray` buffers now have the same alignment | ||
| 2 | + when empty as when allocated. Unaligned buffers can still be created by slicing. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + :class:`array.array` buffers now have the same alignment when empty as when | ||
| 2 | + allocated. Unaligned buffers can still be created by slicing. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -98,6 +98,27 @@ static PyTypeObject testBufType = { | |||
| 98 | 98 | .tp_members = testbuf_members | |
| 99 | 99 | }; | |
| 100 | 100 | ||
| 101 | + /* Get the pointer from a buffer-supporting object as a PyLong. | ||
| 102 | + * | ||
| 103 | + * Used to test alignment properties. */ | ||
| 104 | + static PyObject * | ||
| 105 | + buffer_pointer_as_int(PyObject *Py_UNUSED(module), PyObject *obj) | ||
| 106 | + { | ||
| 107 | + PyObject *out; | ||
| 108 | + Py_buffer view; | ||
| 109 | + if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) { | ||
| 110 | + return NULL; | ||
| 111 | + } | ||
| 112 | + out = PyLong_FromVoidPtr(view.buf); | ||
| 113 | + PyBuffer_Release(&view); | ||
| 114 | + return out; | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + static PyMethodDef test_methods[] = { | ||
| 118 | + {"buffer_pointer_as_int", buffer_pointer_as_int, METH_O}, | ||
| 119 | + {NULL}, | ||
| 120 | + }; | ||
| 121 | + | ||
| 101 | 122 | int | |
| 102 | 123 | _PyTestCapi_Init_Buffer(PyObject *m) { | |
| 103 | 124 | if (PyType_Ready(&testBufType) < 0) { | |
@@ -106,6 +127,9 @@ _PyTestCapi_Init_Buffer(PyObject *m) { | |||
| 106 | 127 | if (PyModule_AddObjectRef(m, "testBuf", (PyObject *)&testBufType)) { | |
| 107 | 128 | return -1; | |
| 108 | 129 | } | |
| 130 | + if (PyModule_AddFunctions(m, test_methods) < 0) { | ||
| 131 | + return -1; | ||
| 132 | + } | ||
| 109 | 133 | ||
| 110 | 134 | return 0; | |
| 111 | 135 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2664,7 +2664,7 @@ array_ass_subscr(PyObject *op, PyObject *item, PyObject *value) | |||
| 2664 | 2664 | } | |
| 2665 | 2665 | } | |
| 2666 | 2666 | ||
| 2667 | - static const void *emptybuf = ""; | ||
| 2667 | + static const _Py_ALIGNED_DEF(ALIGNOF_MAX_ALIGN_T, char) emptybuf[] = ""; | ||
| 2668 | 2668 | ||
| 2669 | 2669 | ||
| 2670 | 2670 | static int | |
| Back | FazBrowse Home | New Git URL |
0 commit comments