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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-113537: support loads str in plistlib.loads #113582

Merged
merged 10 commits into from Jan 6, 2024

Conversation

aisk
Copy link
Contributor

@aisk aisk commented Dec 30, 2023

Copy link
Contributor

@ronaldoussoren ronaldoussoren left a comment

Choose a reason for hiding this comment

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

plistlib supports two formats for the plist file: XML and Binary. Accepting strings for input is only useful for the XML format.

The loads implementation should assert that fmt == FMT_XML when the value is a string and raise ValueError when it is not. This also needs to be tested.

Lib/plistlib.py Outdated Show resolved Hide resolved
Lib/test/test_plistlib.py Outdated Show resolved Hide resolved
@bedevere-app
Copy link

bedevere-app bot commented Dec 30, 2023

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

aisk and others added 5 commits December 31, 2023 20:54
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
@aisk
Copy link
Contributor Author

aisk commented Dec 31, 2023

The loads implementation should assert that fmt == FMT_XML when the value is a string and raise ValueError when it is not.

Updated, but I think raising a TypeError is more suitable here. And it has more compatibility; the old version will raise a TypeError: a bytes-like object is required, not 'str' if a string argument is passed to loads, so it's better to raise the same error type.

@ronaldoussoren
Copy link
Contributor

The loads implementation should assert that fmt == FMT_XML when the value is a string and raise ValueError when it is not.

Updated, but I think raising a TypeError is more suitable here. And it has more compatibility; the old version will raise a TypeError: a bytes-like object is required, not 'str' if a string argument is passed to loads, so it's better to raise the same error type.

I agree.

Copy link
Contributor

@ronaldoussoren ronaldoussoren left a comment

Choose a reason for hiding this comment

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

Getting there!

The PR looks good at this point, just some minor changes.

Lib/plistlib.py Outdated
Comment on lines 893 to 894
msg = "value must be bytes-like oebject when fmt is FMT_BINARY"
raise TypeError(msg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
msg = "value must be bytes-like oebject when fmt is FMT_BINARY"
raise TypeError(msg)
raise TypeError("value must be bytes-like object when fmt is FMT_BINARY")

The msg variable is not necessary (also fixes a typo in the message itself)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review, I fixed the typo, but kept the msg variable. It's here to make the line length shorter than 80.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't like introducing a variable for the message here. You could do this instead:

            raise TypeError(
                        "value must be bytes-like object when fmt is FMT_BINARY"
             )

If I counted correctly this just about fits into 80 characters. Alternatively split the string:

            raise TypeError(
                        "value must be bytes-like object "
                         "when fmt is FMT_BINARY"
             )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, updated.

Lib/test/test_plistlib.py Outdated Show resolved Hide resolved
aisk and others added 3 commits January 2, 2024 22:37
Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
Copy link
Contributor

@ronaldoussoren ronaldoussoren left a comment

Choose a reason for hiding this comment

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

LGTM.

Thanks for the PR!

@ronaldoussoren ronaldoussoren merged commit bbf214d into python:main Jan 6, 2024
30 of 31 checks passed
@aisk aisk deleted the plists-loads-str branch January 6, 2024 09:42
@aisk
Copy link
Contributor Author

aisk commented Jan 6, 2024

Thanks for the view!

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

2 participants