Skip to content

Commit 453262e

Browse files
authored
Merge pull request #559 from dalthviz/fixes_issue_543
PR: Add support for IPython 9
2 parents c95192d + f24c6b0 commit 453262e

File tree

6 files changed

+56
-18
lines changed

6 files changed

+56
-18
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ requires-python = ">=3.9"
3535
dependencies = [
3636
"cloudpickle",
3737
"ipykernel>=6.29.3,<7",
38-
"ipython>=8.13.0,<9,!=8.17.1",
38+
"ipython>=8.13.0,<10,!=8.17.1,!=9.1.0,!=9.2.0,!=9.3.0,!=9.4.0",
3939
"jupyter-client>=7.4.9,<9",
40+
"packaging",
4041
"pyxdg>=0.26;platform_system=='Linux'",
4142
"pyzmq>=24.0.0",
4243
# We need at least this version of traitlets to fix an error when setting

requirements/posix.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cloudpickle
22
ipykernel>=6.29.3,<7
3-
ipython>=8.12.2,<9
3+
ipython>=8.13.0,<10,!=8.17.1,!=9.1.0,!=9.2.0,!=9.3.0,!=9.4.0
44
jupyter_client>=7.4.9,<9
55
pyzmq>=24.0.0
66
pyxdg>=0.26

requirements/windows.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cloudpickle
22
ipykernel>=6.29.3,<7
3-
ipython>=8.12.2,<9
3+
ipython>=8.13.0,<10,!=8.17.1,!=9.1.0,!=9.2.0,!=9.3.0,!=9.4.0
44
jupyter_client>=7.4.9,<9
55
pyzmq>=24.0.0
66
traitlets>=5.14.3

spyder_kernels/console/kernel.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ipykernel.ipkernel import IPythonKernel
2828
from ipykernel import get_connection_info
2929
from IPython.core import release as ipython_release
30+
from packaging.version import parse as parse_version
3031
from traitlets.config.loader import Config, LazyConfigValue
3132
import zmq
3233
from zmq.utils.garbage import gc
@@ -49,7 +50,7 @@
4950
from spyder_kernels.utils.mpl import automatic_backend, MPL_BACKENDS_TO_SPYDER
5051
from spyder_kernels.utils.nsview import (
5152
get_remote_data, make_remote_view, get_size)
52-
from spyder_kernels.utils.style import create_style_class
53+
from spyder_kernels.utils.style import create_pygments_dict, create_style_class
5354
from spyder_kernels.console.shell import SpyderShell
5455
from spyder_kernels.comms.utils import WriteContext
5556

@@ -705,15 +706,37 @@ def set_traceback_highlighting(self, color_scheme):
705706

706707
def set_traceback_syntax_highlighting(self, syntax_style):
707708
"""Set the traceback syntax highlighting style."""
708-
import IPython.core.ultratb
709-
from IPython.core.ultratb import VerboseTB
709+
if parse_version(ipython_release.version) >= parse_version("9.0"):
710+
# Create spyder theme definition and set it (IPython 9.x+)
711+
import IPython.utils.PyColorize
712+
from IPython.utils.PyColorize import (
713+
Theme,
714+
linux_theme,
715+
neutral_theme,
716+
)
717+
718+
base = "default"
719+
extra_style = neutral_theme.extra_style
720+
if self.shell.get_spyder_theme() == "dark":
721+
base = "monokai"
722+
extra_style = linux_theme.extra_style
723+
724+
extra_style.update(create_pygments_dict(syntax_style))
725+
theme = Theme("spyder_theme", base, extra_style)
726+
IPython.utils.PyColorize.theme_table["spyder_theme"] = theme
727+
self.shell.run_line_magic("colors", "spyder_theme")
728+
else:
729+
# Use `tb_highlight_style` class attribute to set the style (
730+
# IPython 8.x)
731+
import IPython.core.ultratb
732+
from IPython.core.ultratb import VerboseTB
710733

711-
IPython.core.ultratb.get_style_by_name = create_style_class
734+
IPython.core.ultratb.get_style_by_name = create_style_class
712735

713-
if getattr(VerboseTB, 'tb_highlight_style', None) is not None:
714-
VerboseTB.tb_highlight_style = syntax_style
715-
elif getattr(VerboseTB, '_tb_highlight_style', None) is not None:
716-
VerboseTB._tb_highlight_style = syntax_style
736+
if getattr(VerboseTB, "tb_highlight_style", None) is not None:
737+
VerboseTB.tb_highlight_style = syntax_style
738+
elif getattr(VerboseTB, "_tb_highlight_style", None) is not None:
739+
VerboseTB._tb_highlight_style = syntax_style
717740

718741
def get_cwd(self):
719742
"""Get current working directory."""

spyder_kernels/console/shell.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
# Third-party imports
2626
from ipykernel.zmqshell import ZMQInteractiveShell
27+
from IPython.core import release as ipython_release
28+
from packaging.version import parse as parse_version
29+
2730

2831
# Local imports
2932
from spyder_kernels.customize.namespace_manager import NamespaceManager
@@ -117,9 +120,11 @@ def _showtraceback(self, etype, evalue, stb):
117120
for line in stb:
118121
if (
119122
# Verbose mode
120-
re.match(r"File (.*)", line)
123+
re.match(r"File (.*)", line) # IPython 8.x
124+
or re.match(r"\x1b(.*)File (.*)", line) # IPython 9.x
121125
# Plain mode
122-
or re.match(r"\x1b\[(.*) File (.*)", line)
126+
or re.match(r"\x1b\[(.*) File (.*)", line) # IPython 8.x
127+
or re.match(r" File (.*)", line) # IPython 9.x
123128
) and (
124129
# The file line should not contain a location where
125130
# Spyder-kernels is installed
@@ -139,11 +144,11 @@ def _showtraceback(self, etype, evalue, stb):
139144
def set_spyder_theme(self, theme):
140145
"""Set the theme for the console."""
141146
self._spyder_theme = theme
142-
if theme == "dark":
143-
# Needed to change the colors of tracebacks
144-
self.run_line_magic("colors", "linux")
145-
elif theme == "light":
146-
self.run_line_magic("colors", "lightbg")
147+
148+
# Call `%colors` following theme for IPython 8.x tracebacks
149+
if parse_version(ipython_release.version) < parse_version("9.0"):
150+
colors = "linux" if theme == "dark" else "lightbg"
151+
self.run_line_magic("colors", colors)
147152

148153
def get_spyder_theme(self):
149154
"""Get the theme for the console."""

spyder_kernels/console/start.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
import sys
1717
import site
1818

19+
# Third-party imports
20+
from IPython.core import release as ipython_release
21+
from packaging.version import parse as parse_version
22+
23+
1924
# Remove current directory from sys.path to prevent kernel crashes when people
2025
# name Python files or modules with the same name as standard library modules.
2126
# See spyder-ide/spyder#8007
@@ -101,6 +106,10 @@ def kernel_config():
101106
# To handle the banner by ourselves
102107
spy_cfg.ZMQInteractiveShell.banner1 = ''
103108

109+
# To disable tips (for the moment) that are only available in IPython 9.0+
110+
if parse_version(ipython_release.version) >= parse_version("9.0"):
111+
spy_cfg.ZMQInteractiveShell.enable_tip = False
112+
104113
# Greedy completer
105114
greedy_o = os.environ.get('SPY_GREEDY_O') == 'True'
106115
spy_cfg.IPCompleter.greedy = greedy_o

0 commit comments

Comments
 (0)