Skip to content

Commit

Permalink
Decode input str when reporting an error in parse_isodate
Browse files Browse the repository at this point in the history
As the function is wrapped around a decorator that converts the input to
bytes, when an exception is raised that includes the value, it contains
the value as bytes (b"<val>"). To provide the expected str in Python 3,
decode that to unicode.

This will result in `u""` for Python 2 callers, but given that we plan
to drop support for Python 2 in the short term, it feels acceptable.
  • Loading branch information
pawl authored and mariocj89 committed May 20, 2021
1 parent 66db2fa commit 5cf3e3c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ switch, and thus all their contributions are dual-licensed.
- Nicolas Évrard (gh: @nicoe) **D**
- Nick Smith <nick.smith@MASKED>
- Orson Adams <orson.network@MASKED> (gh: @parsethis) **D**
- Paul Brown (gh: @pawl) **D**
- Paul Dickson (gh @prdickson) **D**
- Paul Ganssle <paul@ganssle.io> (gh: @pganssle) **R**
- Pascal van Kooten <kootenpv@MASKED> (gh: @kootenpv) **R**
Expand Down
1 change: 1 addition & 0 deletions changelog.d/1122.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug that caused b' prefixes to appear in parse_isodate exception messages.
2 changes: 1 addition & 1 deletion dateutil/parser/isoparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def parse_isodate(self, datestr):
components, pos = self._parse_isodate(datestr)
if pos < len(datestr):
raise ValueError('String contains unknown ISO ' +
'components: {}'.format(datestr))
'components: {!r}'.format(datestr.decode('ascii')))
return date(*components)

@_takes_ascii
Expand Down
12 changes: 12 additions & 0 deletions dateutil/test/test_isoparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ def test_isodate_raises(isostr, exception):
isoparser().parse_isodate(isostr)


def test_parse_isodate_error_text():
with pytest.raises(ValueError) as excinfo:
isoparser().parse_isodate('2014-0423')

# ensure the error message does not contain b' prefixes
if six.PY2:
expected_error = "String contains unknown ISO components: u'2014-0423'"
else:
expected_error = "String contains unknown ISO components: '2014-0423'"
assert expected_error == str(excinfo.value)


###
# Test parse_isotime
def __make_time_examples():
Expand Down

0 comments on commit 5cf3e3c

Please sign in to comment.