Skip to content

bytearray() buffers in addition to array.array #489

Open

Description

@tannewt

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

Activity

  1. jonasmalacofilho commented on Jul 10, 2024

    @jonasmalacofilho
    Member

    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...

  2. tannewt commented on Aug 20, 2024

    @tannewt
    Author

    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.

  3. jonasmalacofilho commented on Aug 21, 2024

    @jonasmalacofilho
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions