Do not use percent format in strings#8045
Conversation
|
created radarhere#25 for the |
src/PIL/PdfParser.py
Outdated
| msg = ( | ||
| "bad or missing Length in stream dict " | ||
| f"({result.get(b'Length')})" | ||
| ) |
There was a problem hiding this comment.
| 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 eI think that makes the code harder to read, because, if you ignore the exception, there's more to understand.
There was a problem hiding this comment.
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 eThere was a problem hiding this comment.
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.
Tests/test_image_access.py
Outdated
| { | ||
| char *home = "%s"; | ||
| char *home = \"""" | ||
| + sys.prefix.replace("\\", "\\\\") |
There was a problem hiding this comment.
Can we put this in a variable outside the string, then make this an f-string?
There was a problem hiding this comment.
Ok, I've pushed a commit. It did require escaping brackets though.
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.