From 9ab93bae337892b02421f8c6eca1ec34896450dc Mon Sep 17 00:00:00 2001 From: Quentin Peter Date: Fri, 22 Jul 2022 23:32:47 +0200 Subject: [PATCH] force close all files --- spyder/app/mainwindow.py | 14 +++++++++----- spyder/app/tests/test_mainwindow.py | 5 ++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/spyder/app/mainwindow.py b/spyder/app/mainwindow.py index 431a76e00ff..e65f4ccecfa 100644 --- a/spyder/app/mainwindow.py +++ b/spyder/app/mainwindow.py @@ -32,6 +32,7 @@ import sys import threading import traceback +import weakref #============================================================================== # Check requirements before proceeding @@ -674,7 +675,7 @@ def signal_handler(signum, frame=None): self.window_position = None # To keep track of the last focused widget - self.last_focused_widget = None + self.last_focused_widget = lambda: None self.previous_focused_widget = None # Server to open external files on a single instance @@ -1477,14 +1478,17 @@ def hideEvent(self, event): try: for plugin in (self.widgetlist + self.thirdparty_plugins): # TODO: Remove old API + last_focused_widget = self.last_focused_widget() + if not last_focused_widget: + continue try: # New API if plugin.get_widget().isAncestorOf( - self.last_focused_widget): + last_focused_widget): plugin.change_visibility(True) except AttributeError: # Old API - if plugin.isAncestorOf(self.last_focused_widget): + if plugin.isAncestorOf(last_focused_widget): plugin._visibility_changed(True) QMainWindow.hideEvent(self, event) @@ -1495,9 +1499,9 @@ def change_last_focused_widget(self, old, now): """To keep track of to the last focused widget""" if (now is None and QApplication.activeWindow() is not None): QApplication.activeWindow().setFocus() - self.last_focused_widget = QApplication.focusWidget() + self.last_focused_widget = weakref.ref(QApplication.focusWidget()) elif now is not None: - self.last_focused_widget = now + self.last_focused_widget = weakref.ref(now) self.previous_focused_widget = old diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py index 3cebd8bf3ac..1a688eb51ab 100644 --- a/spyder/app/tests/test_mainwindow.py +++ b/spyder/app/tests/test_mainwindow.py @@ -351,6 +351,9 @@ def main_window(request, tmpdir, qtbot): # Close editor related elements window.editor.close_all_files() + # force close all files + while window.editor.editorstacks[0].close_file(force=True): + pass for editorwindow in window.editor.editorwindows: editorwindow.close() editorstack = window.editor.get_current_editorstack() @@ -401,7 +404,7 @@ def main_window(request, tmpdir, qtbot): for o in objects: if type(o).__name__ == "ShellWidget": n_shell += 1 - # More than one because the help plugin might keep some refs + # Some widgets might still be shutting down assert n_shell < 4 if os.name == 'nt':