Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Avoid crash at startup when trying to restore the previous session (Editor) #20674

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions spyder/plugins/editor/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2262,17 +2262,25 @@ def _convert(fname):
editorwindow,
focus=focus)
if current_editor is None:
# -- Not a valid filename:
# Not a valid filename, we need to continue.
if not osp.isfile(filename):
continue
# --

current_es = self.get_current_editorstack(editorwindow)

# Creating the editor widget in the first editorstack
# (the one that can't be destroyed), then cloning this
# editor widget in all other editorstacks:
finfo = self.editorstacks[0].load(
filename, set_current=False, add_where=add_where,
processevents=processevents)

# This can happen when it was not possible to load filename
# from disk.
# Fixes spyder-ide/spyder#20670
if finfo is None:
continue

self._clone_file_everywhere(finfo)
current_editor = current_es.set_current_filename(filename,
focus=focus)
Expand All @@ -2281,13 +2289,15 @@ def _convert(fname):
self.register_widget_shortcuts(current_editor)
current_es.analyze_script()
self.__add_recent_file(filename)

if goto is not None: # 'word' is assumed to be None as well
current_editor.go_to_line(goto[index], word=word,
start_column=start_column,
end_column=end_column)
current_editor.clearFocus()
current_editor.setFocus()
current_editor.window().raise_()

if processevents:
QApplication.processEvents()
else:
Expand Down
10 changes: 8 additions & 2 deletions spyder/plugins/editor/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2756,8 +2756,14 @@ def load(self, filename, set_current=True, add_where='end',
if processevents:
self.starting_long_process.emit(_("Loading %s...") % filename)

# Read file contents
text, enc = encoding.read(filename)
# This is necessary to avoid a crash at startup when trying to restore
# files from the previous session.
# Fixes spyder-ide/spyder#20670
try:
# Read file contents
text, enc = encoding.read(filename)
except Exception:
return

# Associate hash of file's text with its name for autosave
self.autosave.file_hashes[filename] = hash(text)
Expand Down