From 0530999b7f562bbf9a4a8a684f7a10bf8133c288 Mon Sep 17 00:00:00 2001 From: Ryan Clary <9618975+mrclary@users.noreply.github.com> Date: Mon, 10 Mar 2025 14:19:45 -0700 Subject: [PATCH 1/5] Change running_under_pytest to running_in_ci because local tests use an interactive shell that results in a SIGSTP. --- spyder/utils/environ.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spyder/utils/environ.py b/spyder/utils/environ.py index 74c9ea3b388..0aa60bd997f 100644 --- a/spyder/utils/environ.py +++ b/spyder/utils/environ.py @@ -27,9 +27,7 @@ from qtpy.QtWidgets import QMessageBox # Local imports -from spyder.config.base import ( - _, running_in_ci, get_conf_path, running_under_pytest -) +from spyder.config.base import _, running_in_ci, get_conf_path from spyder.widgets.collectionseditor import CollectionsEditor from spyder.utils.icon_manager import ima from spyder.utils.programs import run_shell_command @@ -113,7 +111,7 @@ def get_user_environment_variables(): # We only need to do this if Spyder was **not** launched from a # terminal. Otherwise, it'll inherit the env vars present in it. # Fixes spyder-ide/spyder#22415 - if not launched_from_terminal or running_under_pytest(): + if not launched_from_terminal or running_in_ci(): try: user_env_script = _get_user_env_script() proc = run_shell_command(user_env_script, env={}, text=True) From 69ffde5b558e89b4c437782e608496fff498cb74 Mon Sep 17 00:00:00 2001 From: Ryan Clary <9618975+mrclary@users.noreply.github.com> Date: Tue, 11 Mar 2025 10:54:27 -0700 Subject: [PATCH 2/5] Specify timeouts for qtbot.wait* commands to avoid indefinite hangs. Timeouts do not seem to be honored. ctrl-c still indicates test_dedicated_consoles is hanging at "with qtbot.waitSignal(shell.executed". --- spyder/app/tests/test_mainwindow.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py index 7e5ce9f813e..c0bae190f85 100644 --- a/spyder/app/tests/test_mainwindow.py +++ b/spyder/app/tests/test_mainwindow.py @@ -860,7 +860,7 @@ def test_dedicated_consoles(main_window, qtbot): == "script.py/A" ) - qtbot.waitUntil(lambda: nsb.editor.source_model.rowCount() == 4) + qtbot.waitUntil(lambda: nsb.editor.source_model.rowCount() == 4, timeout=SHELL_TIMEOUT) assert nsb.editor.source_model.rowCount() == 4 # --- Assert runfile text is present and we show the banner --- @@ -868,11 +868,11 @@ def test_dedicated_consoles(main_window, qtbot): assert ('runfile' in text) and ('Python' in text and 'IPython' in text) # --- Check namespace retention after re-execution --- - with qtbot.waitSignal(shell.executed): + with qtbot.waitSignal(shell.executed, timeout=SHELL_TIMEOUT): shell.execute('zz = -1') qtbot.keyClick(code_editor, Qt.Key_F5) - qtbot.waitUntil(lambda: shell.is_defined('zz')) + qtbot.waitUntil(lambda: shell.is_defined('zz'), timeout=SHELL_TIMEOUT) assert shell.is_defined('zz') # --- Assert runfile text is present after reruns and there's no banner @@ -886,7 +886,7 @@ def test_dedicated_consoles(main_window, qtbot): qtbot.wait(500) qtbot.keyClick(code_editor, Qt.Key_F5) - qtbot.waitUntil(lambda: not shell.is_defined('zz')) + qtbot.waitUntil(lambda: not shell.is_defined('zz'), timeout=SHELL_TIMEOUT) assert not shell.is_defined('zz') # --- Assert runfile text is present after reruns --- From 322a955ccece0f0bdd3291893d8c792544d77cd6 Mon Sep 17 00:00:00 2001 From: Ryan Clary <9618975+mrclary@users.noreply.github.com> Date: Tue, 11 Mar 2025 08:28:28 -0700 Subject: [PATCH 3/5] Create setter for SpyderKernelSpec.env property and call get_user_environment_variables once at instantiation, rather than every time SpyderKernelSpec.env is referenced. This should improve performance of starting new consoles since user_env.sh will not be executed as frequently. A setter is required in order to remove PYTEST_CURRENT_TEST from the meta property SpyderKernelSpec._env_vars, because CachedKernelMixin compares SpyderKernelSpec.__dict__ to validate cache. --- spyder/plugins/ipythonconsole/utils/kernelspec.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spyder/plugins/ipythonconsole/utils/kernelspec.py b/spyder/plugins/ipythonconsole/utils/kernelspec.py index b6083bcf475..ccde9680a1c 100644 --- a/spyder/plugins/ipythonconsole/utils/kernelspec.py +++ b/spyder/plugins/ipythonconsole/utils/kernelspec.py @@ -90,6 +90,8 @@ def __init__(self, path_to_custom_interpreter=None, self.language = 'python3' self.resource_dir = '' + self.env = get_user_environment_variables() + @property def argv(self): """Command to start kernels""" @@ -201,7 +203,7 @@ def env(self): # Ensure that user environment variables are included, but don't # override existing environ values - env_vars = get_user_environment_variables() + env_vars = self._env_vars.copy() env_vars.update(os.environ) # Avoid IPython adding the virtualenv on which Spyder is running @@ -257,3 +259,8 @@ def env(self): clean_env_vars = clean_env(env_vars) return clean_env_vars + + @env.setter + def env(self, env_vars): + self._env_vars = dict(env_vars) + self._env_vars.pop('PYTEST_CURRENT_TEST', None) From 78dbfe8116e4d76f8b57cd10d072fbe949f09b85 Mon Sep 17 00:00:00 2001 From: Ryan Clary <9618975+mrclary@users.noreply.github.com> Date: Tue, 11 Mar 2025 08:35:08 -0700 Subject: [PATCH 4/5] Increase timeout for executing user_env.sh. The purpose of the timeout is to guard against indefinite hangs; most users will never experience this limit. --- spyder/utils/environ.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spyder/utils/environ.py b/spyder/utils/environ.py index 0aa60bd997f..e897ddb500c 100644 --- a/spyder/utils/environ.py +++ b/spyder/utils/environ.py @@ -117,9 +117,7 @@ def get_user_environment_variables(): proc = run_shell_command(user_env_script, env={}, text=True) # Use timeout to fix spyder-ide/spyder#21172 - stdout, stderr = proc.communicate( - timeout=3 if running_in_ci() else 0.5 - ) + stdout, stderr = proc.communicate(timeout=5) if stderr: logger.info(stderr.strip()) From 15db32115d39d043a7696b2d38b7f98f775ee8f1 Mon Sep 17 00:00:00 2001 From: Ryan Clary <9618975+mrclary@users.noreply.github.com> Date: Thu, 20 Mar 2025 08:30:57 -0700 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Carlos Cordoba --- spyder/app/tests/test_mainwindow.py | 5 ++++- spyder/utils/environ.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py index c0bae190f85..67bef528013 100644 --- a/spyder/app/tests/test_mainwindow.py +++ b/spyder/app/tests/test_mainwindow.py @@ -860,7 +860,10 @@ def test_dedicated_consoles(main_window, qtbot): == "script.py/A" ) - qtbot.waitUntil(lambda: nsb.editor.source_model.rowCount() == 4, timeout=SHELL_TIMEOUT) + qtbot.waitUntil( + lambda: nsb.editor.source_model.rowCount() == 4, + timeout=SHELL_TIMEOUT + ) assert nsb.editor.source_model.rowCount() == 4 # --- Assert runfile text is present and we show the banner --- diff --git a/spyder/utils/environ.py b/spyder/utils/environ.py index e897ddb500c..151a541926d 100644 --- a/spyder/utils/environ.py +++ b/spyder/utils/environ.py @@ -117,7 +117,7 @@ def get_user_environment_variables(): proc = run_shell_command(user_env_script, env={}, text=True) # Use timeout to fix spyder-ide/spyder#21172 - stdout, stderr = proc.communicate(timeout=5) + stdout, stderr = proc.communicate(timeout=3) if stderr: logger.info(stderr.strip())