[PyQt] Access to QSharedMemory with buffer protocol

Phil Thompson phil at riverbankcomputing.com
Tue Nov 2 10:16:35 GMT 2010


On Tue, 2 Nov 2010 00:00:06 +0200, "Hans-Peter Jansen" <hpj at urpla.net>
wrote:
> Hi Phil,
> 
> end of July last year, Alexandre Raczynski asked about accessing 
> QSharedMemory, and you replied, that:
> 
> mutable_data = buffer(sharedMemory.data())
> 
> ...should give _write_ access to the memory.

...bad advice. I didn't realise that you can't create mutable buffers from
Python. (Although there is no reason why you shouldn't be able to.)

> Hmm, not here:
> 
> $ python
> Python 2.6 (r26:66714, Mar 30 2010, 00:30:21) 
> [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from PyQt4 import QtCore
>>>> 
>>>> shmem = QtCore.QSharedMemory("xxx")
>>>> shmem.create(10)
> True
>>>> shmem.lock()
> True
>>>> b = buffer(shmem.data())
>>>> repr(b)
> '<read-only buffer for 0xb74a37a0, size -1, offset 0 at 0xb6c0f9a0>'
>>>> b = buffer(shmem.data(), 0, 10)
>>>> repr(b)
> '<read-only buffer for 0xb74a3710, size 10, offset 0 at 0xb6c0f9c0>'
>>>> 
> 
> Both attempts to create a buffer object resulted in read-only buffers, 
> that refuse getting modified. I must be doing something wrong, but 
> there's not much room left for manoeuvring..

Looks like I need to add methods to sip.voidptr so that you can do...

shmem.data()[:] = "xyz"

Question...

Should the length of the slice (ie. 10) match the length of the data (ie.
3) (and so the above would raise an exception)? Or should the last 7 bytes
be left untouched, with no exception?

Phil

Phil


More information about the PyQt mailing list