Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2013 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5b4f46cfcfaa9263ee14 to your computer and use it in GitHub Desktop.
Save anonymous/5b4f46cfcfaa9263ee14 to your computer and use it in GitHub Desktop.
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