A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2017-January/147127.html below:

[Python-Dev] ctypes, memory mapped files and context manager

[Python-Dev] ctypes, memory mapped files and context managerArmin Rigo armin.rigo at gmail.com
Sun Jan 8 03:25:13 EST 2017
Hi Hans-Peter,

On 6 January 2017 at 00:28, Hans-Peter Jansen <hpj at urpla.net> wrote:
> Leaves the question, how stable this "interface" is?

Another way to jump through hoops:

    c_raw = ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p)(lambda p: p)

    addr = c_raw(ctypes.pointer(T.from_buffer(m)))
    b = ctypes.cast(addr, ctypes.POINTER(T)).contents

These lines give an object 'b' that is equivalent to
'T.from_buffer(m)', but doesn't hold any reference or any "opened
buffer" state to the original 'm'.  Your context manager can yield
that.  It should prevent all BufferErrors, at the price of segfaulting
if used incorrectly.  This means in your case that ``with
map_struct(..) as a:`` should not continue to use ``a`` after the
``with`` statement, which is pretty natural anyway.

(The same issue occurs with cffi instead of ctypes, but in this case a
simple cast is enough to detach the memoryview, instead of the hack
above.)


A bientôt,

Armin.
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4