##// END OF EJS Templates
BUG: raw_input logic incorrect for in-process terminal frontend.
epatters -
Show More
@@ -19,6 +19,7 b' import Queue'
19 19 from threading import Event
20 20
21 21 # Local imports.
22 from IPython.utils.io import raw_print
22 23 from IPython.utils.traitlets import Type
23 24 from kernelmanager import InProcessKernelManager, ShellInProcessChannel, \
24 25 SubInProcessChannel, StdInInProcessChannel
@@ -65,7 +66,18 b' class BlockingSubInProcessChannel(BlockingChannelMixin, SubInProcessChannel):'
65 66 pass
66 67
67 68 class BlockingStdInInProcessChannel(BlockingChannelMixin, StdInInProcessChannel):
68 pass
69
70 def call_handlers(self, msg):
71 """ Overridden for the in-process channel.
72
73 This methods simply calls raw_input directly.
74 """
75 msg_type = msg['header']['msg_type']
76 if msg_type == 'input_request':
77 raw_input = self.manager.kernel.sys_raw_input
78 prompt = msg['content']['prompt']
79 raw_print(prompt, end='')
80 self.input(raw_input())
69 81
70 82 class BlockingInProcessKernelManager(InProcessKernelManager):
71 83
@@ -11,6 +11,8 b''
11 11 from __future__ import print_function
12 12
13 13 # Standard library imports
14 from StringIO import StringIO
15 import sys
14 16 import unittest
15 17
16 18 # Local imports
@@ -34,6 +36,21 b' class InProcessKernelTestCase(unittest.TestCase):'
34 36 msg = get_stream_message(km)
35 37 self.assert_('Welcome to pylab' in msg['content']['data'])
36 38
39 def test_raw_input(self):
40 """ Does the in-process kernel handle raw_input correctly?
41 """
42 km = BlockingInProcessKernelManager()
43 km.start_kernel()
44
45 io = StringIO('foobar\n')
46 sys_stdin = sys.stdin
47 sys.stdin = io
48 try:
49 km.shell_channel.execute('x = raw_input()')
50 finally:
51 sys.stdin = sys_stdin
52 self.assertEqual(km.kernel.shell.user_ns.get('x'), 'foobar')
53
37 54 def test_stdout(self):
38 55 """ Does the in-process kernel correctly capture IO?
39 56 """
@@ -84,6 +84,7 b' class Kernel(Configurable):'
84 84 control_stream = Instance(ZMQStream)
85 85 iopub_socket = Instance(zmq.Socket)
86 86 stdin_socket = Instance(zmq.Socket)
87 sys_raw_input = Any()
87 88 log = Instance(logging.Logger)
88 89
89 90 user_module = Any()
@@ -352,8 +353,10 b' class Kernel(Configurable):'
352 353 raw_input = lambda prompt='' : self._no_raw_input()
353 354
354 355 if py3compat.PY3:
356 self.sys_raw_input = __builtin__.input
355 357 __builtin__.input = raw_input
356 358 else:
359 self.sys_raw_input = __builtin__.raw_input
357 360 __builtin__.raw_input = raw_input
358 361
359 362 # Set the parent message of the display hook and out streams.
@@ -385,6 +388,12 b' class Kernel(Configurable):'
385 388 reply_content.update(shell._showtraceback(etype, evalue, tb_list))
386 389 else:
387 390 status = u'ok'
391 finally:
392 # Restore raw_input.
393 if py3compat.PY3:
394 __builtin__.input = self.sys_raw_input
395 else:
396 __builtin__.raw_input = self.sys_raw_input
388 397
389 398 reply_content[u'status'] = status
390 399
General Comments 0
You need to be logged in to leave comments. Login now