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