ctypes, memory mapped files and context manager

Hans-Peter Jansen hpj at urpla.net
Wed Dec 28 09:17:22 EST 2016


On Mittwoch, 28. Dezember 2016 13:48:48 Peter Otten wrote:
> 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)

Hooray, that did the trick. Great solution, thank you very much!

If you don't mind, I will mention you and your solution at the various places, 
I placed this issue over the last weeks.

You made my day, Peter!

It leaves the question on why is Python2 acting as one would expect related to 
context managers, and Python3 needs this weakref juggling. Maybe something, 
that's worth to be placed in python-dev. What do you think?

Cheers,
Pete


More information about the Python-list mailing list