Skip to content

Commit f81cc44

Browse files
committed
compatibility fix for async in IPython 7.0
1 parent 76a33de commit f81cc44

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

psyplot_gui/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def start_app(fnames=[], name=[], dims=None, plot_method=None,
215215
if script is not None:
216216
mainwindow.console.run_script_in_shell(script)
217217
if command is not None:
218-
mainwindow.console.kernel_manager.kernel.shell.run_code(command)
218+
mainwindow.console.run_command_in_shell(command)
219219
if exec_:
220220
sys.excepthook = mainwindow.excepthook
221221
sys.exit(app.exec_())

psyplot_gui/console.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def __init__(self, main, *args, **kwargs):
133133
self.kernel_manager = kernel_manager
134134
self.kernel_client = kernel_client
135135

136-
self.kernel_manager.kernel.shell.run_code(
136+
self.run_command_in_shell(
137137
'\n'.join('import %s as %s' % t for t in modules2import))
138138
self.exit_requested.connect(self._close_mainwindow)
139139
self.exit_requested.connect(QtCore.QCoreApplication.instance().quit)
@@ -166,13 +166,13 @@ def update_mp(self, project):
166166
"""Update the `mp` variable in the shell is
167167
``rcParams['console.auto_set_mp']`` with a main project"""
168168
if self.rc['auto_set_mp'] and project is not None and project.is_main:
169-
self.kernel_manager.kernel.shell.run_code('mp = psy.gcp(True)')
169+
self.run_command_in_shell('mp = psy.gcp(True)')
170170

171171
def update_sp(self, project):
172172
"""Update the `sp` variable in the shell is
173173
``rcParams['console.auto_set_sp']`` with a sub project"""
174174
if self.rc['auto_set_sp'] and (project is None or not project.is_main):
175-
self.kernel_manager.kernel.shell.run_code('sp = psy.gcp()')
175+
self.run_command_in_shell('sp = psy.gcp()')
176176

177177
def show_current_help(self, to_end=False, force=False):
178178
"""Show the help of the object at the cursor position if
@@ -271,9 +271,18 @@ def _run_command_in_shell(self, args):
271271
# 2: command
272272
self.run_command_in_shell(args[2])
273273

274-
def run_command_in_shell(self, command):
274+
def run_command_in_shell(self, code, *args, **kwargs):
275275
"""Run a script in the shell"""
276-
self.kernel_manager.kernel.shell.run_code(command)
276+
ret = self.kernel_manager.kernel.shell.run_code(code, *args, **kwargs)
277+
import IPython
278+
if IPython.__version__ < '7.0': # run_code is an asyncio.coroutine
279+
return ret
280+
else:
281+
import asyncio
282+
gathered = asyncio.gather(ret)
283+
loop = asyncio.get_event_loop()
284+
ret = loop.run_until_complete(gathered)
285+
return ret[0]
277286

278287
def _close_mainwindow(self):
279288
from psyplot_gui.main import mainwindow

psyplot_gui/fmt_widget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ def run_code(self):
551551
e = ExecutionResult(info)
552552
else:
553553
e = ExecutionResult()
554-
self.shell.run_code(code, e)
554+
self.console.run_command_in_shell(code, e)
555555
try:
556556
e.raise_error()
557557
except Exception: # reset the console and clear the error message

tests/test_console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_current_object(self):
9191
self.assertEqual(c.get_current_object(), 'object')
9292

9393
def test_command(self):
94-
self.window.console.kernel_manager.kernel.shell.run_code('a = 4')
94+
self.window.console.run_command_in_shell('a = 4')
9595
self.assertEqual(self.window.console.get_obj('a')[1], 4)
9696

9797
def test_mp_sp(self):

0 commit comments

Comments
 (0)