[issue15986] memoryview: expose 'buf' attribute

Stefan Krah report at bugs.python.org
Sat Sep 27 14:51:54 CEST 2014


Stefan Krah added the comment:

Sometimes I've used memoryview slices to model pointers in C code.

memoryview could have limited number methods for pointer arithmetic on the
buf pointer. Arithmetic would return new slices, so in fact everything would
be safe with respect to bounds checking and life-cycle management.


Not completely safe of course once you pass the pointer to ctypes, but
the following is less likely to happen:

m = memoryview(b"123")
ptr = m.buf
del m
my_ctypes_func(ptr)


This is safer, since the view itself is the pointer:

m = memoryview(b"123")
my_ctypes_func(m)


Of course you could still subvert the second idiom by using ptr = int(m).

----------

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


More information about the Python-bugs-list mailing list