[issue28970] ctypes.from_buffer counterpart to actively remove the mapping

Hans-Peter Jansen report at bugs.python.org
Wed Dec 14 06:36:48 EST 2016


New submission from Hans-Peter Jansen:

In an attempt of using ctypes.from_buffer() to map a structure to a memory mapped file, it is important to destroy the mapping after use, because the mmap won't be resizable or freeable correctly until then.

The best approach, I was able to came up with is using a context manager, but calling the dtor of the mapping in __exit__ is not enough, which results to code like this:

with StructMap(ctypes_struct, mm, offest) as smap:
    smap.xxx = 'blabla'
del smap # necessary, but ugly

Without the del, the "with" variable still exist in the local context, hence the mapping still exist, until it is explicitly destroyed.

I hope, that ctypes is able to (or can be made to) actively remove the mapping in the __exit__ part of the context manager.

I've put some code on stackoverflow to play with this:

http://stackoverflow.com/questions/41077696/python-ctypes-from-buffer-mapping-with-context-manager-into-memory-mapped-file

The problem seems to exist with the linux mmap implementation only.

----------
components: ctypes
messages: 283188
nosy: frispete
priority: normal
severity: normal
status: open
title: ctypes.from_buffer counterpart to actively remove the mapping
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28970>
_______________________________________


More information about the Python-bugs-list mailing list