From 5e87dd340ef6e6a513c104fed4dbed64dac66c48 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Fri, 14 Jun 2024 10:10:13 +0200 Subject: [PATCH 1/3] Work in progress #8736 --- pylint/reporters/base_reporter.py | 6 +++++- tests/functional/r/regression_02/regression_8736.py | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 tests/functional/r/regression_02/regression_8736.py diff --git a/pylint/reporters/base_reporter.py b/pylint/reporters/base_reporter.py index d370b1910e..415b7aeebb 100644 --- a/pylint/reporters/base_reporter.py +++ b/pylint/reporters/base_reporter.py @@ -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.""" diff --git a/tests/functional/r/regression_02/regression_8736.py b/tests/functional/r/regression_02/regression_8736.py new file mode 100644 index 0000000000..9189c593a1 --- /dev/null +++ b/tests/functional/r/regression_02/regression_8736.py @@ -0,0 +1,2 @@ +"""This does not crash in the functional tests, but it does when called directly""" +assert "\U00010000" == "\ud800\udc00" From fd2883e53c527776a03be4eae7666da39583dfd6 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Fri, 14 Jun 2024 10:15:06 +0200 Subject: [PATCH 2/3] add temp fragment --- doc/whatsnew/fragments/8736.bugfix | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 doc/whatsnew/fragments/8736.bugfix diff --git a/doc/whatsnew/fragments/8736.bugfix b/doc/whatsnew/fragments/8736.bugfix new file mode 100644 index 0000000000..e10c8fe568 --- /dev/null +++ b/doc/whatsnew/fragments/8736.bugfix @@ -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. From 96592c916290f26011da98b65b7f26016b625ccb Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Fri, 14 Jun 2024 10:21:54 +0200 Subject: [PATCH 3/3] same treatment for functional test update --- .../testutils/functional/lint_module_output_update.py | 10 +++++++++- pylint/testutils/lint_module_test.py | 10 +++++++++- tests/functional/r/regression_02/regression_8736.py | 5 +++-- tests/functional/r/regression_02/regression_8736.txt | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 tests/functional/r/regression_02/regression_8736.txt diff --git a/pylint/testutils/functional/lint_module_output_update.py b/pylint/testutils/functional/lint_module_output_update.py index 38ed465aad..3053924121 100644 --- a/pylint/testutils/functional/lint_module_output_update.py +++ b/pylint/testutils/functional/lint_module_output_update.py @@ -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() + ] + ) diff --git a/pylint/testutils/lint_module_test.py b/pylint/testutils/lint_module_test.py index 37839c8908..3f82a7073f 100644 --- a/pylint/testutils/lint_module_test.py +++ b/pylint/testutils/lint_module_test.py @@ -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 diff --git a/tests/functional/r/regression_02/regression_8736.py b/tests/functional/r/regression_02/regression_8736.py index 9189c593a1..097d17c679 100644 --- a/tests/functional/r/regression_02/regression_8736.py +++ b/tests/functional/r/regression_02/regression_8736.py @@ -1,2 +1,3 @@ -"""This does not crash in the functional tests, but it does when called directly""" -assert "\U00010000" == "\ud800\udc00" +"""This does not crash in the functional tests, but it did when called directly.""" + +assert "\U00010000" == "\ud800\udc00" # [comparison-of-constants] diff --git a/tests/functional/r/regression_02/regression_8736.txt b/tests/functional/r/regression_02/regression_8736.txt new file mode 100644 index 0000000000..bf2446f5fd --- /dev/null +++ b/tests/functional/r/regression_02/regression_8736.txt @@ -0,0 +1 @@ +comparison-of-constants:3:7:3:37::"Comparison between constants: '𐀀 == ' has a constant value":HIGH