Skip to content

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.

{
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
@radarhere radarhere deleted the strings branch May 10, 2024 23:15
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