Re: Pickling ctypes pointer/array

eryk sun <[email protected]> Thu, 9 Jun 2016 19:01:40 +0000
Newsgroups gmane.comp.python.ctypes
Message-ID <CACL+1av0o3Q7uCY+caQSj0NkUErCmFPDg+V5nqkSMf0bnkTLOg@mail.gmail.com>
On Thu, Jun 9, 2016 at 5:49 PM, Carlos Pita <[email protected]> wrote:
>
> ptr --string_at --> bytes --dump--> pickle
> pickle --load--> bytes --> create_string_buffer --> ptr
>
> (where I'm taking create_string_buffer as a kind of inverse of string_at).

An alternative is array_type.from_buffer_copy, but it isn't more
efficient. If a memoryview were allowed, you could use from_buffer.

> There are at least two obstacles on the way to pickling without copies:
>
> 1. Pickle is unable to serialize memoryviews.
> ...
> Now, what about bytearrays? If the underlying buffer of ctypes objects were
> get/settable as (views to) bytearrays no immutability constraint would be
> violated and we could move between python and c worlds without copying.

bytearray's __reduce_ex__ creates a bytes copy in the args tuple (see
_common_reduce in Objects/bytearrayobject.c):

    >>> bytearray(3).__reduce_ex__(3)
    (<class 'bytearray'>, (b'\x00\x00\x00',), None)

Supporting memoryview would make pickling bytearray more efficient.

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity 
planning reports. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e