|
11 | 11 | from collections import Counter |
12 | 12 | from io import StringIO |
13 | 13 | from pathlib import Path |
14 | | -from typing import TextIO |
| 14 | +from typing import TYPE_CHECKING, TextIO |
15 | 15 |
|
16 | 16 | import pytest |
17 | 17 | from _pytest.config import Config |
|
20 | 20 | from pylint.config.config_initialization import _config_initialization |
21 | 21 | from pylint.lint import PyLinter |
22 | 22 | from pylint.message.message import Message |
| 23 | +from pylint.reporters import BaseReporter |
23 | 24 | from pylint.testutils.constants import _EXPECTED_RE, _OPERATORS, UPDATE_OPTION |
24 | 25 |
|
25 | 26 | # need to import from functional.test_file to avoid cyclic import |
|
31 | 32 | from pylint.testutils.output_line import OutputLine |
32 | 33 | from pylint.testutils.reporter_for_tests import FunctionalTestReporter |
33 | 34 |
|
| 35 | +if TYPE_CHECKING: |
| 36 | + import _csv |
34 | 37 | MessageCounter = Counter[tuple[int, str]] |
35 | 38 |
|
36 | 39 | PYLINTRC = Path(__file__).parent / "testing_pylintrc" |
@@ -309,10 +312,22 @@ def error_msg_for_unequal_output( |
309 | 312 | expected_csv = StringIO() |
310 | 313 | writer = csv.writer(expected_csv, dialect="test") |
311 | 314 | for line in sorted(received_lines, key=sort_by_line_number): |
312 | | - writer.writerow(line.to_csv()) |
| 315 | + self.safe_write_output_line(writer, line) |
313 | 316 | error_msg += expected_csv.getvalue() |
314 | 317 | return error_msg |
315 | 318 |
|
| 319 | + def safe_write_output_line(self, writer: _csv._writer, line: OutputLine) -> None: |
| 320 | + """Write an OutputLine to the CSV writer, handling UnicodeEncodeError.""" |
| 321 | + try: |
| 322 | + writer.writerow(line.to_csv()) |
| 323 | + except UnicodeEncodeError: |
| 324 | + writer.writerow( |
| 325 | + [ |
| 326 | + BaseReporter.reencode_output_after_unicode_error(s) |
| 327 | + for s in line.to_csv() |
| 328 | + ] |
| 329 | + ) |
| 330 | + |
316 | 331 | def _check_output_text( |
317 | 332 | self, |
318 | 333 | _: MessageCounter, |
|
0 commit comments