Skip to content
New issue

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

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

Already on GitHub? # to your account

Do not use percent format in strings #8045

Merged
merged 4 commits into from
May 10, 2024
Merged

Conversation

radarhere
Copy link
Member

Fixes the failures in #8044

Converts several percent format strings to f-strings.

There is also a percent format inside a multiline string - I think the neatest solution is just to break the multiline string.

@Yay295
Copy link
Contributor

Yay295 commented May 7, 2024

created radarhere#25 for the PdfParser change

Comment on lines 828 to 831
msg = (
"bad or missing Length in stream dict "
f"({result.get(b'Length')})"
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
msg = (
"bad or missing Length in stream dict "
f"({result.get(b'Length')})"
)
stream_len = result.get(b"Length")
msg = f"bad or missing Length in stream dict ({stream_len})"

radarhere#25 feels that error message should fit onto one line. The change above this would be my suggestion for that goal.

The suggestion from the PR is

stream_len_str = result.get(b"Length")
try:
	stream_len = int(stream_len_str)
except (TypeError, KeyError, ValueError) as e:
	msg = f"bad or missing Length in stream dict ({stream_len_str})"
	raise PdfFormatError(msg) from e

I think that makes the code harder to read, because, if you ignore the exception, there's more to understand.

Copy link
Contributor

@nulano nulano May 7, 2024

Choose a reason for hiding this comment

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

My suggestion would be radarhere#25 (comment):

try:
	stream_len_str = result.get(b"Length")
	stream_len = int(stream_len_str)
except (TypeError, KeyError, ValueError) as e:
	msg = f"bad or missing Length in stream dict ({stream_len_str})"
	raise PdfFormatError(msg) from e

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I've pushed that - minus the KeyError, because now that result[b"Length"] is gone, it will no longer happen. int(None) will instead raise a TypeError.

@@ -415,7 +415,9 @@ def test_embeddable(self) -> None:

int main(int argc, char* argv[])
{
char *home = "%s";
char *home = \""""
+ sys.prefix.replace("\\", "\\\\")
Copy link
Member

Choose a reason for hiding this comment

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

Can we put this in a variable outside the string, then make this an f-string?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I've pushed a commit. It did require escaping brackets though.

@hugovk hugovk merged commit bfbe339 into python-pillow:main May 10, 2024
56 checks passed
@radarhere radarhere deleted the strings branch May 10, 2024 23:15
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants