From e00a072d15e1f2eda445157084e5f579646216ab Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Fri, 10 Nov 2017 22:39:10 -0500 Subject: [PATCH 1/2] Fix garbage collection of StyleSheetObserver objects --- qutebrowser/config/config.py | 17 +++++++++++------ tests/unit/config/test_config.py | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index b519c5354..c68c315ff 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -483,8 +483,7 @@ def set_register_stylesheet(obj, *, stylesheet=None, update=True): stylesheet: The stylesheet to use. update: Whether to update the stylesheet on config changes. """ - observer = StyleSheetObserver(obj, stylesheet=stylesheet) - observer.register(update=update) + StyleSheetObserver(obj, stylesheet, update) @functools.lru_cache() @@ -504,13 +503,19 @@ class StyleSheetObserver(QObject): _stylesheet: The stylesheet template to use. """ - def __init__(self, obj, stylesheet): - super().__init__(parent=obj) + def __init__(self, obj, stylesheet, update): + super().__init__() self._obj = obj + self.update = update + + # We only need to hang around if we are asked to update. + if self.update: + self.setParent(self._obj) if stylesheet is None: self._stylesheet = obj.STYLESHEET else: self._stylesheet = stylesheet + self._register() def _get_stylesheet(self): """Format a stylesheet based on a template. @@ -525,7 +530,7 @@ class StyleSheetObserver(QObject): """Update the stylesheet for obj.""" self._obj.setStyleSheet(self._get_stylesheet()) - def register(self, update): + def _register(self): """Do a first update and listen for more. Args: @@ -535,5 +540,5 @@ class StyleSheetObserver(QObject): log.config.vdebug("stylesheet for {}: {}".format( self._obj.__class__.__name__, qss)) self._obj.setStyleSheet(qss) - if update: + if self.update: instance.changed.connect(self._update_stylesheet) diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index 73bf76ecd..bf1969e8a 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -601,7 +601,7 @@ class StyleObj(QObject): def test_get_stylesheet(config_stub): config_stub.val.colors.hints.fg = 'magenta' observer = config.StyleSheetObserver( - StyleObj(), stylesheet="{{ conf.colors.hints.fg }}") + StyleObj(), stylesheet="{{ conf.colors.hints.fg }}", update=False) assert observer._get_stylesheet() == 'magenta' From 0f17e6633d3c913d414e895696125678dcc8de40 Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Mon, 13 Nov 2017 12:04:31 -0500 Subject: [PATCH 2/2] Stop calling _register automatically for StyleSheetObservers --- qutebrowser/config/config.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index c68c315ff..802695b8e 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -483,7 +483,8 @@ def set_register_stylesheet(obj, *, stylesheet=None, update=True): stylesheet: The stylesheet to use. update: Whether to update the stylesheet on config changes. """ - StyleSheetObserver(obj, stylesheet, update) + observer = StyleSheetObserver(obj, stylesheet, update) + observer.register() @functools.lru_cache() @@ -506,16 +507,15 @@ class StyleSheetObserver(QObject): def __init__(self, obj, stylesheet, update): super().__init__() self._obj = obj - self.update = update + self._update = update # We only need to hang around if we are asked to update. - if self.update: + if self._update: self.setParent(self._obj) if stylesheet is None: self._stylesheet = obj.STYLESHEET else: self._stylesheet = stylesheet - self._register() def _get_stylesheet(self): """Format a stylesheet based on a template. @@ -530,7 +530,7 @@ class StyleSheetObserver(QObject): """Update the stylesheet for obj.""" self._obj.setStyleSheet(self._get_stylesheet()) - def _register(self): + def register(self): """Do a first update and listen for more. Args: @@ -540,5 +540,5 @@ class StyleSheetObserver(QObject): log.config.vdebug("stylesheet for {}: {}".format( self._obj.__class__.__name__, qss)) self._obj.setStyleSheet(qss) - if self.update: + if self._update: instance.changed.connect(self._update_stylesheet)