问题
I'm trying to write some kind of API client for PyQt4-based app. And, following this documentation i wrote this code:
from PyQt4 import QtCore, QtNetwork
class API(QtCore.QObject):
def processResponse(self, response):
print 'response'
response.deleteLater()
def processError(self, error):
print 'error'
def authenticate(self, authUrl, login, password):
manager = QtNetwork.QNetworkAccessManager()
request = QtNetwork.QNetworkRequest(QtCore.QUrl(authUrl))
reply = manager.get(request)
reply.error.connect(self.processError)
reply.finished.connect(self.processResponse)
return reply
def authenticate2(self, authUrl, login, password):
manager = QtNetwork.QNetworkAccessManager()
request = QtNetwork.QNetworkRequest(QtCore.QUrl(authUrl))
self.reply = manager.get(request)
self.reply.error.connect(self.processError)
self.reply.finished.connect(self.processResponse)
return self.reply
p = API()
z = p.authenticate('http://dev.dentv.ru/edda/v1/token/', 'test', 'test')
print z
print z.isFinished()
The first problem is -- none of signals (processResponse
, processError
) gets called (even if I declare the connection with self.connect(.., SIGNAL(..), ..)
). The second problem is that this code causes a segfault on the last line, when i'm calling isFinished
method. If I call this method inside the API method it works properly.
I was thinking, that the cause is that reply
object get garbage-collected or somehow deleted, so i wrote the second version of authenticate
method with explicit reply
binding to object variable. It causes segfault too.
What's the problem with my code?
回答1:
I'm an idiot :(
The problem was resolved by binding the manager
to object variable, not the reply
. The manager was garbage collected (or smth like that).
来源:https://stackoverflow.com/questions/12288458/qnetworkreply-causes-segfault