##// END OF EJS Templates
Fixed bugs with raw_input and finished and implementing InStream.
epatters -
Show More
@@ -715,7 +715,7 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
715 'is not visible!')
715 'is not visible!')
716
716
717 self._reading = True
717 self._reading = True
718 self._show_prompt(prompt)
718 self._show_prompt(prompt, newline=False)
719
719
720 if callback is None:
720 if callback is None:
721 self._reading_callback = None
721 self._reading_callback = None
@@ -739,18 +739,25 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
739 """
739 """
740 self.setTextCursor(self._get_selection_cursor(start, end))
740 self.setTextCursor(self._get_selection_cursor(start, end))
741
741
742 def _show_prompt(self, prompt=None):
742 def _show_prompt(self, prompt=None, newline=True):
743 """ Writes a new prompt at the end of the buffer. If 'prompt' is not
743 """ Writes a new prompt at the end of the buffer.
744 specified, the previous prompt is used.
744
745 """
745 Parameters
746 # Use QTextDocumentFragment intermediate object because it strips
746 ----------
747 # out the Unicode line break characters that Qt insists on inserting.
747 prompt : str, optional
748 cursor = self._get_end_cursor()
748 The prompt to show. If not specified, the previous prompt is used.
749 if cursor.position() > 0:
749
750 cursor.movePosition(QtGui.QTextCursor.Left,
750 newline : bool, optional (default True)
751 QtGui.QTextCursor.KeepAnchor)
751 If set, a new line will be written before showing the prompt if
752 if str(cursor.selection().toPlainText()) != '\n':
752 there is not already a newline at the end of the buffer.
753 self.appendPlainText('\n')
753 """
754 if newline:
755 cursor = self._get_end_cursor()
756 if cursor.position() > 0:
757 cursor.movePosition(QtGui.QTextCursor.Left,
758 QtGui.QTextCursor.KeepAnchor)
759 if str(cursor.selection().toPlainText()) != '\n':
760 self.appendPlainText('\n')
754
761
755 if prompt is not None:
762 if prompt is not None:
756 self._prompt = prompt
763 self._prompt = prompt
@@ -143,12 +143,12 b' class FrontendWidget(HistoryConsoleWidget):'
143 # 'ConsoleWidget' protected interface
143 # 'ConsoleWidget' protected interface
144 #---------------------------------------------------------------------------
144 #---------------------------------------------------------------------------
145
145
146 def _show_prompt(self, prompt=None):
146 def _show_prompt(self, prompt=None, newline=True):
147 """ Reimplemented to set a default prompt.
147 """ Reimplemented to set a default prompt.
148 """
148 """
149 if prompt is None:
149 if prompt is None:
150 prompt = '>>> '
150 prompt = '>>> '
151 super(FrontendWidget, self)._show_prompt(prompt)
151 super(FrontendWidget, self)._show_prompt(prompt, newline)
152
152
153 #---------------------------------------------------------------------------
153 #---------------------------------------------------------------------------
154 # 'FrontendWidget' interface
154 # 'FrontendWidget' interface
@@ -297,6 +297,10 b' class FrontendWidget(HistoryConsoleWidget):'
297 self._call_tip()
297 self._call_tip()
298
298
299 def _handle_req(self, req):
299 def _handle_req(self, req):
300 # Make sure that all output from the SUB channel has been processed
301 # before entering readline mode.
302 self.kernel_manager.sub_channel.flush()
303
300 def callback(line):
304 def callback(line):
301 self.kernel_manager.rep_channel.readline(line)
305 self.kernel_manager.rep_channel.readline(line)
302 self._readline(callback=callback)
306 self._readline(callback=callback)
@@ -13,11 +13,11 b' Things to do:'
13
13
14 # Standard library imports.
14 # Standard library imports.
15 import __builtin__
15 import __builtin__
16 from code import CommandCompiler
16 import os
17 import os
17 import sys
18 import sys
18 import time
19 import time
19 import traceback
20 import traceback
20 from code import CommandCompiler
21
21
22 # System library imports.
22 # System library imports.
23 import zmq
23 import zmq
@@ -40,7 +40,7 b' class InStream(object):'
40
40
41 def flush(self):
41 def flush(self):
42 if self.socket is None:
42 if self.socket is None:
43 raise ValueError(u'I/O operation on closed file')
43 raise ValueError('I/O operation on closed file')
44
44
45 def isatty(self):
45 def isatty(self):
46 return False
46 return False
@@ -49,19 +49,33 b' class InStream(object):'
49 raise IOError('Seek not supported.')
49 raise IOError('Seek not supported.')
50
50
51 def read(self, size=-1):
51 def read(self, size=-1):
52 raise NotImplementedError
52 # FIXME: Do we want another request for this?
53 string = '\n'.join(self.readlines())
54 return self._truncate(string, size)
53
55
54 def readline(self, size=-1):
56 def readline(self, size=-1):
55 if self.socket is None:
57 if self.socket is None:
56 raise ValueError(u'I/O operation on closed file')
58 raise ValueError('I/O operation on closed file')
57 else:
59 else:
58 content = dict(size=size)
60 content = dict(size=size)
59 msg = self.session.msg('readline_request', content=content)
61 msg = self.session.msg('readline_request', content=content)
60 reply = self._request(msg)
62 reply = self._request(msg)
61 return reply['content']['line']
63 line = reply['content']['line']
64 return self._truncate(line, size)
62
65
63 def readlines(self, size=-1):
66 def readlines(self, sizehint=-1):
64 raise NotImplementedError
67 # Sizehint is ignored, as is permitted.
68 if self.socket is None:
69 raise ValueError('I/O operation on closed file')
70 else:
71 lines = []
72 while True:
73 line = self.readline()
74 if line:
75 lines.append(line)
76 else:
77 break
78 return lines
65
79
66 def seek(self, offset, whence=None):
80 def seek(self, offset, whence=None):
67 raise IOError('Seek not supported.')
81 raise IOError('Seek not supported.')
@@ -73,6 +87,11 b' class InStream(object):'
73 raise IOError('Write not supported on a read only stream.')
87 raise IOError('Write not supported on a read only stream.')
74
88
75 def _request(self, msg):
89 def _request(self, msg):
90 # Flush output before making the request. This ensures, for example,
91 # that raw_input(prompt) actually gets a prompt written.
92 sys.stderr.flush()
93 sys.stdout.flush()
94
76 self.socket.send_json(msg)
95 self.socket.send_json(msg)
77 while True:
96 while True:
78 try:
97 try:
@@ -86,6 +105,15 b' class InStream(object):'
86 break
105 break
87 return reply
106 return reply
88
107
108 def _truncate(self, string, size):
109 if size >= 0:
110 if isinstance(string, str):
111 return string[:size]
112 elif isinstance(string, unicode):
113 encoded = string.encode('utf-8')[:size]
114 return encoded.decode('utf-8', 'ignore')
115 return string
116
89
117
90 class OutStream(object):
118 class OutStream(object):
91 """A file like object that publishes the stream to a 0MQ PUB socket."""
119 """A file like object that publishes the stream to a 0MQ PUB socket."""
General Comments 0
You need to be logged in to leave comments. Login now