-
-
Save anonymous/5b4f46cfcfaa9263ee14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import six | |
class FooError(Exception): | |
message = _(u'An unknown exception occurred.') | |
def __str__(self): | |
if six.PY3: | |
return self.message | |
return self.message.encode('utf-8') | |
def __unicode__(self): | |
return self.message | |
# elsewhere... | |
def do_something(): | |
raise FooError() | |
try: | |
do_something() | |
except FooError as ex: | |
# Returns a UTF-8 string in py2, and a wide string in py3, | |
# both of type six.text_type, with no coercion. | |
msg = str(ex) | |
# Returns unicode type in py2 and str in py3. | |
msg = six.text_type(ex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment