##// END OF EJS Templates
Improve io.rprint* interface, unify usage in ipkernel....
Fernando Perez -
Show More
@@ -9,6 +9,7 b' IO related utilities.'
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 from __future__ import print_function
12
13
13 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
14 # Imports
15 # Imports
@@ -38,13 +39,13 b' class IOStream:'
38 except:
39 except:
39 try:
40 try:
40 # print handles some unicode issues which may trip a plain
41 # print handles some unicode issues which may trip a plain
41 # write() call. Attempt to emulate write() by using a
42 # write() call. Emulate write() by using an empty end
42 # trailing comma
43 # argument.
43 print >> self.stream, data,
44 print(data, end='', file=self.stream)
44 except:
45 except:
45 # if we get here, something is seriously broken.
46 # if we get here, something is seriously broken.
46 print >> sys.stderr, \
47 print('ERROR - failed to write data to stream:', self.stream,
47 'ERROR - failed to write data to stream:', self.stream
48 file=sys.stderr)
48
49
49 # This class used to have a writeln method, but regular files and streams
50 # This class used to have a writeln method, but regular files and streams
50 # in Python don't have this method. We need to keep this completely
51 # in Python don't have this method. We need to keep this completely
@@ -240,7 +241,7 b' class NLprinter:'
240 start = kw['start']; del kw['start']
241 start = kw['start']; del kw['start']
241 stop = kw['stop']; del kw['stop']
242 stop = kw['stop']; del kw['stop']
242 if self.depth == 0 and 'header' in kw.keys():
243 if self.depth == 0 and 'header' in kw.keys():
243 print kw['header']
244 print(kw['header'])
244
245
245 for idx in range(start,stop):
246 for idx in range(start,stop):
246 elem = lst[idx]
247 elem = lst[idx]
@@ -277,10 +278,17 b" def temp_pyfile(src, ext='.py'):"
277 return fname, f
278 return fname, f
278
279
279
280
280 def rprint(*info):
281 def rprint(*args, **kw):
282 """Raw print to sys.__stdout__"""
283
284 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
285 file=sys.__stdout__)
286 sys.__stdout__.flush()
287
288
289 def rprinte(*args, **kw):
281 """Raw print to sys.__stderr__"""
290 """Raw print to sys.__stderr__"""
282
291
283 for item in info:
292 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
284 print >> sys.__stderr__, item,
293 file=sys.__stderr__)
285 print >> sys.__stderr__
286 sys.__stderr__.flush()
294 sys.__stderr__.flush()
@@ -13,6 +13,7 b' Things to do:'
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from __future__ import print_function
16
17
17 # Standard library imports.
18 # Standard library imports.
18 import __builtin__
19 import __builtin__
@@ -25,6 +26,7 b' import zmq'
25
26
26 # Local imports.
27 # Local imports.
27 from IPython.config.configurable import Configurable
28 from IPython.config.configurable import Configurable
29 from IPython.utils import io
28 from IPython.utils.traitlets import Instance
30 from IPython.utils.traitlets import Instance
29 from completer import KernelCompleter
31 from completer import KernelCompleter
30 from entry_point import base_launch_kernel, make_argument_parser, make_kernel, \
32 from entry_point import base_launch_kernel, make_argument_parser, make_kernel, \
@@ -126,11 +128,10 b' class Kernel(Configurable):'
126 assert self.reply_socket.rcvmore(), "Missing message part."
128 assert self.reply_socket.rcvmore(), "Missing message part."
127 msg = self.reply_socket.recv_json()
129 msg = self.reply_socket.recv_json()
128 omsg = Message(msg)
130 omsg = Message(msg)
129 print>>sys.__stdout__
131 io.rprint('\n', omsg)
130 print>>sys.__stdout__, omsg
131 handler = self.handlers.get(omsg.msg_type, None)
132 handler = self.handlers.get(omsg.msg_type, None)
132 if handler is None:
133 if handler is None:
133 print >> sys.__stderr__, "UNKNOWN MESSAGE TYPE:", omsg
134 io.rprinte("UNKNOWN MESSAGE TYPE:", omsg)
134 else:
135 else:
135 handler(ident, omsg)
136 handler(ident, omsg)
136
137
@@ -142,8 +143,8 b' class Kernel(Configurable):'
142 try:
143 try:
143 code = parent[u'content'][u'code']
144 code = parent[u'content'][u'code']
144 except:
145 except:
145 print>>sys.__stderr__, "Got bad msg: "
146 io.rprinte("Got bad msg: ")
146 print>>sys.__stderr__, Message(parent)
147 io.rprinte(Message(parent))
147 return
148 return
148 pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent)
149 pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent)
149 self.pub_socket.send_json(pyin_msg)
150 self.pub_socket.send_json(pyin_msg)
@@ -200,25 +201,27 b' class Kernel(Configurable):'
200
201
201 # Send the reply.
202 # Send the reply.
202 reply_msg = self.session.msg(u'execute_reply', reply_content, parent)
203 reply_msg = self.session.msg(u'execute_reply', reply_content, parent)
203 print>>sys.__stdout__, Message(reply_msg)
204 io.rprint(Message(reply_msg))
204 self.reply_socket.send(ident, zmq.SNDMORE)
205 self.reply_socket.send(ident, zmq.SNDMORE)
205 self.reply_socket.send_json(reply_msg)
206 self.reply_socket.send_json(reply_msg)
206 if reply_msg['content']['status'] == u'error':
207 if reply_msg['content']['status'] == u'error':
207 self._abort_queue()
208 self._abort_queue()
208
209
209 def complete_request(self, ident, parent):
210 def complete_request(self, ident, parent):
210 matches = {'matches' : self._complete(parent),
211 txt, matches = self._complete(parent)
212 matches = {'matches' : matches,
213 'matched_text' : txt,
211 'status' : 'ok'}
214 'status' : 'ok'}
212 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
215 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
213 matches, parent, ident)
216 matches, parent, ident)
214 print >> sys.__stdout__, completion_msg
217 io.rprint(completion_msg)
215
218
216 def object_info_request(self, ident, parent):
219 def object_info_request(self, ident, parent):
217 context = parent['content']['oname'].split('.')
220 context = parent['content']['oname'].split('.')
218 object_info = self._object_info(context)
221 object_info = self._object_info(context)
219 msg = self.session.send(self.reply_socket, 'object_info_reply',
222 msg = self.session.send(self.reply_socket, 'object_info_reply',
220 object_info, parent, ident)
223 object_info, parent, ident)
221 print >> sys.__stdout__, msg
224 io.rprint(msg)
222
225
223 def prompt_request(self, ident, parent):
226 def prompt_request(self, ident, parent):
224 prompt_number = self.shell.displayhook.prompt_count
227 prompt_number = self.shell.displayhook.prompt_count
@@ -228,7 +231,7 b' class Kernel(Configurable):'
228 'input_sep' : self.shell.displayhook.input_sep}
231 'input_sep' : self.shell.displayhook.input_sep}
229 msg = self.session.send(self.reply_socket, 'prompt_reply',
232 msg = self.session.send(self.reply_socket, 'prompt_reply',
230 content, parent, ident)
233 content, parent, ident)
231 print >> sys.__stdout__, msg
234 io.rprint(msg)
232
235
233 def history_request(self, ident, parent):
236 def history_request(self, ident, parent):
234 output = parent['content']['output']
237 output = parent['content']['output']
@@ -238,7 +241,7 b' class Kernel(Configurable):'
238 content = {'history' : hist}
241 content = {'history' : hist}
239 msg = self.session.send(self.reply_socket, 'history_reply',
242 msg = self.session.send(self.reply_socket, 'history_reply',
240 content, parent, ident)
243 content, parent, ident)
241 print >> sys.__stdout__, msg
244 io.rprint(msg)
242
245
243 #---------------------------------------------------------------------------
246 #---------------------------------------------------------------------------
244 # Protected interface
247 # Protected interface
@@ -254,12 +257,11 b' class Kernel(Configurable):'
254 else:
257 else:
255 assert self.reply_socket.rcvmore(), "Unexpected missing message part."
258 assert self.reply_socket.rcvmore(), "Unexpected missing message part."
256 msg = self.reply_socket.recv_json()
259 msg = self.reply_socket.recv_json()
257 print>>sys.__stdout__, "Aborting:"
260 io.rprint("Aborting:\n", Message(msg))
258 print>>sys.__stdout__, Message(msg)
259 msg_type = msg['msg_type']
261 msg_type = msg['msg_type']
260 reply_type = msg_type.split('_')[0] + '_reply'
262 reply_type = msg_type.split('_')[0] + '_reply'
261 reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg)
263 reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg)
262 print>>sys.__stdout__, Message(reply_msg)
264 io.rprint(Message(reply_msg))
263 self.reply_socket.send(ident,zmq.SNDMORE)
265 self.reply_socket.send(ident,zmq.SNDMORE)
264 self.reply_socket.send_json(reply_msg)
266 self.reply_socket.send_json(reply_msg)
265 # We need to wait a bit for requests to come in. This can probably
267 # We need to wait a bit for requests to come in. This can probably
@@ -281,23 +283,22 b' class Kernel(Configurable):'
281 try:
283 try:
282 value = reply['content']['value']
284 value = reply['content']['value']
283 except:
285 except:
284 print>>sys.__stderr__, "Got bad raw_input reply: "
286 io.rprinte("Got bad raw_input reply: ")
285 print>>sys.__stderr__, Message(parent)
287 io.rprinte(Message(parent))
286 value = ''
288 value = ''
287 return value
289 return value
288
290
289 def _complete(self, msg):
291 def _complete(self, msg):
290 #from IPython.utils.io import rprint # dbg
291 #rprint('\n\n**MSG**\n\n', msg) # dbg
292 #import traceback; rprint(''.join(traceback.format_stack())) # dbg
293 c = msg['content']
292 c = msg['content']
294 try:
293 try:
295 cpos = int(c['cursor_pos'])
294 cpos = int(c['cursor_pos'])
296 except:
295 except:
297 # If we don't get something that we can convert to an integer, at
296 # If we don't get something that we can convert to an integer, at
298 # leasat attempt the completion guessing the cursor is at the end
297 # least attempt the completion guessing the cursor is at the end of
299 # of the text
298 # the text, if there's any, and otherwise of the line
300 cpos = len(c['text'])
299 cpos = len(c['text'])
300 if cpos==0:
301 cpos = len(c['line'])
301 return self.shell.complete(c['text'], c['line'], cpos)
302 return self.shell.complete(c['text'], c['line'], cpos)
302
303
303 def _object_info(self, context):
304 def _object_info(self, context):
General Comments 0
You need to be logged in to leave comments. Login now