##// END OF EJS Templates
Merge pull request #7547 from takluyver/widget-eg-fix...
Merge pull request #7547 from takluyver/widget-eg-fix Update some broken examples of using widgets

File last commit:

r19213:71644c28
r20133:a560cee7 merge
Show More
interactiveshell.py
578 lines | 23.0 KiB | text/x-python | PythonLexer
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 # -*- coding: utf-8 -*-
MinRK
pyout -> execute_result...
r16568 """terminal client to the IPython kernel"""
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
MinRK
pyout -> execute_result...
r16568 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
from __future__ import print_function
MinRK
handle getpass in terminal frontend
r16577 import base64
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 import bdb
MinRK
[termconsole] raw_input improvements...
r5622 import signal
Takafumi Arakaki
Redirect unused pipes to devnull
r8244 import os
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 import sys
MinRK
two-process interactive shell progress...
r5616 import time
Takafumi Arakaki
Add more image handler backends...
r8240 import subprocess
MinRK
handle getpass in terminal frontend
r16577 from getpass import getpass
Takafumi Arakaki
Use io.BytesIO instead of StringIO
r8239 from io import BytesIO
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
Thomas Kluyver
Update imports for Python 3...
r13354 try:
from queue import Empty # Py 3
except ImportError:
from Queue import Empty # Py 2
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
MinRK
two-process interactive shell progress...
r5616 from IPython.core import page
MinRK
add kernel banner to terminal and qt frontends
r16583 from IPython.core import release
Doug Blank
Addressed issues: renamed ZMQHistoryManager (appears to do need to do more than just an Accessor) in IPython/terminal/console/zmqhistory.py; added abc methods; rewrote access methods to load the history from the kernel client; works with 'ipython console' and with 'ipython console --kernel'
r18846 from IPython.terminal.console.zmqhistory import ZMQHistoryManager
Thomas Kluyver
Remove unused imports in IPython.terminal
r11132 from IPython.utils.warn import warn, error
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 from IPython.utils import io
Thomas Kluyver
Fix references to raw_input()
r13355 from IPython.utils.py3compat import string_types, input
MinRK
Support echoing output from other clients...
r18373 from IPython.utils.traitlets import List, Enum, Any, Instance, Unicode, Float, Bool
Takafumi Arakaki
Do not use NamedTemporaryFile in handle_image_tempfile...
r8246 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
Fernando Perez
Fix all imports for 2-process text console.
r11021 from IPython.terminal.interactiveshell import TerminalInteractiveShell
from IPython.terminal.console.completer import ZMQCompleter
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
class ZMQTerminalInteractiveShell(TerminalInteractiveShell):
MinRK
two-process interactive shell progress...
r5616 """A subclass of TerminalInteractiveShell that uses the 0MQ kernel"""
_executing = False
MinRK
cleanup terminal console iopub handling...
r11670 _execution_state = Unicode('')
Jonathan Frederic
Make `ipython console` aware of clear_output changes.
r16215 _pending_clearoutput = False
MinRK
add kernel banner to terminal and qt frontends
r16583 kernel_banner = Unicode('')
MinRK
make kernel_timeout configurable in terminal console
r11790 kernel_timeout = Float(60, config=True,
help="""Timeout for giving up on a kernel (in seconds).
On first connect and restart, the console tests whether the
kernel is running and responsive by sending kernel_info_requests.
This sets the timeout in seconds for how long the kernel can take
before being presumed dead.
"""
)
Takafumi Arakaki
Add image message handler in ZMQTerminalInteractiveShell...
r8238
Takafumi Arakaki
Add more image handler backends...
r8240 image_handler = Enum(('PIL', 'stream', 'tempfile', 'callable'),
config=True, help=
Takafumi Arakaki
Add image message handler in ZMQTerminalInteractiveShell...
r8238 """
Takafumi Arakaki
Add more image handler backends...
r8240 Handler for image type output. This is useful, for example,
Takafumi Arakaki
Add image message handler in ZMQTerminalInteractiveShell...
r8238 when connecting to the kernel in which pylab inline backend is
Takafumi Arakaki
Add more image handler backends...
r8240 activated. There are four handlers defined. 'PIL': Use
Python Imaging Library to popup image; 'stream': Use an
external program to show the image. Image will be fed into
the STDIN of the program. You will need to configure
`stream_image_handler`; 'tempfile': Use an external program to
show the image. Image will be saved in a temporally file and
the program is called with the temporally file. You will need
to configure `tempfile_image_handler`; 'callable': You can set
any Python callable which is called with the image data. You
will need to configure `callable_image_handler`.
"""
)
stream_image_handler = List(config=True, help=
"""
Command to invoke an image viewer program when you are using
'stream' image handler. This option is a list of string where
the first element is the command itself and reminders are the
options for the command. Raw image data is given as STDIN to
the program.
"""
)
tempfile_image_handler = List(config=True, help=
"""
Command to invoke an image viewer program when you are using
'tempfile' image handler. This option is a list of string
where the first element is the command itself and reminders
Takafumi Arakaki
Support passing image format to external program...
r8242 are the options for the command. You can use {file} and
{format} in the string to represent the location of the
generated image file and image format.
Takafumi Arakaki
Add more image handler backends...
r8240 """
)
callable_image_handler = Any(config=True, help=
"""
Callable object called via 'callable' image handler with one
argument, `data`, which is `msg["content"]["data"]` where
`msg` is the message from iopub channel. For exmaple, you can
Takafumi Arakaki
Fix help for callable_image_handler
r8249 find base64 encoded PNG data as `data['image/png']`.
Takafumi Arakaki
Add image message handler in ZMQTerminalInteractiveShell...
r8238 """
)
Takafumi Arakaki
Make MIME preference order configurable
r8243 mime_preference = List(
default_value=['image/png', 'image/jpeg', 'image/svg+xml'],
config=True, allow_none=False, help=
"""
Preferred object representation MIME type in order. First
matched MIME type will be used.
"""
)
MinRK
add manager and client as trait lets of ZMQInteractiveShell...
r10331 manager = Instance('IPython.kernel.KernelManager')
client = Instance('IPython.kernel.KernelClient')
def _client_changed(self, name, old, new):
self.session_id = new.session.session
session_id = Unicode()
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 def init_completer(self):
"""Initialize the completion machinery.
This creates completion machinery that can be used by client code,
either interactively in-process (typically triggered by the readline
MinRK
only shutdown kernel when it is owned by the console
r13560 library), programmatically (such as in test suites) or out-of-process
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 (typically over the network by remote frontends).
"""
from IPython.core.completerlib import (module_completer,
magic_run_completer, cd_completer)
Matthias BUSSONNIER
Add configurability to tabcompletion timeout...
r11676 self.Completer = ZMQCompleter(self, self.client, config=self.config)
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
self.set_hook('complete_command', module_completer, str_key = 'import')
self.set_hook('complete_command', module_completer, str_key = 'from')
self.set_hook('complete_command', magic_run_completer, str_key = '%run')
self.set_hook('complete_command', cd_completer, str_key = '%cd')
# Only configure readline if we truly are using readline. IPython can
# do tab-completion over the network, in GUIs, etc, where readline
# itself may be absent
if self.has_readline:
self.set_readline_completer()
def run_cell(self, cell, store_history=True):
"""Run a complete IPython cell.
Parameters
----------
cell : str
The code (including IPython code such as %magic functions) to run.
store_history : bool
If True, the raw and translated cell will be stored in IPython's
history. For user code calling back into IPython's machinery, this
should be set to False.
"""
if (not cell) or cell.isspace():
MinRK
display any output from this session in terminal console...
r15590 # pressing enter flushes any pending display
self.handle_iopub()
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 return
MinRK
use ask_exit payload in terminal console...
r17325
MinRK
two-process interactive shell progress...
r5616 # flush stale replies, which could have been ignored, due to missed heartbeats
MinRK
update terminal console to new KM / KC APIs
r10287 while self.client.shell_channel.msg_ready():
self.client.shell_channel.get_msg()
Thomas Kluyver
Use messaging methods on kernel client instead of channel objects
r19213 # execute takes 'hidden', which is the inverse of store_hist
msg_id = self.client.execute(cell, not store_history)
MinRK
cleanup terminal console iopub handling...
r11670
# first thing is wait for any side effects (output, stdin, etc.)
self._executing = True
self._execution_state = "busy"
while self._execution_state != 'idle' and self.client.is_alive():
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 try:
MinRK
handle getpass in terminal frontend
r16577 self.handle_input_request(msg_id, timeout=0.05)
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 except Empty:
MinRK
two-process interactive shell progress...
r5616 # display intermediate print statements, etc.
MinRK
cleanup terminal console iopub handling...
r11670 self.handle_iopub(msg_id)
# after all of that is done, wait for the execute reply
while self.client.is_alive():
try:
self.handle_execute_reply(msg_id, timeout=0.05)
except Empty:
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 pass
MinRK
cleanup terminal console iopub handling...
r11670 else:
break
MinRK
two-process interactive shell progress...
r5616 self._executing = False
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
#-----------------
# message handlers
#-----------------
MinRK
cleanup terminal console iopub handling...
r11670 def handle_execute_reply(self, msg_id, timeout=None):
msg = self.client.shell_channel.get_msg(block=False, timeout=timeout)
MinRK
two-process interactive shell progress...
r5616 if msg["parent_header"].get("msg_id", None) == msg_id:
MinRK
cleanup terminal console iopub handling...
r11670 self.handle_iopub(msg_id)
MinRK
two-process interactive shell progress...
r5616
content = msg["content"]
status = content['status']
if status == 'aborted':
self.write('Aborted\n')
return
elif status == 'ok':
MinRK
pager payload is a mime-bundle
r16586 # handle payloads
MinRK
two-process interactive shell progress...
r5616 for item in content["payload"]:
MinRK
pager payload is a mime-bundle
r16586 source = item['source']
if source == 'page':
page.page(item['data']['text/plain'])
elif source == 'set_next_input':
self.set_next_input(item['text'])
MinRK
use ask_exit payload in terminal console...
r17325 elif source == 'ask_exit':
self.ask_exit()
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
MinRK
two-process interactive shell progress...
r5616 elif status == 'error':
for frame in content["traceback"]:
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 print(frame, file=io.stderr)
MinRK
two-process interactive shell progress...
r5616 self.execution_count = int(content["execution_count"] + 1)
MinRK
Support echoing output from other clients...
r18373
include_other_output = Bool(False, config=True,
help="""Whether to include output from clients
other than this one sharing the same kernel.
Outputs are not displayed until enter is pressed.
"""
)
other_output_prefix = Unicode("[remote] ", config=True,
help="""Prefix to add to outputs coming from clients other than this one.
Only relevant if include_other_output is True.
"""
)
def from_here(self, msg):
"""Return whether a message is from this session"""
return msg['parent_header'].get("session", self.session_id) == self.session_id
def include_output(self, msg):
"""Return whether we should include a given output message"""
from_here = self.from_here(msg)
if msg['msg_type'] == 'execute_input':
# only echo inputs not from here
return self.include_other_output and not from_here
if self.include_other_output:
return True
else:
return from_here
MinRK
display any output from this session in terminal console...
r15590 def handle_iopub(self, msg_id=''):
"""Process messages on the IOPub channel
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
MinRK
cleanup terminal console iopub handling...
r11670 This method consumes and processes messages on the IOPub channel,
MinRK
pyout -> execute_result...
r16568 such as stdout, stderr, execute_result and status.
MinRK
cleanup terminal console iopub handling...
r11670
MinRK
display any output from this session in terminal console...
r15590 It only displays output that is caused by this session.
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 """
MinRK
update terminal console to new KM / KC APIs
r10287 while self.client.iopub_channel.msg_ready():
sub_msg = self.client.iopub_channel.get_msg()
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 msg_type = sub_msg['header']['msg_type']
MinRK
protect term console from empty parent header (startup messages)
r5631 parent = sub_msg["parent_header"]
MinRK
display any output from this session in terminal console...
r15590
MinRK
Support echoing output from other clients...
r18373 if self.include_output(sub_msg):
MinRK
cleanup terminal console iopub handling...
r11670 if msg_type == 'status':
MinRK
display any output from this session in terminal console...
r15590 self._execution_state = sub_msg["content"]["execution_state"]
MinRK
cleanup terminal console iopub handling...
r11670 elif msg_type == 'stream':
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 if sub_msg["content"]["name"] == "stdout":
Jonathan Frederic
Make `ipython console` aware of clear_output changes.
r16215 if self._pending_clearoutput:
print("\r", file=io.stdout, end="")
self._pending_clearoutput = False
MinRK
msgspec 5: stream.data -> stream.text
r18104 print(sub_msg["content"]["text"], file=io.stdout, end="")
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 io.stdout.flush()
MinRK
msgspec 5: stream.data -> stream.text
r18104 elif sub_msg["content"]["name"] == "stderr":
Jonathan Frederic
Make `ipython console` aware of clear_output changes.
r16215 if self._pending_clearoutput:
print("\r", file=io.stderr, end="")
self._pending_clearoutput = False
MinRK
msgspec 5: stream.data -> stream.text
r18104 print(sub_msg["content"]["text"], file=io.stderr, end="")
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 io.stderr.flush()
Paul Ivanov
Out[] prompt now reflects updated execution_count
r5606
MinRK
pyout -> execute_result...
r16568 elif msg_type == 'execute_result':
Jonathan Frederic
Make `ipython console` aware of clear_output changes.
r16215 if self._pending_clearoutput:
print("\r", file=io.stdout, end="")
self._pending_clearoutput = False
Paul Ivanov
Out[] prompt now reflects updated execution_count
r5606 self.execution_count = int(sub_msg["content"]["execution_count"])
MinRK
Support echoing output from other clients...
r18373 if not self.from_here(sub_msg):
sys.stdout.write(self.other_output_prefix)
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 format_dict = sub_msg["content"]["data"]
Takafumi Arakaki
Add image message handler in ZMQTerminalInteractiveShell...
r8238 self.handle_rich_data(format_dict)
MinRK
Support echoing output from other clients...
r18373
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 # taken from DisplayHook.__call__:
hook = self.displayhook
hook.start_displayhook()
hook.write_output_prompt()
hook.write_format_data(format_dict)
hook.log_output(format_dict)
hook.finish_displayhook()
Takafumi Arakaki
Add image message handler in ZMQTerminalInteractiveShell...
r8238 elif msg_type == 'display_data':
Paul Ivanov
don't double display in the console...
r13821 data = sub_msg["content"]["data"]
handled = self.handle_rich_data(data)
if not handled:
MinRK
Support echoing output from other clients...
r18373 if not self.from_here(sub_msg):
sys.stdout.write(self.other_output_prefix)
Paul Ivanov
don't double display in the console...
r13821 # if it was an image, we handled it by now
if 'text/plain' in data:
print(data['text/plain'])
MinRK
Support echoing output from other clients...
r18373
elif msg_type == 'execute_input':
content = sub_msg['content']
self.execution_count = content['execution_count']
if not self.from_here(sub_msg):
sys.stdout.write(self.other_output_prefix)
sys.stdout.write(self.prompt_manager.render('in'))
sys.stdout.write(content['code'])
Jonathan Frederic
Make `ipython console` aware of clear_output changes.
r16215 elif msg_type == 'clear_output':
if sub_msg["content"]["wait"]:
self._pending_clearoutput = True
else:
print("\r", file=io.stdout, end="")
Takafumi Arakaki
Support passing image format to external program...
r8242 _imagemime = {
'image/png': 'png',
'image/jpeg': 'jpeg',
'image/svg+xml': 'svg',
}
Takafumi Arakaki
Add image message handler in ZMQTerminalInteractiveShell...
r8238 def handle_rich_data(self, data):
Takafumi Arakaki
Make MIME preference order configurable
r8243 for mime in self.mime_preference:
if mime in data and mime in self._imagemime:
Takafumi Arakaki
Add more image handler backends...
r8240 self.handle_image(data, mime)
Paul Ivanov
don't double display in the console...
r13821 return True
Takafumi Arakaki
Add more image handler backends...
r8240
def handle_image(self, data, mime):
handler = getattr(
self, 'handle_image_{0}'.format(self.image_handler), None)
if handler:
handler(data, mime)
def handle_image_PIL(self, data, mime):
if mime not in ('image/png', 'image/jpeg'):
return
Takafumi Arakaki
"import PIL.Image" instead of "import PIL"...
r8258 import PIL.Image
Takafumi Arakaki
Encode to byte before base64.decodestring
r8252 raw = base64.decodestring(data[mime].encode('ascii'))
Takafumi Arakaki
Add more image handler backends...
r8240 img = PIL.Image.open(BytesIO(raw))
img.show()
def handle_image_stream(self, data, mime):
Takafumi Arakaki
Encode to byte before base64.decodestring
r8252 raw = base64.decodestring(data[mime].encode('ascii'))
Takafumi Arakaki
Support passing image format to external program...
r8242 imageformat = self._imagemime[mime]
fmt = dict(format=imageformat)
args = [s.format(**fmt) for s in self.stream_image_handler]
Takafumi Arakaki
Redirect unused pipes to devnull
r8244 with open(os.devnull, 'w') as devnull:
proc = subprocess.Popen(
args, stdin=subprocess.PIPE,
stdout=devnull, stderr=devnull)
proc.communicate(raw)
Takafumi Arakaki
Add more image handler backends...
r8240
def handle_image_tempfile(self, data, mime):
Takafumi Arakaki
Encode to byte before base64.decodestring
r8252 raw = base64.decodestring(data[mime].encode('ascii'))
Takafumi Arakaki
Support passing image format to external program...
r8242 imageformat = self._imagemime[mime]
Takafumi Arakaki
Simplify handle_image_tempfile
r8248 filename = 'tmp.{0}'.format(imageformat)
Thomas Kluyver
Drop nested() function for context managers
r12148 with NamedFileInTemporaryDirectory(filename) as f, \
open(os.devnull, 'w') as devnull:
Takafumi Arakaki
Add more image handler backends...
r8240 f.write(raw)
f.flush()
Takafumi Arakaki
Support passing image format to external program...
r8242 fmt = dict(file=f.name, format=imageformat)
Takafumi Arakaki
Add more image handler backends...
r8240 args = [s.format(**fmt) for s in self.tempfile_image_handler]
Takafumi Arakaki
Redirect unused pipes to devnull
r8244 subprocess.call(args, stdout=devnull, stderr=devnull)
Takafumi Arakaki
Add more image handler backends...
r8240
def handle_image_callable(self, data, mime):
self.callable_image_handler(data)
Takafumi Arakaki
Add image message handler in ZMQTerminalInteractiveShell...
r8238
MinRK
handle getpass in terminal frontend
r16577 def handle_input_request(self, msg_id, timeout=0.1):
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 """ Method to capture raw_input
"""
MinRK
handle getpass in terminal frontend
r16577 req = self.client.stdin_channel.get_msg(timeout=timeout)
MinRK
[termconsole] raw_input improvements...
r5622 # in case any iopub came while we were waiting:
MinRK
cleanup terminal console iopub handling...
r11670 self.handle_iopub(msg_id)
MinRK
handle getpass in terminal frontend
r16577 if msg_id == req["parent_header"].get("msg_id"):
MinRK
[termconsole] raw_input improvements...
r5622 # wrap SIGINT handler
real_handler = signal.getsignal(signal.SIGINT)
def double_int(sig,frame):
# call real handler (forwards sigint to kernel),
# then raise local interrupt, stopping local raw_input
real_handler(sig,frame)
raise KeyboardInterrupt
signal.signal(signal.SIGINT, double_int)
MinRK
handle getpass in terminal frontend
r16577 content = req['content']
read = getpass if content.get('password', False) else input
MinRK
[termconsole] raw_input improvements...
r5622 try:
MinRK
handle getpass in terminal frontend
r16577 raw_data = read(content["prompt"])
MinRK
[termconsole] raw_input improvements...
r5622 except EOFError:
# turn EOFError into EOF character
raw_data = '\x04'
except KeyboardInterrupt:
sys.stdout.write('\n')
return
finally:
# restore SIGINT handler
signal.signal(signal.SIGINT, real_handler)
# only send stdin reply if there *was not* another request
# or execution finished while we were reading.
MinRK
update terminal console to new KM / KC APIs
r10287 if not (self.client.stdin_channel.msg_ready() or self.client.shell_channel.msg_ready()):
Thomas Kluyver
Use messaging methods on kernel client instead of channel objects
r19213 self.client.input(raw_data)
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
def mainloop(self, display_banner=False):
while True:
try:
self.interact(display_banner=display_banner)
MinRK
add kernel banner to terminal and qt frontends
r16583 #self.interact_with_readline()
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 # XXX for testing of a readline-decoupled repl loop, call
# interact_with_readline above
break
except KeyboardInterrupt:
# this should not be necessary, but KeyboardInterrupt
# handling seems rather unpredictable...
self.write("\nKeyboardInterrupt in interact()\n")
Thomas Kluyver
Send shutdown message when exiting ipython console
r18793
Thomas Kluyver
Use messaging methods on kernel client instead of channel objects
r19213 self.client.shutdown()
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600
MinRK
add kernel banner to terminal and qt frontends
r16583 def _banner1_default(self):
return "IPython Console {version}\n".format(version=release.version)
def compute_banner(self):
super(ZMQTerminalInteractiveShell, self).compute_banner()
if self.client and not self.kernel_banner:
msg_id = self.client.kernel_info()
while True:
try:
reply = self.client.get_shell_msg(timeout=1)
except Empty:
break
else:
if reply['parent_header'].get('msg_id') == msg_id:
self.kernel_banner = reply['content'].get('banner', '')
break
self.banner += self.kernel_banner
MinRK
two-process interactive shell progress...
r5616 def wait_for_kernel(self, timeout=None):
"""method to wait for a kernel to be ready"""
tic = time.time()
MinRK
update terminal console to new KM / KC APIs
r10287 self.client.hb_channel.unpause()
MinRK
two-process interactive shell progress...
r5616 while True:
MinRK
relax timeouts in Terminal console check for kernel...
r11785 msg_id = self.client.kernel_info()
reply = None
while True:
try:
reply = self.client.get_shell_msg(timeout=1)
except Empty:
break
else:
if reply['parent_header'].get('msg_id') == msg_id:
return True
if timeout is not None \
and (time.time() - tic) > timeout \
and not self.client.hb_channel.is_beating():
MinRK
two-process interactive shell progress...
r5616 # heart failed
MinRK
relax timeouts in Terminal console check for kernel...
r11785 return False
MinRK
two-process interactive shell progress...
r5616 return True
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 def interact(self, display_banner=None):
"""Closely emulate the interactive Python console."""
# batch run -> do not interact
if self.exit_now:
return
if display_banner is None:
display_banner = self.display_banner
Thomas Kluyver
Replace references to unicode and basestring
r13353 if isinstance(display_banner, string_types):
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 self.show_banner(display_banner)
elif display_banner:
self.show_banner()
more = False
MinRK
two-process interactive shell progress...
r5616 # run a non-empty no-op, so that we don't get a prompt until
# we know the kernel is ready. This keeps the connection
# message above the first prompt.
MinRK
make kernel_timeout configurable in terminal console
r11790 if not self.wait_for_kernel(self.kernel_timeout):
MinRK
two-process interactive shell progress...
r5616 error("Kernel did not respond\n")
return
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 if self.has_readline:
self.readline_startup_hook(self.pre_readline)
MinRK
bring multiline-hist fix from trunk into zmq console
r5617 hlen_b4_cell = self.readline.get_current_history_length()
else:
hlen_b4_cell = 0
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 # exit_now is set by a call to %Exit or %Quit, through the
# ask_exit callback.
while not self.exit_now:
MinRK
update terminal console to new KM / KC APIs
r10287 if not self.client.is_alive():
MinRK
two-process interactive shell progress...
r5616 # kernel died, prompt for action or exit
MinRK
update terminal console to new KM / KC APIs
r10287
action = "restart" if self.manager else "wait for restart"
MinRK
two-process interactive shell progress...
r5616 ans = self.ask_yes_no("kernel died, %s ([y]/n)?" % action, default='y')
if ans:
MinRK
update terminal console to new KM / KC APIs
r10287 if self.manager:
self.manager.restart_kernel(True)
MinRK
make kernel_timeout configurable in terminal console
r11790 self.wait_for_kernel(self.kernel_timeout)
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 else:
MinRK
two-process interactive shell progress...
r5616 self.exit_now = True
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 continue
try:
MinRK
two-process interactive shell progress...
r5616 # protect prompt block from KeyboardInterrupt
# when sitting on ctrl-C
self.hooks.pre_prompt_hook()
if more:
try:
MinRK
update two-process console with prompt_manager changes
r5628 prompt = self.prompt_manager.render('in2')
MinRK
two-process interactive shell progress...
r5616 except Exception:
self.showtraceback()
if self.autoindent:
self.rl_do_indent = True
else:
try:
MinRK
update two-process console with prompt_manager changes
r5628 prompt = self.separate_in + self.prompt_manager.render('in')
MinRK
two-process interactive shell progress...
r5616 except Exception:
self.showtraceback()
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 line = self.raw_input(prompt)
if self.exit_now:
# quick exit on sys.std[in|out] close
break
if self.autoindent:
self.rl_do_indent = False
except KeyboardInterrupt:
#double-guard against keyboardinterrupts during kbdint handling
try:
Jeroen Demeyer
Print exception instead of "KeyboardInterrupt"...
r19114 self.write('\n' + self.get_exception_only())
Thomas Kluyver
Simplify IPythonInputSplitter API
r13912 source_raw = self.input_splitter.raw_reset()
MinRK
bring multiline-hist fix from trunk into zmq console
r5617 hlen_b4_cell = self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 more = False
except KeyboardInterrupt:
pass
except EOFError:
if self.autoindent:
self.rl_do_indent = False
if self.has_readline:
self.readline_startup_hook(None)
self.write('\n')
self.exit()
except bdb.BdbQuit:
warn('The Python debugger has exited with a BdbQuit exception.\n'
'Because of how pdb handles the stack, it is impossible\n'
'for IPython to properly format this particular exception.\n'
'IPython will resume normal operation.')
except:
# exceptions here are VERY RARE, but they can be triggered
# asynchronously by signal handlers, for example.
self.showtraceback()
else:
Thomas Kluyver
Catch transformer syntax errors in zmq terminal console
r13913 try:
self.input_splitter.push(line)
more = self.input_splitter.push_accepts_more()
except SyntaxError:
# Run the code directly - run_cell takes care of displaying
# the exception.
more = False
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 if (self.SyntaxTB.last_syntax_error and
self.autoedit_syntax):
self.edit_syntax_error()
if not more:
Thomas Kluyver
Simplify IPythonInputSplitter API
r13912 source_raw = self.input_splitter.raw_reset()
MinRK
bring multiline-hist fix from trunk into zmq console
r5617 hlen_b4_cell = self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
MinRK
zmqterminal subclasses TerminalInteractiveShell/IPApp...
r5600 self.run_cell(source_raw)
# Turn off the exit flag, so the mainloop can be restarted if desired
self.exit_now = False
Doug Blank
Working draft of KernelHistoryManager
r18815
def init_history(self):
"""Sets up the command history. """
Doug Blank
Addressed issues: renamed ZMQHistoryManager (appears to do need to do more than just an Accessor) in IPython/terminal/console/zmqhistory.py; added abc methods; rewrote access methods to load the history from the kernel client; works with 'ipython console' and with 'ipython console --kernel'
r18846 self.history_manager = ZMQHistoryManager(client=self.client)
Doug Blank
Working draft of KernelHistoryManager
r18815 self.configurables.append(self.history_manager)