Skip to content

Call .decode() only for binary data#1060

Merged
mariocj89 merged 1 commit into
dateutil:masterfrom
frenzymadness:decode
May 21, 2021
Merged

Call .decode() only for binary data#1060
mariocj89 merged 1 commit into
dateutil:masterfrom
frenzymadness:decode

Conversation

@frenzymadness

Copy link
Copy Markdown
Contributor

Summary of changes

I believe that the __init__ method might be simplified in this way. In my opinion, it makes sense to call decode() method only on bytes/bytearray objects in both major Pythons.

In Python 2, bytes is builtin alias for str so decode() call does not happen for unicode.

Do you know about any corner case this simplified code is not aware of?

Pull Request Checklist

  • Changes have tests — this class is hopefully tested well enough
  • Authors have been added to AUTHORS.md — not necessary, nothing really important contributed
  • News fragment added in changelog.d. See CONTRIBUTING.md for details — not important user-facing change

Comment thread dateutil/parser/_parser.py
Comment thread dateutil/parser/_parser.py Outdated
else:
if getattr(instream, 'decode', None) is not None:
instream = instream.decode()
if isinstance(instream, (bytes, bytearray)) or six.PY3 and isinstance(instream, memoryview):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be better to do duck-typing and test for the presence of the decode() method?

if hasattr(instream, "decode"):
    instream = instream.decode()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you pass in Python 2 a unicode string it will still attempt to decode it by encoding it first. And if that string has a non-ascii char, it'd fail to encode.

Comment thread dateutil/parser/_parser.py Outdated
else:
if getattr(instream, 'decode', None) is not None:
instream = instream.decode()
if isinstance(instream, (bytes, bytearray)) or six.PY3 and isinstance(instream, memoryview):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that what @vstinner meant with supporting memoryview was not this. This will fail when a memoryview is passed, as it does not have a decode method AFAIK.

If you want to add support for memoryview, please add tests for it. Alternatively, feel free to just remove or six.PY3 and isinstance(instream, memoryview) from this PR.

@frenzymadness

Copy link
Copy Markdown
Contributor Author

I must admit that I kinda lost the context of this change after the eleven months. Give me a while or, if you know the solution, feel free to open a new PR based on this one and finish the fix.

@mariocj89

Copy link
Copy Markdown
Member

Give me a while or, if you know the solution, feel free to open a new PR based on this one and finish the fix.

Sure, I'll push a commit with the additions.

Comment thread dateutil/parser/_parser.py Outdated
else:
if getattr(instream, 'decode', None) is not None:
instream = instream.decode()
if isinstance(instream, (bytes, bytearray)) or six.PY3 and isinstance(instream, memoryview):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the same time, I see that commit 8e78646 says "Add support for bytesarray and other bytes-like", not sure if there is any other case we are missing here than bytes, bytearray and unicode.

Let's see if the test suite catches anything.

We can handle both bytes and bytearray in the same way in Python 2 and
Python 3.

Co-authored-by: Mario Corchero <[email protected]>

@mariocj89 mariocj89 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but would be good to get another review as I rewrote your commit.

@mariocj89
mariocj89 requested a review from ffe4 May 20, 2021 13:57
@frenzymadness

Copy link
Copy Markdown
Contributor Author

@vstinner could you please take a look?

@mariocj89
mariocj89 requested a review from pganssle May 21, 2021 06:24

@vstinner vstinner left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Ignore my remark about memoryview, memoryview objects have no decode() method.

@mariocj89
mariocj89 merged commit 7ca3eb2 into dateutil:master May 21, 2021
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.

4 participants