Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2467d2e

Browse files
committed
Various Python 3 fixes in IPython.utils
1 parent 5299658 commit 2467d2e

6 files changed

Lines changed: 20 additions & 8 deletions

File tree

IPython/utils/jsonutil.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import types
1717
from datetime import datetime
1818

19+
from IPython.utils import py3compat
20+
next_attr_name = '__next__' if py3compat.PY3 else 'next'
21+
1922
#-----------------------------------------------------------------------------
2023
# Globals and constants
2124
#-----------------------------------------------------------------------------
@@ -134,7 +137,7 @@ def json_clean(obj):
134137
return obj.decode(sys.getdefaultencoding(), 'replace')
135138

136139
if isinstance(obj, container_to_list) or (
137-
hasattr(obj, '__iter__') and hasattr(obj, 'next')):
140+
hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)):
138141
obj = list(obj)
139142

140143
if isinstance(obj, list):

IPython/utils/process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def find_cmd(cmd):
6666
except OSError:
6767
raise FindCmdError('command could not be found: %s' % cmd)
6868
# which returns empty if not found
69-
if path == '':
69+
if path == b'':
7070
raise FindCmdError('command could not be found: %s' % cmd)
7171
return os.path.abspath(path)
7272

IPython/utils/tests/test_path.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,24 @@
3131
from IPython.testing.decorators import skip_if_not_win32, skip_win32
3232
from IPython.testing.tools import make_tempfile
3333
from IPython.utils import path, io
34+
from IPython.utils import py3compat
3435

3536
# Platform-dependent imports
3637
try:
3738
import _winreg as wreg
3839
except ImportError:
3940
#Fake _winreg module on none windows platforms
40-
import new
41-
sys.modules["_winreg"] = new.module("_winreg")
41+
import types
42+
wr_name = "winreg" if py3compat.PY3 else "_winreg"
43+
sys.modules[wr_name] = types.ModuleType(wr_name)
4244
import _winreg as wreg
4345
#Add entries that needs to be stubbed by the testing code
4446
(wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
47+
48+
try:
49+
reload
50+
except NameError: # Python 3
51+
from imp import reload
4552

4653
#-----------------------------------------------------------------------------
4754
# Globals

IPython/utils/tests/test_process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_find_cmd_python():
3737
def test_find_cmd_ls():
3838
"""Make sure we can find the full path to ls."""
3939
path = find_cmd('ls')
40-
nt.assert_true(path.endswith('ls'))
40+
nt.assert_true(path.endswith(b'ls'))
4141

4242

4343
def has_pywin32():

IPython/utils/text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,10 @@ class EvalFormatter(Formatter):
587587
--------
588588
589589
In [1]: f = EvalFormatter()
590-
In [2]: f.format('{n/4}', n=8)
590+
In [2]: f.format('{n//4}', n=8)
591591
Out[2]: '2'
592592
593-
In [3]: f.format('{range(3)}')
593+
In [3]: f.format('{list(range(3))}')
594594
Out[3]: '[0, 1, 2]'
595595
596596
In [4]: f.format('{3*2}')

IPython/utils/traitlets.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,9 @@ def validate(self, obj, value):
882882
except:
883883
self.error(obj, value)
884884

885-
if not py3compat.PY3:
885+
if py3compat.PY3:
886+
Long, CLong = Int, CInt
887+
else:
886888
class Long(TraitType):
887889
"""A long integer trait."""
888890

0 commit comments

Comments
 (0)