From f11906fb983e96f6714ca05ca5fbac8bdce283fe Mon Sep 17 00:00:00 2001 From: dalthviz Date: Tue, 26 Sep 2017 20:44:21 -0500 Subject: [PATCH 1/2] Adds handling for FileNotFoundError when changing directories. --- spyder/plugins/workingdirectory.py | 23 ++++++++++++++++------- spyder/widgets/explorer.py | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/spyder/plugins/workingdirectory.py b/spyder/plugins/workingdirectory.py index 94f3e3612b4..4a7c26767f8 100644 --- a/spyder/plugins/workingdirectory.py +++ b/spyder/plugins/workingdirectory.py @@ -283,11 +283,20 @@ def chdir(self, directory, browsing_history=False, self.histindex = len(self.history)-1 # Changing working directory - os.chdir(directory) + try: + FileNotFoundError + except NameError: + if os.name == 'nt': + FileNotFoundError = WindowsError + else: + FileNotFoundError = IOError + try: + os.chdir(directory) + if refresh_explorer: + self.set_explorer_cwd.emit(directory) + if refresh_console: + self.set_current_console_wd.emit(directory) + self.refresh_findinfiles.emit() + except FileNotFoundError: + self.history.pop(self.histindex) self.refresh_plugin() - - if refresh_explorer: - self.set_explorer_cwd.emit(directory) - if refresh_console: - self.set_current_console_wd.emit(directory) - self.refresh_findinfiles.emit() diff --git a/spyder/widgets/explorer.py b/spyder/widgets/explorer.py index 5ddad79fd1f..a5e7b55c4bd 100644 --- a/spyder/widgets/explorer.py +++ b/spyder/widgets/explorer.py @@ -35,7 +35,7 @@ # Local imports from spyder.config.base import _ from spyder.py3compat import (getcwd, str_lower, to_binary_string, - to_text_string, PY2) + to_text_string) from spyder.utils import icon_manager as ima from spyder.utils import encoding, misc, programs, vcs from spyder.utils.qthelpers import (add_actions, create_action, file_uri, @@ -1126,8 +1126,15 @@ def chdir(self, directory=None, browsing_history=False): self.history.append(directory) self.histindex = len(self.history)-1 directory = to_text_string(directory) - if PY2: + try: + PermissionError + FileNotFoundError + except NameError: PermissionError = OSError + if os.name == 'nt': + FileNotFoundError = WindowsError + else: + FileNotFoundError = IOError try: os.chdir(directory) self.parent_widget.open_dir.emit(directory) @@ -1136,6 +1143,9 @@ def chdir(self, directory=None, browsing_history=False): QMessageBox.critical(self.parent_widget, "Error", _("You don't have the right permissions to " "open this directory")) + except FileNotFoundError: + # Handle renaming directories on the fly. See issue #5183 + self.history.pop(self.histindex) class ExplorerWidget(QWidget): From e62893ddd67dca35ed812dd293de10ee1e4b29ba Mon Sep 17 00:00:00 2001 From: dalthviz Date: Thu, 28 Sep 2017 13:41:42 -0500 Subject: [PATCH 2/2] Improvements. --- spyder/plugins/workingdirectory.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/spyder/plugins/workingdirectory.py b/spyder/plugins/workingdirectory.py index 4a7c26767f8..cae7b501de1 100644 --- a/spyder/plugins/workingdirectory.py +++ b/spyder/plugins/workingdirectory.py @@ -283,13 +283,6 @@ def chdir(self, directory, browsing_history=False, self.histindex = len(self.history)-1 # Changing working directory - try: - FileNotFoundError - except NameError: - if os.name == 'nt': - FileNotFoundError = WindowsError - else: - FileNotFoundError = IOError try: os.chdir(directory) if refresh_explorer: @@ -297,6 +290,6 @@ def chdir(self, directory, browsing_history=False, if refresh_console: self.set_current_console_wd.emit(directory) self.refresh_findinfiles.emit() - except FileNotFoundError: + except OSError: self.history.pop(self.histindex) self.refresh_plugin()