From 36b8363b4b0d4e18fabc2e0527fa6f22520d0ce8 2011-06-21 09:54:27 From: Thomas Kluyver Date: 2011-06-21 09:54:27 Subject: [PATCH] Arrange Str and Bytes traitlets to simplify automatic conversion to Python 3. --- diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index c4c8b12..6c7c7a9 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -952,24 +952,24 @@ class CComplex(Complex): 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 @@ class CUnicode(Unicode): 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):