Repository: ipython/ipython

Issue:
input() in notebook doesn't work in Python 3
raw_input() in Python 2 brings up a text box, which returns when I hit enter. `input()` in Python 3 leaves the kernel waiting for a ZeroMQ reply, but doesn't show me the UI for the input line. When I interrupt the kernel, I get this traceback:

```
---------------------------------------------------------------------------
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: 
```



Discussion:
any message in the js console?

Timestamp: 29/06/13 23:57:50
Warning: Empty string passed to getElementById().
Source File: http://127.0.0.1:8888/static/components/jquery/jquery.min.js
Line: 4

That's from Firefox's "Error Console" - is there a way to get more info, like a stack trace?

I should also have mentioned that there's a whole series of that same error message. We have to vacate the room now, though.

No worries, thanks.  I will dig into it tomorrow (off to a wedding reception now-ish).

Ah, that's a warning, not an exception, that must be why I can't get anything like a stack trace out of it. Still not sure how I can debug the frontend stuff. I had a play with the Firefox dev tools, but I couldn't get into anything relevant.


Resolving patch:
diff --git a/IPython/html/base/zmqhandlers.py b/IPython/html/base/zmqhandlers.py
--- a/IPython/html/base/zmqhandlers.py
+++ b/IPython/html/base/zmqhandlers.py
@@ -25,7 +25,7 @@
 
 from IPython.kernel.zmq.session import Session
 from IPython.utils.jsonutil import date_default
-from IPython.utils.py3compat import PY3
+from IPython.utils.py3compat import PY3, cast_unicode
 
 from .handlers import IPythonHandler
 
@@ -83,7 +83,7 @@ def allow_draft76(self):
 class AuthenticatedZMQStreamHandler(ZMQStreamHandler, IPythonHandler):
 
     def open(self, kernel_id):
-        self.kernel_id = kernel_id.decode('ascii')
+        self.kernel_id = cast_unicode(kernel_id, 'ascii')
         self.session = Session(config=self.config)
         self.save_on_message = self.on_message
         self.on_message = self.on_first_message
@@ -97,7 +97,7 @@ def _inject_cookie_message(self, msg):
             msg = msg.encode('utf8', 'replace')
         try:
             identity, msg = msg.split(':', 1)
-            self.session.session = identity.decode('ascii')
+            self.session.session = cast_unicode(identity, 'ascii')
         except Exception:
             logging.error("First ws message didn't have the form 'identity:[cookie]' - %r", msg)
         


Test patch: