##// 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 from threading import Event
19 from threading import Event
20
20
21 # Local imports.
21 # Local imports.
22 from IPython.utils.io import raw_print
22 from IPython.utils.traitlets import Type
23 from IPython.utils.traitlets import Type
23 from kernelmanager import InProcessKernelManager, ShellInProcessChannel, \
24 from kernelmanager import InProcessKernelManager, ShellInProcessChannel, \
24 SubInProcessChannel, StdInInProcessChannel
25 SubInProcessChannel, StdInInProcessChannel
@@ -65,7 +66,18 b' class BlockingSubInProcessChannel(BlockingChannelMixin, SubInProcessChannel):'
65 pass
66 pass
66
67
67 class BlockingStdInInProcessChannel(BlockingChannelMixin, StdInInProcessChannel):
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 class BlockingInProcessKernelManager(InProcessKernelManager):
82 class BlockingInProcessKernelManager(InProcessKernelManager):
71
83
@@ -11,6 +11,8 b''
11 from __future__ import print_function
11 from __future__ import print_function
12
12
13 # Standard library imports
13 # Standard library imports
14 from StringIO import StringIO
15 import sys
14 import unittest
16 import unittest
15
17
16 # Local imports
18 # Local imports
@@ -34,6 +36,21 b' class InProcessKernelTestCase(unittest.TestCase):'
34 msg = get_stream_message(km)
36 msg = get_stream_message(km)
35 self.assert_('Welcome to pylab' in msg['content']['data'])
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 def test_stdout(self):
54 def test_stdout(self):
38 """ Does the in-process kernel correctly capture IO?
55 """ Does the in-process kernel correctly capture IO?
39 """
56 """
@@ -84,6 +84,7 b' class Kernel(Configurable):'
84 control_stream = Instance(ZMQStream)
84 control_stream = Instance(ZMQStream)
85 iopub_socket = Instance(zmq.Socket)
85 iopub_socket = Instance(zmq.Socket)
86 stdin_socket = Instance(zmq.Socket)
86 stdin_socket = Instance(zmq.Socket)
87 sys_raw_input = Any()
87 log = Instance(logging.Logger)
88 log = Instance(logging.Logger)
88
89
89 user_module = Any()
90 user_module = Any()
@@ -352,8 +353,10 b' class Kernel(Configurable):'
352 raw_input = lambda prompt='' : self._no_raw_input()
353 raw_input = lambda prompt='' : self._no_raw_input()
353
354
354 if py3compat.PY3:
355 if py3compat.PY3:
356 self.sys_raw_input = __builtin__.input
355 __builtin__.input = raw_input
357 __builtin__.input = raw_input
356 else:
358 else:
359 self.sys_raw_input = __builtin__.raw_input
357 __builtin__.raw_input = raw_input
360 __builtin__.raw_input = raw_input
358
361
359 # Set the parent message of the display hook and out streams.
362 # Set the parent message of the display hook and out streams.
@@ -385,6 +388,12 b' class Kernel(Configurable):'
385 reply_content.update(shell._showtraceback(etype, evalue, tb_list))
388 reply_content.update(shell._showtraceback(etype, evalue, tb_list))
386 else:
389 else:
387 status = u'ok'
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 reply_content[u'status'] = status
398 reply_content[u'status'] = status
390
399
General Comments 0
You need to be logged in to leave comments. Login now