You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bytearray() buffers in addition to array.array #489
Any objection to a PR to support bytearray in addition to array.array buffers? bytearray() has the advantage that you can specific the length when you create it instead of needing an initializer object.
Context is this commit which added the implicit readinto support: 1028dc6
Some methods already support this (those using _interop.as_array, e.g. Device.write).
Others currently don't support it (e.g. Device.read), and I'm not sure it's practical to change them: in the case of Device.read, internally we convert the buffer to array('B'); therefore we would need to make sure that the conversion and back are zero-copy, and actually remember to convert it back to a bytearray before returning, or if any exception is raised...
I think memoryview() would help in the conversion. (It'd also make the "returns less than requested" case zero copy.)
You could invert the test to create a buffer and only do it when given an int. Anything else you'd want to treat as something that implements the buffer protocol.
Please note that I don't intend to introduce breaking changes to this API. There's still some leeway in what can be defined as an acceptable change (since every visible side-effect of the current API definition and implementation is probably depended on by someone), but at the very least I don't intend to make programs that are unquestionably correct today break tomorrow.
Activity
jonasmalacofilho commentedon Jul 10, 2024
Some methods already support this (those using
_interop.as_array, e.g.Device.write).Others currently don't support it (e.g.
Device.read), and I'm not sure it's practical to change them: in the case ofDevice.read, internally we convert the buffer toarray('B'); therefore we would need to make sure that the conversion and back are zero-copy, and actually remember to convert it back to abytearraybefore returning, or if any exception is raised...tannewt commentedon Aug 20, 2024
I think
memoryview()would help in the conversion. (It'd also make the "returns less than requested" case zero copy.)You could invert the test to create a buffer and only do it when given an int. Anything else you'd want to treat as something that implements the buffer protocol.
jonasmalacofilho commentedon Aug 21, 2024
Can you send a draft PR?
Please note that I don't intend to introduce breaking changes to this API. There's still some leeway in what can be defined as an acceptable change (since every visible side-effect of the current API definition and implementation is probably depended on by someone), but at the very least I don't intend to make programs that are unquestionably correct today break tomorrow.