Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More Python 3 compatibility fixes.

  • Loading branch information...
commit 54a171839c9bdf6ea91e29062bc42af9ec93cbbc 1 parent ee492f8
@takluyver authored
View
6 IPython/utils/text.py
@@ -23,7 +23,7 @@
from string import Formatter
from IPython.external.path import path
-
+from IPython.utils import py3compat
from IPython.utils.io import nlprint
from IPython.utils.data import flatten
@@ -284,7 +284,7 @@ def make_quoted_expr(s):
tail = ''
tailpadding = ''
raw = ''
- ucode = 'u'
+ ucode = '' if py3compat.PY3 else 'u'
if "\\" in s:
raw = 'r'
if s.endswith('\\'):
@@ -494,7 +494,7 @@ def marquee(txt='',width=78,mark='*'):
"""
if not txt:
return (mark*width)[:width]
- nmark = (width-len(txt)-2)/len(mark)/2
+ nmark = (width-len(txt)-2)//len(mark)//2
if nmark < 0: nmark =0
marks = mark*nmark
return '%s %s %s' % (marks,txt,marks)
View
60 IPython/utils/traitlets.py
@@ -52,15 +52,17 @@
import re
import sys
import types
-from types import (
- InstanceType, ClassType, FunctionType,
- ListType, TupleType
-)
+from types import FunctionType
+try:
+ from types import ClassType, InstanceType
+ ClassTypes = (ClassType, type)
+except:
+ ClassTypes = (type,)
+
from .importstring import import_item
+from IPython.utils import py3compat
-ClassTypes = (ClassType, type)
-
-SequenceTypes = (ListType, TupleType, set, frozenset)
+SequenceTypes = (list, tuple, set, frozenset)
#-----------------------------------------------------------------------------
# Basic classes
@@ -108,7 +110,7 @@ def repr_type(obj):
error messages.
"""
the_type = type(obj)
- if the_type is InstanceType:
+ if (not py3compat.PY3) and the_type is InstanceType:
# Old-style class.
the_type = obj.__class__
msg = '%r %r' % (obj, the_type)
@@ -616,7 +618,7 @@ class ClassBasedTraitType(TraitType):
def error(self, obj, value):
kind = type(value)
- if kind is InstanceType:
+ if (not py3compat.PY3) and kind is InstanceType:
msg = 'class %s' % value.__class__.__name__
else:
msg = '%s (i.e. %s)' % ( str( kind )[1:-1], repr( value ) )
@@ -880,29 +882,29 @@ def validate(self, obj, value):
except:
self.error(obj, value)
+if not py3compat.PY3:
+ class Long(TraitType):
+ """A long integer trait."""
-class Long(TraitType):
- """A long integer trait."""
+ default_value = 0L
+ info_text = 'a long'
- default_value = 0L
- info_text = 'a long'
-
- def validate(self, obj, value):
- if isinstance(value, long):
- return value
- if isinstance(value, int):
- return long(value)
- self.error(obj, value)
+ def validate(self, obj, value):
+ if isinstance(value, long):
+ return value
+ if isinstance(value, int):
+ return long(value)
+ self.error(obj, value)
-class CLong(Long):
- """A casting version of the long integer trait."""
+ class CLong(Long):
+ """A casting version of the long integer trait."""
- def validate(self, obj, value):
- try:
- return long(value)
- except:
- self.error(obj, value)
+ def validate(self, obj, value):
+ try:
+ return long(value)
+ except:
+ self.error(obj, value)
class Float(TraitType):
@@ -955,7 +957,7 @@ def validate (self, obj, value):
# for Python 3 conversion and for reliable unicode behaviour on Python 2. So
# we don't have a Str type.
class Bytes(TraitType):
- """A trait for strings."""
+ """A trait for byte strings."""
default_value = ''
info_text = 'a string'
@@ -967,7 +969,7 @@ def validate(self, obj, value):
class CBytes(Bytes):
- """A casting version of the string trait."""
+ """A casting version of the byte string trait."""
def validate(self, obj, value):
try:
View
2  IPython/zmq/session.py
@@ -380,7 +380,7 @@ def sign(self, msg_list):
h = self.auth.copy()
for m in msg_list:
h.update(m)
- return h.hexdigest()
+ return str_to_bytes(h.hexdigest())
def serialize(self, msg, ident=None):
"""Serialize the message components to bytes.
Please sign in to comment.
Something went wrong with that request. Please try again.