Skip to content

Commit

Permalink
Configure UTF-8 output encoding globally
Browse files Browse the repository at this point in the history
Force UTF-8 encoding for stdout/stderr streams to handle locale-related
issues across platforms. This replaces the manual encoding workaround and
ensures consistent behavior regardless of the system's locale settings.

- Fixes crashes when printing Unicode characters on systems with non-UTF-8
  locales (e.g. Chinese GB18030 or Windows CP1252)
- Centralizes encoding configuration in init.py
- Removes redundant per-file encoding logic

This approach works cross-platform by directly wrapping the standard
streams rather than relying on environment variables, which can be
unreliable on Windows.
  • Loading branch information
gamesh411 committed Feb 19, 2025
1 parent d0c86b9 commit 3f60da0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
18 changes: 18 additions & 0 deletions codechecker_common/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,27 @@

import argparse
from importlib import machinery
import io
import json
import os
import signal
import sys

def configure_utf8_output():
"""Force UTF-8 encoding for stdout/stderr across all platforms."""
# Reconfigure standard streams
sys.stdout = io.TextIOWrapper(
sys.stdout.buffer,
encoding='utf-8',
errors='backslashreplace',
line_buffering=sys.stdout.line_buffering
)
sys.stderr = io.TextIOWrapper(
sys.stderr.buffer,
encoding='utf-8',
errors='backslashreplace',
line_buffering=sys.stderr.line_buffering
)

class ArgumentParser(argparse.ArgumentParser):
def error(self, message):
Expand Down Expand Up @@ -93,6 +109,8 @@ def main():
"""
CodeChecker main command line.
"""
configure_utf8_output()

if not os.environ.get('CC_LIB_DIR'):
os.environ['CC_LIB_DIR'] = \
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# -------------------------------------------------------------------------
""" Helper and converter functions for plain text format. """

import locale
import logging
import math
import os
Expand Down Expand Up @@ -177,9 +176,8 @@ def convert(
if source_comment and source_comment.line:
output.write(f"{source_comment.line.rstrip()}\n")

output.write(
f"{format_main_report(report)}"
.encode('utf-8').decode(locale.getlocale()[1] or "utf-8"))
output.write(format_main_report(report))

else:
output.write(InvalidFileContentMsg)

Expand Down

0 comments on commit 3f60da0

Please sign in to comment.