Add ability to read bytes from objc.varlist

Issue #205 resolved
Nickolas Pohilets
created an issue

Use case: CGBitmapContextGetData returns a pointer to raw data, several MBs in size. I would like to pass it to NumPy for further processing. Currently, it gets converted into a tuple of millions of single-byte objects.

A simple solution would be adding a method def as_bytes(self, count: int) -> bytes.

Another alternative is to implement Buffer Protocol

Comments (5)

  1. Ronald Oussoren repo owner

    Adding as_bytes would be useful, and I'll look into this. I guess as_memoryview would be more useful because this could avoid copying data.

    Implementing the buffer protocol is not possible, this requires knowing the size of the buffer and that's something a varlist object doesn't do.

  2. Ronald Oussoren repo owner

    I'm adding method as_buffer(self, count: int) that returns a writable memory view object that refers to the same memory as the varlist object (for the first count elements of the list).

    This makes it possible to directly write to the underlying memory through the buffer, just like you can write to that memory through the item setter of the varlist object. The user is responsible for checking if the memory is writable by checking the API documentation.

  3. Ronald Oussoren repo owner

    This will be in PyObjC 4.0, which will be released around High Sierra's release (if I read Apple's website correct High Sierra is released sept. 25, with the current state of the 10.13 branch that means PyObjC should be released sometime next week)

  4. Log in to comment