Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call .decode() only for binary data #1060

Merged
merged 1 commit into from May 21, 2021
Merged

Conversation

frenzymadness
Copy link
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

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

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
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.

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
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
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
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.

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
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 <mariocj89@gmail.com>
Copy link
Member

@mariocj89 mariocj89 left a comment

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
Contributor Author

@vstinner could you please take a look?

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

@vstinner vstinner left a comment

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.

None yet

4 participants