From 3aefe852714598cc9bb28bd26fb909d763575aa4 Mon Sep 17 00:00:00 2001 From: Gilles Dartiguelongue Date: Wed, 28 Dec 2016 14:05:15 +0100 Subject: [PATCH] Do not use lambda to declare signal callbacks As written at [1], this might be a cause for the segfaults observed at interpreter shutdown time. [1] http://enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html --- ghost/ghost.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ghost/ghost.py b/ghost/ghost.py index e5ca9ba..c44e76b 100755 --- a/ghost/ghost.py +++ b/ghost/ghost.py @@ -7,7 +7,7 @@ import logging import subprocess import re -from functools import wraps +from functools import partial, wraps try: from cookielib import Cookie, LWPCookieJar except ImportError: @@ -235,6 +235,12 @@ def reply_ready_read(reply): reply.readAll() +def reply_download_progress(reply, received, total): + """Log `reply` download progress.""" + reply.manager().logger.debug('Downloading content of %s: %s of %s', + reply.url().toString(), received, total) + + class NetworkAccessManager(QNetworkAccessManager): """Subclass QNetworkAccessManager to always cache the reply content @@ -248,8 +254,7 @@ def __init__(self, exclude_regex=None, logger=None, *args, **kwargs): # Keep a registry of in-flight requests self._registry = {} - self.finished.connect(lambda reply: - self._reply_finished_callback(reply)) + self.finished.connect(self._reply_finished_callback) def createRequest(self, operation, request, data): """Create a new QNetworkReply.""" @@ -266,12 +271,9 @@ def createRequest(self, operation, request, data): request, data ) - reply.readyRead.connect(lambda reply=reply: reply_ready_peek(reply)) - + reply.readyRead.connect(partial(reply_ready_peek, reply)) reply.downloadProgress.connect( - lambda received, total: - self.logger.debug('Downloading content of %s: %s of %s', - reply.url(), received, total) + partial(reply_download_progress, reply) ) self.logger.debug('Registring reply for %s', reply.url())