Permalink
Browse files

Arrange Str and Bytes traitlets to simplify automatic conversion to P…

…ython 3.
  • Loading branch information...
1 parent ef34a2f commit 36b8363b4b0d4e18fabc2e0527fa6f22520d0ce8 @takluyver committed May 22, 2011
Showing with 10 additions and 5 deletions.
  1. +10 −5 IPython/utils/traitlets.py
View
15 IPython/utils/traitlets.py
@@ -952,24 +952,24 @@ def validate (self, obj, value):
self.error(obj, value)
-class Str(TraitType):
+class Bytes(TraitType):
"""A trait for strings."""
default_value = ''
info_text = 'a string'
def validate(self, obj, value):
- if isinstance(value, str):
+ if isinstance(value, bytes):
return value
self.error(obj, value)
-class CStr(Str):
+class CBytes(Bytes):
"""A casting version of the string trait."""
def validate(self, obj, value):
try:
- return str(value)
+ return bytes(value)
except:
try:
return unicode(value)
@@ -986,7 +986,7 @@ class Unicode(TraitType):
def validate(self, obj, value):
if isinstance(value, unicode):
return value
- if isinstance(value, str):
+ if isinstance(value, bytes):
return unicode(value)
self.error(obj, value)
@@ -999,6 +999,11 @@ def validate(self, obj, value):
return unicode(value)
except:
self.error(obj, value)
+
+if sys.version_info[0] < 3:
+ Str, CStr = Bytes, CBytes
+else:
+ Str, CStr = Unicode, CUnicode
class Bool(TraitType):

0 comments on commit 36b8363

Please sign in to comment.