From 9deb416e023396617928cb68b4629dc17a95cf6a 2013-02-12 23:59:00 From: y-p Date: 2013-02-12 23:59:00 Subject: [PATCH] Don't die if stderr/stdout do not support set_parent() #2925 If the user redirects the streams to a file for example, the kernel would raise an exception because file objects do not have set_parent defined, unlike ipython's OutStream. This happens even if the user does this in a spawned thread. --- diff --git a/IPython/kernel/zmq/ipkernel.py b/IPython/kernel/zmq/ipkernel.py index b010143..d69ef9c 100755 --- a/IPython/kernel/zmq/ipkernel.py +++ b/IPython/kernel/zmq/ipkernel.py @@ -364,8 +364,14 @@ class Kernel(Configurable): shell.displayhook.set_parent(parent) shell.display_pub.set_parent(parent) shell.data_pub.set_parent(parent) - sys.stdout.set_parent(parent) - sys.stderr.set_parent(parent) + try: + sys.stdout.set_parent(parent) + except AttributeError: + pass + try: + sys.stderr.set_parent(parent) + except AttributeError: + pass # Re-broadcast our input for the benefit of listening clients, and # start computing output @@ -567,14 +573,20 @@ class Kernel(Configurable): return self._publish_status(u'busy', parent) - + # Set the parent message of the display hook and out streams. shell = self.shell shell.displayhook.set_parent(parent) shell.display_pub.set_parent(parent) shell.data_pub.set_parent(parent) - sys.stdout.set_parent(parent) - sys.stderr.set_parent(parent) + try: + sys.stdout.set_parent(parent) + except AttributeError: + pass + try: + sys.stderr.set_parent(parent) + except AttributeError: + pass # pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent) # self.iopub_socket.send(pyin_msg)