Permalink
Browse files

BUG: raw_input logic incorrect for in-process terminal frontend.

  • Loading branch information...
1 parent 6632e6c commit d32bdb0b1a8916079540dda4e798272e45c41b4a @epatters epatters committed Sep 26, 2012
Showing with 39 additions and 1 deletion.
  1. +13 −1 IPython/inprocess/blockingkernelmanager.py
  2. +17 −0 IPython/inprocess/tests/test_kernel.py
  3. +9 −0 IPython/zmq/ipkernel.py
@@ -19,6 +19,7 @@
from threading import Event
# Local imports.
+from IPython.utils.io import raw_print
from IPython.utils.traitlets import Type
from kernelmanager import InProcessKernelManager, ShellInProcessChannel, \
SubInProcessChannel, StdInInProcessChannel
@@ -65,7 +66,18 @@ class BlockingSubInProcessChannel(BlockingChannelMixin, SubInProcessChannel):
pass
class BlockingStdInInProcessChannel(BlockingChannelMixin, StdInInProcessChannel):
- pass
+
+ def call_handlers(self, msg):
+ """ Overridden for the in-process channel.
+
+ This methods simply calls raw_input directly.
+ """
+ msg_type = msg['header']['msg_type']
+ if msg_type == 'input_request':
+ raw_input = self.manager.kernel.sys_raw_input
+ prompt = msg['content']['prompt']
+ raw_print(prompt, end='')
+ self.input(raw_input())
class BlockingInProcessKernelManager(InProcessKernelManager):
@@ -11,6 +11,8 @@
from __future__ import print_function
# Standard library imports
+from StringIO import StringIO
+import sys
import unittest
# Local imports
@@ -34,6 +36,21 @@ def test_pylab(self):
msg = get_stream_message(km)
self.assert_('Welcome to pylab' in msg['content']['data'])
+ def test_raw_input(self):
+ """ Does the in-process kernel handle raw_input correctly?
+ """
+ km = BlockingInProcessKernelManager()
+ km.start_kernel()
+
+ io = StringIO('foobar\n')
+ sys_stdin = sys.stdin
+ sys.stdin = io
+ try:
+ km.shell_channel.execute('x = raw_input()')
+ finally:
+ sys.stdin = sys_stdin
+ self.assertEqual(km.kernel.shell.user_ns.get('x'), 'foobar')
+
def test_stdout(self):
""" Does the in-process kernel correctly capture IO?
"""
@@ -84,6 +84,7 @@ def _eventloop_changed(self, name, old, new):
control_stream = Instance(ZMQStream)
iopub_socket = Instance(zmq.Socket)
stdin_socket = Instance(zmq.Socket)
+ sys_raw_input = Any()
log = Instance(logging.Logger)
user_module = Any()
@@ -352,8 +353,10 @@ def execute_request(self, stream, ident, parent):
raw_input = lambda prompt='' : self._no_raw_input()
if py3compat.PY3:
+ self.sys_raw_input = __builtin__.input
__builtin__.input = raw_input
else:
+ self.sys_raw_input = __builtin__.raw_input
__builtin__.raw_input = raw_input
# Set the parent message of the display hook and out streams.
@@ -385,6 +388,12 @@ def execute_request(self, stream, ident, parent):
reply_content.update(shell._showtraceback(etype, evalue, tb_list))
else:
status = u'ok'
+ finally:
+ # Restore raw_input.
+ if py3compat.PY3:
+ __builtin__.input = self.sys_raw_input
+ else:
+ __builtin__.raw_input = self.sys_raw_input
reply_content[u'status'] = status

0 comments on commit d32bdb0

Please sign in to comment.