From 2467d2edf7939ff2322c438423fd659ffae165b5 2011-09-07 11:22:32
From: Thomas Kluyver <takowl@gmail.com>
Date: 2011-09-07 11:22:32
Subject: [PATCH] Various Python 3 fixes in IPython.utils

---

diff --git a/IPython/utils/jsonutil.py b/IPython/utils/jsonutil.py
index 26cd389..06ccb3d 100644
--- a/IPython/utils/jsonutil.py
+++ b/IPython/utils/jsonutil.py
@@ -16,6 +16,9 @@ import sys
 import types
 from datetime import datetime
 
+from IPython.utils import py3compat
+next_attr_name = '__next__' if py3compat.PY3 else 'next'
+
 #-----------------------------------------------------------------------------
 # Globals and constants
 #-----------------------------------------------------------------------------
@@ -134,7 +137,7 @@ def json_clean(obj):
         return obj.decode(sys.getdefaultencoding(), 'replace')
     
     if isinstance(obj, container_to_list) or (
-        hasattr(obj, '__iter__') and hasattr(obj, 'next')):
+        hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)):
         obj = list(obj)
         
     if isinstance(obj, list):
diff --git a/IPython/utils/process.py b/IPython/utils/process.py
index a26024f..f317e34 100644
--- a/IPython/utils/process.py
+++ b/IPython/utils/process.py
@@ -66,7 +66,7 @@ def find_cmd(cmd):
     except OSError:
         raise FindCmdError('command could not be found: %s' % cmd)
     # which returns empty if not found
-    if path == '':
+    if path == b'':
         raise FindCmdError('command could not be found: %s' % cmd)
     return os.path.abspath(path)
 
diff --git a/IPython/utils/tests/test_path.py b/IPython/utils/tests/test_path.py
index 01ad730..8b9b27e 100644
--- a/IPython/utils/tests/test_path.py
+++ b/IPython/utils/tests/test_path.py
@@ -31,17 +31,24 @@ from IPython.testing import decorators as dec
 from IPython.testing.decorators import skip_if_not_win32, skip_win32
 from IPython.testing.tools import make_tempfile
 from IPython.utils import path, io
+from IPython.utils import py3compat
 
 # Platform-dependent imports
 try:
     import _winreg as wreg
 except ImportError:
     #Fake _winreg module on none windows platforms
-    import new
-    sys.modules["_winreg"] = new.module("_winreg")
+    import types
+    wr_name = "winreg" if py3compat.PY3 else "_winreg"
+    sys.modules[wr_name] = types.ModuleType(wr_name)
     import _winreg as wreg
     #Add entries that needs to be stubbed by the testing code
     (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
+    
+try:
+    reload
+except NameError:   # Python 3
+    from imp import reload
 
 #-----------------------------------------------------------------------------
 # Globals
diff --git a/IPython/utils/tests/test_process.py b/IPython/utils/tests/test_process.py
index cbde427..d40dc46 100644
--- a/IPython/utils/tests/test_process.py
+++ b/IPython/utils/tests/test_process.py
@@ -37,7 +37,7 @@ def test_find_cmd_python():
 def test_find_cmd_ls():
     """Make sure we can find the full path to ls."""
     path = find_cmd('ls')
-    nt.assert_true(path.endswith('ls'))
+    nt.assert_true(path.endswith(b'ls'))
 
     
 def has_pywin32():
diff --git a/IPython/utils/text.py b/IPython/utils/text.py
index 0ec91ef..bf4a143 100644
--- a/IPython/utils/text.py
+++ b/IPython/utils/text.py
@@ -587,10 +587,10 @@ class EvalFormatter(Formatter):
     --------
     
     In [1]: f = EvalFormatter()
-    In [2]: f.format('{n/4}', n=8)
+    In [2]: f.format('{n//4}', n=8)
     Out[2]: '2'
     
-    In [3]: f.format('{range(3)}')
+    In [3]: f.format('{list(range(3))}')
     Out[3]: '[0, 1, 2]'
 
     In [4]: f.format('{3*2}')
diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py
index cb5c34a..4926a9f 100644
--- a/IPython/utils/traitlets.py
+++ b/IPython/utils/traitlets.py
@@ -882,7 +882,9 @@ class CInt(Int):
         except:
             self.error(obj, value)
 
-if not py3compat.PY3:
+if py3compat.PY3:
+    Long, CLong = Int, CInt
+else:
     class Long(TraitType):
         """A long integer trait."""