Permalink
Browse files

use logging instead of `print >>` in pykernel

  • Loading branch information...
1 parent af50d03 commit 8ae3e9d83841878003a7a4ab54c36bd7985cc751 @minrk committed Jun 9, 2011
Showing with 16 additions and 19 deletions.
  1. +1 −0 IPython/zmq/kernelapp.py
  2. +15 −19 IPython/zmq/pykernel.py
@@ -192,6 +192,7 @@ def init_kernel(self):
shell_socket=self.shell_socket,
iopub_socket=self.iopub_socket,
stdin_socket=self.stdin_socket,
+ log=self.log
)
self.kernel.record_ports(self.ports)
@@ -51,6 +51,7 @@ class Kernel(HasTraits):
shell_socket = Instance('zmq.Socket')
iopub_socket = Instance('zmq.Socket')
stdin_socket = Instance('zmq.Socket')
+ log = Instance('logging.Logger')
def __init__(self, **kwargs):
super(Kernel, self).__init__(**kwargs)
@@ -73,11 +74,10 @@ def start(self):
ident,msg = self.session.recv(self.shell_socket,0)
assert ident is not None, "Missing message part."
omsg = Message(msg)
- print>>sys.__stdout__
- print>>sys.__stdout__, omsg
+ self.log.debug(str(omsg))
handler = self.handlers.get(omsg.msg_type, None)
if handler is None:
- print >> sys.__stderr__, "UNKNOWN MESSAGE TYPE:", omsg
+ self.log.error("UNKNOWN MESSAGE TYPE: %s"%omsg)
else:
handler(ident, omsg)
@@ -97,8 +97,7 @@ def execute_request(self, ident, parent):
try:
code = parent[u'content'][u'code']
except:
- print>>sys.__stderr__, "Got bad msg: "
- print>>sys.__stderr__, Message(parent)
+ self.log.error("Got bad msg: %s"%Message(parent))
return
pyin_msg = self.session.send(self.iopub_socket, u'pyin',{u'code':code}, parent=parent)
@@ -136,7 +135,7 @@ def execute_request(self, ident, parent):
# Send the reply.
reply_msg = self.session.send(self.shell_socket, u'execute_reply', reply_content, parent, ident=ident)
- print>>sys.__stdout__, Message(reply_msg)
+ self.log.debug(Message(reply_msg))
if reply_msg['content']['status'] == u'error':
self._abort_queue()
@@ -145,22 +144,22 @@ def complete_request(self, ident, parent):
'status' : 'ok'}
completion_msg = self.session.send(self.shell_socket, 'complete_reply',
matches, parent, ident)
- print >> sys.__stdout__, completion_msg
+ self.log.debug(completion_msg)
def object_info_request(self, ident, parent):
context = parent['content']['oname'].split('.')
object_info = self._object_info(context)
msg = self.session.send(self.shell_socket, 'object_info_reply',
object_info, parent, ident)
- print >> sys.__stdout__, msg
+ self.log.debug(msg)
def shutdown_request(self, ident, parent):
content = dict(parent['content'])
msg = self.session.send(self.shell_socket, 'shutdown_reply',
content, parent, ident)
msg = self.session.send(self.iopub_socket, 'shutdown_reply',
content, parent, ident)
- print >> sys.__stdout__, msg
+ self.log.debug(msg)
time.sleep(0.1)
sys.exit(0)
@@ -170,19 +169,17 @@ def shutdown_request(self, ident, parent):
def _abort_queue(self):
while True:
- try:
- ident,msg = self.session.recv(self.shell_socket, zmq.NOBLOCK)
- except zmq.ZMQError, e:
- if e.errno == zmq.EAGAIN:
- break
+ ident,msg = self.session.recv(self.shell_socket, zmq.NOBLOCK)
+ if msg is None:
+ # msg=None on EAGAIN
+ break
else:
assert ident is not None, "Missing message part."
- print>>sys.__stdout__, "Aborting:"
- print>>sys.__stdout__, Message(msg)
+ self.log.debug("Aborting: %s"%Message(msg))
msg_type = msg['msg_type']
reply_type = msg_type.split('_')[0] + '_reply'
reply_msg = self.session.send(self.shell_socket, reply_type, {'status':'aborted'}, msg, ident=ident)
- print>>sys.__stdout__, Message(reply_msg)
+ self.log.debug(Message(reply_msg))
# We need to wait a bit for requests to come in. This can probably
# be set shorter for true asynchronous clients.
time.sleep(0.1)
@@ -201,8 +198,7 @@ def _raw_input(self, prompt, ident, parent):
try:
value = reply['content']['value']
except:
- print>>sys.__stderr__, "Got bad raw_input reply: "
- print>>sys.__stderr__, Message(parent)
+ self.log.error("Got bad raw_input reply: %s"%Message(parent))
value = ''
return value

0 comments on commit 8ae3e9d

Please sign in to comment.