Skip to content

Add function to get Exception.message#177

Closed
dgingrich wants to merge 1 commit intobenjaminp:masterfrom
dgingrich:topic-add-exception_message
Closed

Add function to get Exception.message#177
dgingrich wants to merge 1 commit intobenjaminp:masterfrom
dgingrich:topic-add-exception_message

Conversation

@dgingrich
Copy link
Copy Markdown

@dgingrich dgingrich commented Feb 18, 2017

  • Add exception_message
  • Move reraise _add_doc to right after definition

Manual py.test runs with Python2.6 and 3.5 passed, but tox failed with AttributeError: 'module' object has no attribute 'exception_message'.

@dgingrich dgingrich force-pushed the topic-add-exception_message branch 2 times, most recently from 8b7fd06 to 91cec05 Compare February 18, 2017 18:47
* Add exception_message
* Move reraise _add_doc to right after definition
@benjaminp
Copy link
Copy Markdown
Owner

Thanks for the PR. What prevents you from using str(some_exception) in both Python 2 and 3?

@dgingrich
Copy link
Copy Markdown
Author

Unicode strings get decode errors in Python 2:

>>> e = Exception(u'unicöde')
>>> e.message
u'unic\xf6de'
>>> str(e)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 4: ordinal not in range(128)

There is a workaround using six.text_type:

# Python2.6
>>> import six
>>> six.text_type(Exception(u"unicöde"))
u'unic\xf6de'

In addition this shouldn't be much of a problem since Exception.message has been deprecated since 2.6, so I understand if you the PR is rejected. The missing .message attribute does come up in the real world though, this was prompted by another PR for Spark: apache/spark#16845

@benjaminp
Copy link
Copy Markdown
Owner

Yeah, I was going to suggest your six.text_type approach or e.args[0] to deal with unicode.

You're right that I don't want to support deprecated features if possible. Thanks for the suggestion, though.

@benjaminp benjaminp closed this Mar 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants