[PyQt] QHttpMultiPart impossible to use

Andrew Suffield asuffield at suffields.me.uk
Wed Jun 20 06:21:04 BST 2012


On Tue, Jun 19, 2012 at 06:10:31PM -0700, Doogster wrote:
> On both Arch Linux (PyQt 4.9) and Windows (4.8.1, I believe), the
> attached code sample segfaults the Python interpreter.

It's probably the usual garbage-collection problem: your
QHttpMultiPart is being destroyed before the network manager tries to
send the data to the network, then it reads freed data.

Here's a fragment of some code that works:

        multipart = QtNetwork.QHttpMultiPart(QtNetwork.QHttpMultiPart.FormDataType)
        for name, value in fields.iteritems():
            part = QtNetwork.QHttpPart()
            part.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, 'text/plain; charset=utf-8')
            part.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader, 'form-data; name="%s"' % name)
            part.setBody(value.encode('utf-8'))
            multipart.append(part)
        filepart = QtNetwork.QHttpPart()
        filepart.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, file['type'])
        filepart.setHeader(QtNetwork.QNetworkRequest.ContentDispositionHeader, 'form-data; name="%s"; filename="%s"' % (file['name'], file['filename']))
        filepart.setBodyDevice(file['device'])
        multipart.append(filepart)
        reply = manager.post(request, multipart)
        # Hook multipart to the reply so that it sticks around for the lifetime of the request
        multipart.setParent(reply)


More information about the PyQt mailing list