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

Do not use percent format in strings #8045

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Tests/test_file_eps.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,7 @@ def test_readline_psfile(tmp_path: Path) -> None:
strings = ["something", "else", "baz", "bif"]

def _test_readline(t: EpsImagePlugin.PSFile, ending: str) -> None:
ending = "Failure with line ending: %s" % (
"".join("%s" % ord(s) for s in ending)
)
ending = f"Failure with line ending: {''.join(str(ord(s)) for s in ending)}"
assert t.readline().strip("\r\n") == "something", ending
assert t.readline().strip("\r\n") == "else", ending
assert t.readline().strip("\r\n") == "baz", ending
Expand Down
5 changes: 3 additions & 2 deletions Tests/test_image_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

+ """\";
wchar_t *whome = Py_DecodeLocale(home, NULL);
Py_SetPythonHome(whome);

Expand All @@ -432,7 +434,6 @@ def test_embeddable(self) -> None:
return 0;
}
"""
% sys.prefix.replace("\\", "\\\\")
)

compiler = getattr(build_ext, "new_compiler")()
Expand Down
4 changes: 2 additions & 2 deletions selftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ def testimage() -> None:
print("Running selftest:")
status = doctest.testmod(sys.modules[__name__])
if status[0]:
print("*** %s tests of %d failed." % status)
print(f"*** {status[0]} tests of {status[1]} failed.")
exit_status = 1
else:
print("--- %s tests passed." % status[1])
print(f"--- {status[1]} tests passed.")

sys.exit(exit_status)
2 changes: 1 addition & 1 deletion src/PIL/IptcImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
""".. deprecated:: 10.2.0"""
deprecate("IptcImagePlugin.dump", 12)
for i in c:
print("%02x" % _i8(i), end=" ")
print(f"{_i8(i):02x}", end=" ")

Check warning on line 60 in src/PIL/IptcImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/IptcImagePlugin.py#L60

Added line #L60 was not covered by tests
print()


Expand Down
5 changes: 3 additions & 2 deletions src/PIL/PdfParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,9 @@
try:
stream_len = int(result[b"Length"])
except (TypeError, KeyError, ValueError) as e:
msg = "bad or missing Length in stream dict (%r)" % result.get(
b"Length", None
msg = (

Check warning on line 828 in src/PIL/PdfParser.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PdfParser.py#L828

Added line #L828 was not covered by tests
"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.

raise PdfFormatError(msg) from e
stream_data = data[m.end() : m.end() + stream_len]
Expand Down
Loading