Repository: ipython/ipython

Issue:
IPython notebook not starting on winXP
Hi,

I am getting the following error when I try to start the Ipython notebook 

```
  Traceback (most recent call last):
 File "C:\Python26\lib\site-packages\zmq\eventloop\ioloop.py", line 354, in _run_callback
   callback()
 File "C:\Python26\lib\site-packages\zmq\eventloop\stack_context.py", line 133, in wrapped
   callback(*args, **kwargs)
 File "C:\Python26\lib\site-packages\tornado-2.2-py2.6.egg\tornado\iostream.py", line 304, in wrapper
   callback(*args)
 File "C:\Python26\lib\site-packages\tornado-2.2-py2.6.egg\tornado\httpserver.py", line 250, in _on_headers
   self.request_callback(self._request)
 File "C:\Python26\lib\site-packages\tornado-2.2-py2.6.egg\tornado\web.py", line 1362, in __call__
   handler._execute(transforms, *args, **kwargs)
  TypeError: _execute() keywords must be strings
```

please note that I am using ZMQ version 2.1.7

Thanks!



Discussion:
What version of tornado are you using?

Version 2.2

Thanks!

Does it work if you update to current (2.1.11) pyzmq?

No, that's not it. I found it:

http://stackoverflow.com/questions/4598604/how-to-pass-unicode-keywords-to-kwargs

Python version > 2.7 evidently is a requirement.

On Wednesday, February 01, 2012 4:05:29 PM, Min RK wrote:

> Does it work if you update to current (2.1.11) pyzmq?
> 
> ---
> 
> Reply to this email directly or view it on GitHub:
> https://github.com/ipython/ipython/issues/1360#issuecomment-3769968

Ah, yes, good catch.  As I understand it, it's not quite Python 2.7 that is required, but Python >= 2.6.5.

In any case, this bug was introduced by PR #1332, which made all of the handler patterns unicode, which results in unicode keys in the kwargs dict.

There are two fixes:
1. coerce our URL patterns to str.  This is probably the easiest, but _technically_ nonascii urls are valid, and I just don't know what would happen, or how to set up a situation to test that (e.g. a proxy with nonascii base URL)
2. tornado should coerce unicode keys to str [here](https://github.com/facebook/tornado/blob/master/tornado/web.py#L1345).

Since we obviously want to work with released tornado, we have to do something on our end to ensure that our URL patterns are not unicode.  The simplest possible answer is to simply enforce ascii, but that does break unlikely, if technically possible, use cases.


Resolving patch:
diff --git a/IPython/frontend/html/notebook/notebookapp.py b/IPython/frontend/html/notebook/notebookapp.py
--- a/IPython/frontend/html/notebook/notebookapp.py
+++ b/IPython/frontend/html/notebook/notebookapp.py
@@ -1,10 +1,10 @@
+# coding: utf-8
 """A tornado based IPython notebook server.
 
 Authors:
 
 * Brian Granger
 """
-
 #-----------------------------------------------------------------------------
 #  Copyright (C) 2008-2011  The IPython Development Team
 #
@@ -65,6 +65,7 @@
     IPKernelApp
 )
 from IPython.utils.traitlets import Dict, Unicode, Integer, List, Enum, Bool
+from IPython.utils import py3compat
 
 #-----------------------------------------------------------------------------
 # Module globals
@@ -130,6 +131,16 @@ def __init__(self, ipython_app, kernel_manager, notebook_manager, log,
         # allow custom overrides for the tornado web app.
         settings.update(settings_overrides)
 
+        # Python < 2.6.5 doesn't accept unicode keys in f(**kwargs), and
+        # base_project_url will always be unicode, which will in turn
+        # make the patterns unicode, and ultimately result in unicode
+        # keys in kwargs to handler._execute(**kwargs) in tornado.
+        # This enforces that base_project_url be ascii in that situation.
+        # 
+        # Note that the URLs these patterns check against are escaped,
+        # and thus guaranteed to be ASCII: 'héllo' is really 'h%C3%A9llo'.
+        base_project_url = py3compat.unicode_to_str(base_project_url, 'ascii')
+        
         # prepend base_project_url onto the patterns that we match
         new_handlers = []
         for handler in handlers:


Test patch: