[PyQt] Access to QSharedMemory with buffer protocol

Hans-Peter Jansen hpj at urpla.net
Tue Nov 2 19:07:49 GMT 2010


On Tuesday 02 November 2010, 13:26:44 Phil Thompson wrote:
> On Tue, 02 Nov 2010 13:19:06 +0100, Giovanni Bajo <rasky at develer.com>
>
> wrote:
> > On mar, 2010-11-02 at 13:57 +0200, Hans-Peter Jansen wrote:
> >> On Tuesday 02 November 2010, 11:26:46 Giovanni Bajo wrote:
> >> > On mar, 2010-11-02 at 10:16 +0000, Phil Thompson wrote:
> >> > > 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.)
> >> >
> >> > Actually, Python 2.7 has memoryview(); if
> >> > memoryview(sharedMemory.data()) worked, it would be possible to
> >> > use memoryview's method to mutate the buffer, without having to
> >> > come up with a different API for sip.voidptr.
> >>
> >> Giovanni, isn't memoryview just a different _interface_ to the
> >> buffer protocol?
> >
> > To the best of my understanding, it is a more complete interface
> > that also allows writable buffers. I think it is the best match to
> > allow access to the "contents" of a sip.voidptr and its const
> > version.
>
> Agreed - it's implemented in the hg repo.
>
> >> Anyway, being able to use QSharedMemory with 2.7 and higher
> >> only looks like an arbitrary restriction to me. At least many of
> >> my production systems would be locked out, then. For proper
> >> support, Phil might have to do both variants of the buffer
> >> protocol anyway..
> >
> > I understand your concerns. Support for memoryview (as a silent
> > conversion to/from void* at SIP level) would still be required for
> > interoperability with libraries which don't know sip.voidptr but
> > can use/produce a memoryview.
> >
> > I was just wondering if there was a better solution than coming up
> > with a custom API for sip.voidptr for pre-2.7, but I can't see it.
>
> It's not really a custom API - just standard slice and index
> assignment.

If I'm not mistaken, it should work like:

        self.sharedMemory.data()[:] = buf.data().data()

where buf is a QtCore.QBuffer() instance, but:

File "dialog.py", line 87, in loadFromFile
    self.sharedMemory.data()[:] = buf.data().data()
TypeError: 'sip.voidptr' object does not support item assignment

with:

python: 2.6
sip: 4.12-snapshot-f9dfbda5844f
qt4: 4.7.0
pyqt4: 4.8.1

Shared memory example for the collection (when fixed) attached ;-).

Pete
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sharedmemory.tar.gz
Type: application/x-tgz
Size: 15899 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101102/1a9ec1f4/attachment-0001.bin>


More information about the PyQt mailing list