# Buggy code
def _inject_cookie_message(self, msg):
    """Inject the first message, which is the document cookie,
    for authentication."""
    if not PY3 and isinstance(msg, unicode):
        # Cookie constructor doesn't accept unicode strings
        # under Python 2.x for some reason
        msg = msg.encode("utf8", "replace")
    try:
        identity, msg = msg.split(":", 1)
        self.session.session = identity.decode("ascii")
    except Exception:
        logging.error(
            "First ws message didn't have the form 'identity:[cookie]' - %r", msg
        )

    try:
        self.request._cookies = Cookie.SimpleCookie(msg)
    except:
        self.log.warn("couldn't parse cookie string: %s", msg, exc_info=True)


# Traceback (zmq.ZMQError)
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-1-9b8a430d8199> in <module>()
----> 1 a = input()

/home/thomas/Code/virtualenvs/ipy-3/lib/python3.3/site-packages/ipython-1.0.dev-py3.3.egg/IPython/kernel/zmq/ipkernel.py in <lambda>(prompt)
353         # raw_input in the user namespace.
354         if content.get('allow_stdin', False):
--> 355             raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
356         else:
357             raw_input = lambda prompt='' : self._no_raw_input()

/home/thomas/Code/virtualenvs/ipy-3/lib/python3.3/site-packages/ipython-1.0.dev-py3.3.egg/IPython/kernel/zmq/ipkernel.py in _raw_input(self, prompt, ident, parent)
765         while True:
766             try:
--> 767                 ident, reply = self.session.recv(self.stdin_socket, 0)
768             except Exception:
769                 self.log.warn("Invalid Message:", exc_info=True)

/home/thomas/Code/virtualenvs/ipy-3/lib/python3.3/site-packages/ipython-1.0.dev-py3.3.egg/IPython/kernel/zmq/session.py in recv(self, socket, mode, content, copy)
657             socket = socket.socket
658         try:
--> 659             msg_list = socket.recv_multipart(mode, copy=copy)
660         except zmq.ZMQError as e:
661             if e.errno == zmq.EAGAIN:

/usr/lib/python3/dist-packages/zmq/core/pysocket.py in recv_multipart(self, flags, copy, track)
208
209     """
--> 210     parts = [self.recv(flags, copy=copy, track=track)]
211     # have first part already, only loop while more to receive
212     while self.getsockopt(zmq.RCVMORE):

/usr/lib/python3/dist-packages/zmq/core/socket.cpython-33m-i386-linux-gnu.so in zmq.core.socket.Socket.recv (zmq/core/socket.c:5643)()

/usr/lib/python3/dist-packages/zmq/core/socket.cpython-33m-i386-linux-gnu.so in zmq.core.socket.Socket.recv (zmq/core/socket.c:5465)()

/usr/lib/python3/dist-packages/zmq/core/socket.cpython-33m-i386-linux-gnu.so in zmq.core.socket._recv_copy (zmq/core/socket.c:1688)()

/usr/lib/python3/dist-packages/zmq/core/error.cpython-33m-i386-linux-gnu.so in zmq.core.error.ZMQError.__init__ (zmq/core/error.c:1059)()

KeyboardInterrupt:

# Fixed code
def _inject_cookie_message(self, msg):
    """Inject the first message, which is the document cookie,
    for authentication."""
    if not PY3 and isinstance(msg, unicode):
        # Cookie constructor doesn't accept unicode strings
        # under Python 2.x for some reason
        msg = msg.encode("utf8", "replace")
    try:
        identity, msg = msg.split(":", 1)
        self.session.session = cast_unicode(identity, "ascii")
    except Exception:
        logging.error(
            "First ws message didn't have the form 'identity:[cookie]' - %r", msg
        )

    try:
        self.request._cookies = Cookie.SimpleCookie(msg)
    except:
        self.log.warn("couldn't parse cookie string: %s", msg, exc_info=True)