Skip to content

Commit eac428f

Browse files
authored
Merge pull request #20233 from ccordoba12/issue-20023
PR: Fix showing Spyder-kernels message on kernel restarts (IPython console)
2 parents f586c20 + 408691d commit eac428f

File tree

4 files changed

+52
-50
lines changed

4 files changed

+52
-50
lines changed

.github/scripts/install.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ python -bb -X dev -W error -m build
6767
python -bb -X dev -W error -m pip install --no-deps dist/spyder*.whl
6868

6969
# Create environment for Jedi environments tests
70-
mamba create -n jedi-test-env -q -y python=3.6 flask spyder-kernels
70+
mamba create -n jedi-test-env -q -y python=3.9 flask spyder-kernels
7171
mamba list -n jedi-test-env
7272

7373
# Create environment to test conda activation before launching a spyder kernel
74-
mamba create -n spytest-ž -q -y python=3.6 spyder-kernels
74+
mamba create -n spytest-ž -q -y python=3.9 spyder-kernels
7575
mamba list -n spytest-ž
7676

7777
# Install pyenv in Posix systems

spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@
4444
running_in_ci, running_in_ci_with_conda)
4545
from spyder.config.gui import get_color_scheme
4646
from spyder.config.manager import CONF
47+
from spyder.config.utils import is_anaconda
4748
from spyder.py3compat import PY2, to_text_string
4849
from spyder.plugins.help.tests.test_plugin import check_text
4950
from spyder.plugins.help.utils.sphinxify import CSS_PATH
5051
from spyder.plugins.ipythonconsole.plugin import IPythonConsole
5152
from spyder.plugins.ipythonconsole.utils.style import create_style_class
5253
from spyder.plugins.ipythonconsole.widgets import ClientWidget
5354
from spyder.utils.programs import get_temp_dir
54-
from spyder.utils.conda import is_conda_env
55+
from spyder.utils.conda import get_conda_root_prefix
5556

5657

5758
# =============================================================================
@@ -78,8 +79,11 @@ def get_console_background_color(style_sheet):
7879
return background_color
7980

8081

81-
def get_conda_test_env(test_env_name=u'spytest-ž'):
82-
"""Return the full prefix path of the given `test_env_name`."""
82+
def get_conda_test_env(test_env_name='spytest-ž'):
83+
"""
84+
Return the full prefix path of the given `test_env_name` and its
85+
executable.
86+
"""
8387
if 'envs' in sys.prefix:
8488
root_prefix = os.path.dirname(os.path.dirname(sys.prefix))
8589
else:
@@ -92,7 +96,7 @@ def get_conda_test_env(test_env_name=u'spytest-ž'):
9296
else:
9397
test_env_executable = os.path.join(test_env_prefix, 'bin', 'python')
9498

95-
return test_env_executable
99+
return (test_env_prefix, test_env_executable)
96100

97101

98102
# =============================================================================
@@ -196,7 +200,7 @@ def __getattr__(self, attr):
196200
if test_environment_interpreter:
197201
configuration.set('main_interpreter', 'default', False)
198202
configuration.set(
199-
'main_interpreter', 'executable', get_conda_test_env())
203+
'main_interpreter', 'executable', get_conda_test_env()[1])
200204
else:
201205
configuration.set('main_interpreter', 'default', True)
202206
configuration.set('main_interpreter', 'executable', '')
@@ -1657,6 +1661,8 @@ def test_calltip(ipyconsole, qtbot):
16571661
@flaky(max_runs=3)
16581662
@pytest.mark.order(1)
16591663
@pytest.mark.test_environment_interpreter
1664+
@pytest.mark.skipif(not is_anaconda(), reason='Only works with Anaconda')
1665+
@pytest.mark.skipif(not running_in_ci(), reason='Only works on CIs')
16601666
def test_conda_env_activation(ipyconsole, qtbot):
16611667
"""
16621668
Test that the conda environment associated with an external interpreter
@@ -1668,12 +1674,12 @@ def test_conda_env_activation(ipyconsole, qtbot):
16681674
# Get conda activation environment variable
16691675
with qtbot.waitSignal(shell.executed):
16701676
shell.execute(
1671-
"import os; conda_prefix = os.environ.get('CONDA_PREFIX')")
1677+
"import os; conda_prefix = os.environ.get('CONDA_PREFIX')"
1678+
)
16721679

1673-
expected_output = get_conda_test_env().replace('\\', '/')
1674-
if is_conda_env(expected_output):
1675-
output = shell.get_value('conda_prefix').replace('\\', '/')
1676-
assert expected_output == output
1680+
expected_output = get_conda_test_env()[0].replace('\\', '/')
1681+
output = shell.get_value('conda_prefix').replace('\\', '/')
1682+
assert expected_output == output
16771683

16781684

16791685
@flaky(max_runs=3)
@@ -2438,8 +2444,7 @@ def test_run_script(ipyconsole, qtbot, tmp_path):
24382444

24392445

24402446
@pytest.mark.skipif(
2441-
not sys.platform.startswith('linux'),
2442-
reason="Only runs on Linux")
2447+
not is_anaconda(), reason="Only works with Anaconda")
24432448
def test_show_spyder_kernels_error_on_restart(ipyconsole, qtbot):
24442449
"""Test that we show Spyder-kernels error message on restarts."""
24452450
# Wait until the window is fully up
@@ -2449,11 +2454,13 @@ def test_show_spyder_kernels_error_on_restart(ipyconsole, qtbot):
24492454

24502455
# Point to an interpreter without Spyder-kernels
24512456
ipyconsole.set_conf('default', False, section='main_interpreter')
2452-
ipyconsole.set_conf('executable', '/usr/bin/python3',
2453-
section='main_interpreter')
2457+
if os.name == 'nt':
2458+
pyexec = osp.join(get_conda_root_prefix(), 'python.exe')
2459+
else:
2460+
pyexec = osp.join(get_conda_root_prefix(), 'bin', 'python')
2461+
ipyconsole.set_conf('executable', pyexec, section='main_interpreter')
24542462

24552463
# Restart kernel
2456-
os.environ['SPY_TEST_SHOW_RESTART_MESSAGE'] = 'true'
24572464
ipyconsole.restart_kernel()
24582465

24592466
# Assert we show a kernel error
@@ -2467,7 +2474,7 @@ def test_show_spyder_kernels_error_on_restart(ipyconsole, qtbot):
24672474
timeout=6000
24682475
)
24692476

2470-
# To check kernel error visually
2477+
# To check the kernel error visually
24712478
qtbot.wait(500)
24722479

24732480
# Check kernel related actions are disabled

spyder/plugins/ipythonconsole/widgets/client.py

+24-30
Original file line numberDiff line numberDiff line change
@@ -833,38 +833,32 @@ def has_spyder_kernels(self):
833833
interpreter=pyexec,
834834
version=SPYDER_KERNELS_VERSION)
835835

836-
if (
837-
not has_spyder_kernels
838-
# This is necessary to show this message for some tests and not
839-
# do it for others.
840-
and os.environ.get('SPY_TEST_SHOW_RESTART_MESSAGE')
841-
):
842-
self.show_kernel_error(
843-
_("The Python environment or installation whose "
844-
"interpreter is located at"
845-
"<pre>"
846-
" <tt>{0}</tt>"
847-
"</pre>"
848-
"doesn't have the <tt>spyder-kernels</tt> module or the "
849-
"right version of it installed ({1}). "
850-
"Without this module is not possible for Spyder to "
851-
"create a console for you.<br><br>"
852-
"You can install it by activating your environment (if "
853-
"necessary) and then running in a system terminal:"
854-
"<pre>"
855-
" <tt>{2}</tt>"
856-
"</pre>"
857-
"or"
858-
"<pre>"
859-
" <tt>{3}</tt>"
860-
"</pre>").format(
861-
pyexec,
862-
SPYDER_KERNELS_VERSION_MSG,
863-
SPYDER_KERNELS_CONDA,
864-
SPYDER_KERNELS_PIP
865-
)
836+
msg = _(
837+
"The Python environment or installation whose interpreter is "
838+
"located at"
839+
"<pre>"
840+
" <tt>{0}</tt>"
841+
"</pre>"
842+
"doesn't have the <tt>spyder-kernels</tt> module or the right "
843+
"version of it installed ({1}). Without this module is not "
844+
"possible for Spyder to create a console for you.<br><br>"
845+
"You can install it by activating your environment first (if "
846+
"necessary) and then running in a system terminal:"
847+
"<pre>"
848+
" <tt>{2}</tt>"
849+
"</pre>"
850+
"or"
851+
"<pre>"
852+
" <tt>{3}</tt>"
853+
"</pre>").format(
854+
pyexec,
855+
SPYDER_KERNELS_VERSION_MSG,
856+
SPYDER_KERNELS_CONDA,
857+
SPYDER_KERNELS_PIP
866858
)
867859

860+
if not has_spyder_kernels:
861+
self.show_kernel_error(msg)
868862
return False
869863
else:
870864
return True

spyder/plugins/ipythonconsole/widgets/shell.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,9 @@ def interrupt_kernel(self):
264264
super(ShellWidget, self).interrupt_kernel()
265265
else:
266266
self._append_html(
267-
_("The kernel appears to be dead, so it can't be interrupted. "
268-
"Please open a new console to keep working.")
267+
_("<br><br>The kernel appears to be dead, so it can't be "
268+
"interrupted. Please open a new console to keep "
269+
"working.<br>")
269270
)
270271

271272
def execute(self, source=None, hidden=False, interactive=False):

0 commit comments

Comments
 (0)