Wrapping a `const void *` with the buffer interface without copying

Skip to first unread message

Craig de Stigter

Mar 7, 2022, 1:43:29 AMMar 7
to cython-users
Hi folks

I'm trying to wrap a git blob class with a simple readonly buffer. The buffer should wrap the blob contents, which is exposed as a `const void*`, preferably without copying it. The latter is hard because it seems like the `buf` member of the PyBuffer struct is declared as a `void*`, so it can't accept values which are declared const.

My attempt looks like this so far:

> cdef class Blob:
> cdef unique_ptr[cppgit2_blob] thisptr
> def __getbuffer__(self, Py_buffer *buffer, int flags):
> # const void *blob::raw_contents()
> contents = <char*>deref(self.thisptr).raw_contents()
> size = deref(self.thisptr).raw_size()
> buffer.buf = contents
> buffer.format = 'c'
> buffer.internal = NULL
> buffer.itemsize = 1
> buffer.len = size;
> buffer.ndim = 1
> buffer.obj = self
> buffer.readonly = 1
> buffer.shape = [size]
> buffer.strides = [1]
> buffer.suboffsets = NULL
> def __releasebuffer__(self, Py_buffer *buffer):
> pass

And results in this compile error:

libkart.cpp:6252:47: error: assigning to 'void *' from 'const void *' discards qualifiers
  __pyx_v_contents = (*__pyx_v_self->thisptr).raw_contents();

Is there any way to avoid this? Or perhaps is there a better way to achieve this, e.g. is it possible to achieve this using the Typed Memoryviews somehow?



Mar 7, 2022, 4:24:10 AMMar 7
to cython...@googlegroups.com
I'm a bit puzzled because the cast to <char*> should do it (and does when I check myself with a simplified example) but for some reason it doesn't appear in the generated code  "__pyx_v_contents = (*__pyx_v_self->thisptr).raw_contents();". I'd expect to see
"__pyx_v_contents = (char*)(*__pyx_v_self->thisptr).raw_contents();"

I'd make sure that you're definitely compiling the code that you think you are (because I think it's right)

You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/5a9cdde5-1e47-4c78-8453-656213f41ffdn%40googlegroups.com.

Reply all
Reply to author
0 new messages