From 6c5e8f35108c0a5312c47d201043f33007c7bb53 2013-12-02 21:01:47 From: Thomas Kluyver Date: 2013-12-02 21:01:47 Subject: [PATCH] More informative failure when passing bytes to a Unicode traitlet --- diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index 7b33e2f..31fb5cf 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -1024,7 +1024,11 @@ class Unicode(TraitType): if isinstance(value, py3compat.unicode_type): return value if isinstance(value, bytes): - return py3compat.unicode_type(value) + try: + return value.decode('ascii', 'strict') + except UnicodeDecodeError: + msg = "Could not decode {!r} for unicode trait '{}' of {} instance." + raise TraitError(msg.format(value, self.name, class_of(obj))) self.error(obj, value)