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

Fix a crash when a pylint must display unicode raising a UnicodeEncodeError #9732

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/8736.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
When displaying unicode with surrogates (or other potential ``UnicodeEncodeError``),
pylint will now display the best representation of the string by ignoring unicode
errors instead of crashing.

Closes #8736.
6 changes: 5 additions & 1 deletion pylint/reporters/base_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def handle_message(self, msg: Message) -> None:

def writeln(self, string: str = "") -> None:
"""Write a line in the output buffer."""
print(string, file=self.out)
try:
print(string, file=self.out)
except UnicodeEncodeError:
best_effort_string = string.encode(encoding="utf-8", errors="ignore")
print(best_effort_string.decode("utf8"), file=self.out)

def display_reports(self, layout: Section) -> None:
"""Display results encapsulated in the layout tree."""
Expand Down
10 changes: 9 additions & 1 deletion pylint/testutils/functional/lint_module_output_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ def _check_output_text(
with open(self._test_file.expected_output, "w", encoding="utf-8") as f:
writer = csv.writer(f, dialect="test")
for line in actual_output:
writer.writerow(line.to_csv())
try:
writer.writerow(line.to_csv())
except UnicodeEncodeError:
writer.writerow(
[
s.encode("utf8", "ignore").decode("utf8")
for s in line.to_csv()
]
)
10 changes: 9 additions & 1 deletion pylint/testutils/lint_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,15 @@ def error_msg_for_unequal_output(
expected_csv = StringIO()
writer = csv.writer(expected_csv, dialect="test")
for line in sorted(received_lines, key=sort_by_line_number):
writer.writerow(line.to_csv())
try:
writer.writerow(line.to_csv())
except UnicodeEncodeError:
writer.writerow(
[
s.encode("utf8", "ignore").decode("utf8")
for s in line.to_csv()
]
)
error_msg += expected_csv.getvalue()
return error_msg

Expand Down
3 changes: 3 additions & 0 deletions tests/functional/r/regression_02/regression_8736.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""This does not crash in the functional tests, but it did when called directly."""

assert "\U00010000" == "\ud800\udc00" # [comparison-of-constants]
1 change: 1 addition & 0 deletions tests/functional/r/regression_02/regression_8736.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
comparison-of-constants:3:7:3:37::"Comparison between constants: '𐀀 == ' has a constant value":HIGH
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
comparison-of-constants:3:7:3:37::"Comparison between constants: '𐀀 == ' has a constant value":HIGH
comparison-of-constants:3:7:3:37::"Comparison between constants: '"\U00010000" == "\ud800\udc00"' has a constant value":HIGH

Imo we should aim for this, but I have an intuition that the ast module internal might be involved.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, but I don't think we can. self.out is utf-8 encoded so we always need to encode/escape to utf-8.

Loading