ctypes, memory mapped files and context manager

Peter Otten __peter__ at web.de
Wed Dec 28 07:48:48 EST 2016


Hans-Peter Jansen wrote:

> Dear Peter,
> 
> thanks for taking valuable time to look into my issue.

You're welcome!

> It might be related to my distinct silliness, but the problem persists
> with your code as well. 

Unfortunately I posted the broken toy example rather than the fixed one. 
Here's the latter. Basically you have to keep a reference in the context 
manager (whether you implement it as a class or a generator doesn't matter) 
without giving another reference away to client code:

$ cat mmap_after.py
import ctypes
import mmap
import weakref

from contextlib import contextmanager

class T(ctypes.Structure):
    _fields = [("foo", ctypes.c_uint32)]


@contextmanager
def map_struct(m, n):
    m.resize(n * mmap.PAGESIZE)
    keep_me = T.from_buffer(m)
    yield weakref.proxy(keep_me)


SIZE = mmap.PAGESIZE * 2
f = open("tmp.dat", "w+b")
f.write(b"\0" * SIZE)
f.seek(0)
m = mmap.mmap(f.fileno(), mmap.PAGESIZE)

with map_struct(m, 1) as a:
    a.foo = 1
with map_struct(m, 2) as b:
    b.foo = 2
$ python3 mmap_after.py 
$ 


If that doesn't suffice to fix it I'll take another look into your real code 
later.




More information about the Python-list mailing list