GrahamDumpleton / wrapt

A Python module for decorators, wrappers and monkey patching.
BSD 2-Clause "Simplified" License
2.04k stars 230 forks source link

Wrapping bytearrays under ctypes #35

Open simonzack opened 9 years ago

simonzack commented 9 years ago

The following raises TypeError: expected an object with a writable buffer interface:

ctypes.c_char.from_buffer(wrapt.ObjectProxy(bytearray(b'12345')))

This is a corner case I've encountered in my own proxy implementation which also doesn't work, and doesn't seem to be possible to wrap in pure python without copying the bytearray. Now that I've discovered wrapt which is written in C. I'm hoping it can be implemented.

GrahamDumpleton commented 9 years ago

As wrapt has a pure Python fallback, if it cannot be done in pure Python now, then wouldn't be able to be universally supported.

Anyway, I will have a dig and see what I can find.

simonzack commented 9 years ago

Thanks for considering it, I've come up with a simpler example, btw, I think it's the same underlying problem:

memoryview(wrapt.ObjectProxy(bytearray(b'12345')))
GrahamDumpleton commented 9 years ago

I don't have a good answer of what to do here. What it comes down to is that yes you can likely do it in the C version of the object proxy, but you can't do it in the pure Python code version.

I am somewhat hesitant to implement on the C code side if the same thing can't be done on the pure Python side.

If I ever got a chance, it might be interesting to validate that it can be made to work for the C variant at least, but what you do then I am not sure. One could perhaps have it off by default, but have a way of enabling the support for those who were willing to live with fact that C extension must always exist.

simonzack commented 9 years ago

have it off by default, but have a way of enabling the support for those who were willing to live with fact that C extension must always exist

That's definitely good enough to me.