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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ctypes.Array.from_buffer segmentation fault when trying to create from array.array #69845
Comments
I'm trying to find a way to create a ctypes array from the underlying memory buffer exposed by an array.array object. The ctypes.Array.from_buffer function isn't documented, but I did find the source code in _ctypes.c around line 497. It's not clear to me where the problem might be. Code to reproduce it below: import array, ctypes
a1 = array.array('l')
a1.fromlist(range(10))
ctypes.Array.from_buffer(a1)
#Segfault |
You have to subclass ctypes.Array with a _type_ and _length_. But ctypes types also implement sequence repetition (*) to facilitate creating array types. For example: import array, ctypes
a1 = array.array('l')
a1.fromlist(range(10))
c1 = (ctypes.c_long * 10).from_buffer(a1) >>> c1[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> c1[0] = 42
>>> a1[0]
42 That said, it's way too easy to segfault this. Why not simply fail the call in this case? e.g. StgDictObject *dict = PyType_stgdict(type);
if (!dict) {
PyErr_SetString(PyExc_TypeError,
"abstract class");
return NULL;
} |
Here is a patch implementing Eryk’s suggestion, for both from_buffer() and from_buffer_copy() methods. This is exactly how it is already handled for Array.from_address(). The two buffer methods were added more recently than from_address(). |
New changeset 5f061870d49c by Martin Panter in branch '3.5': New changeset 1253ef20c947 by Martin Panter in branch '3.6': New changeset 821da4891051 by Martin Panter in branch 'default': New changeset 4de956751cf1 by Martin Panter in branch '2.7': |
Misc/NEWS
so that it is managed by towncrier #552Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: