diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py index f3eb79be6f8..127de082e5e 100644 --- a/spyder/app/tests/test_mainwindow.py +++ b/spyder/app/tests/test_mainwindow.py @@ -1625,7 +1625,32 @@ def test_run_code(main_window, qtbot, tmpdir): timeout=EVAL_TIMEOUT) assert shell.get_value('li') == [1, 2, 3] - # try running cell without file name + # Clean namespace + with qtbot.waitSignal(shell.executed): + shell.execute('%reset -f') + + # Wait until there are no objects in the variable explorer + qtbot.waitUntil(lambda: nsb.editor.source_model.rowCount() == 0, + timeout=EVAL_TIMEOUT) + + # Open a new file + editor.load(osp.join(LOCATION, 'script_pylint.py')) + qtbot.wait(500) + + # Re-run last cell (from the previous file). + # This is a regression for spyder-ide/spyder#23076 + with qtbot.waitSignal(shell.executed): + re_run_action.trigger() + + # We should get the same result as before + qtbot.waitUntil(lambda: nsb.editor.source_model.rowCount() == 1, + timeout=EVAL_TIMEOUT) + assert shell.get_value('li') == [1, 2, 3] + + # Close new file + editor.close_file() + + # Try running cell without file name shell.clear() # Clean namespace diff --git a/spyder/plugins/editor/widgets/main_widget.py b/spyder/plugins/editor/widgets/main_widget.py index 228ab39907c..7a822eb81e2 100644 --- a/spyder/plugins/editor/widgets/main_widget.py +++ b/spyder/plugins/editor/widgets/main_widget.py @@ -3109,13 +3109,12 @@ def get_run_configuration_per_context( self, context, extra_action_name, context_modificator, re_run=False ) -> Optional[RunConfiguration]: editorstack = self.get_current_editorstack() - fname = self.get_current_filename() - __, filename_ext = osp.splitext(fname) - fname_ext = filename_ext[1:] run_input = {} context_name = None if context == RunContext.Selection: + fname = self.get_current_filename() + if context_modificator == SelectionContextModificator.ToLine: to_current_line = editorstack.get_to_current_line() if to_current_line is not None: @@ -3144,8 +3143,11 @@ def get_run_configuration_per_context( elif context == RunContext.Cell: if re_run: info = editorstack.get_last_cell() + fname = editorstack.last_cell_call[0] else: info = editorstack.get_current_cell() + fname = self.get_current_filename() + text, offsets, line_cols, cell_name, enc = info context_name = 'Cell' copy_cell = self.get_conf('run_cell_copy', section='run') @@ -3157,6 +3159,9 @@ def get_run_configuration_per_context( if extra_action_name == ExtraAction.Advance: editorstack.advance_cell() + __, filename_ext = osp.splitext(fname) + fname_ext = filename_ext[1:] + metadata: RunConfigurationMetadata = { 'name': fname, 'source': self._plugin.NAME, diff --git a/spyder/plugins/run/container.py b/spyder/plugins/run/container.py index b9ae3b5fc89..400eacca75d 100644 --- a/spyder/plugins/run/container.py +++ b/spyder/plugins/run/container.py @@ -124,6 +124,7 @@ def setup(self): self.last_executed_file: Optional[str] = None self.last_executed_per_context: Set[Tuple[str, str]] = set() + self._last_executed_configuration: Optional[str] = None def update_actions(self): pass @@ -143,12 +144,25 @@ def anonymous_execution_run(): if self.currently_selected_configuration is None: return - input_provider = self.run_metadata_provider[ - self.currently_selected_configuration] + # We save the last executed configuration in case we need it to + # re-run the last cell with the same parameters. + # This is necessary for spyder-ide/spyder#23076 + if not re_run: + uuid = self.currently_selected_configuration + self._last_executed_configuration = ( + self.currently_selected_configuration + ) + else: + uuid = self._last_executed_configuration + + # If nothing has been executed so far, we can't re-run it. + if uuid is None: + return + + input_provider = self.run_metadata_provider[uuid] if context in self.super_contexts: - run_conf = input_provider.get_run_configuration( - self.currently_selected_configuration) + run_conf = input_provider.get_run_configuration(uuid) else: run_conf = input_provider.get_run_configuration_per_context( context, extra_action_name, context_modificator, @@ -157,7 +171,6 @@ def anonymous_execution_run(): if run_conf is None: return - uuid = self.currently_selected_configuration super_metadata = self.metadata_model[uuid] extension = super_metadata['input_extension']