diff --git a/AUTHORS b/AUTHORS index d0b4a5cee..6218abb01 100644 --- a/AUTHORS +++ b/AUTHORS @@ -161,6 +161,7 @@ Contributors: * Wei-Lun Li (b84330808) * jackwalkerlabs (Codex-assisted) + * Alex Castillo González (pcbeingused333) Creator: -------- diff --git a/changelog.rst b/changelog.rst index fc362092b..0848adc99 100644 --- a/changelog.rst +++ b/changelog.rst @@ -16,6 +16,8 @@ Bug fixes: created by ``uv venv``. * Read ``alias_map_file`` as UTF-8 (with or without a BOM) instead of the locale encoding, which made non-ASCII aliases fail on Windows. +* Read and write the casing file and the log file as UTF-8 instead of the + locale encoding, which broke non-ASCII identifiers and queries on Windows. Internal: --------- diff --git a/pgcli/completion_refresher.py b/pgcli/completion_refresher.py index 34771b8a2..8c2ae61ce 100644 --- a/pgcli/completion_refresher.py +++ b/pgcli/completion_refresher.py @@ -136,10 +136,10 @@ def refresh_casing(completer, executor): generate_casing_file = completer.generate_casing_file if generate_casing_file and not os.path.isfile(casing_file): casing_prefs = "\n".join(executor.casing()) - with open(casing_file, "w") as f: + with open(casing_file, "w", encoding="utf-8") as f: f.write(casing_prefs) if os.path.isfile(casing_file): - with open(casing_file) as f: + with open(casing_file, encoding="utf-8") as f: completer.extend_casing([line.strip() for line in f]) diff --git a/pgcli/main.py b/pgcli/main.py index 972b3c670..bea5162f8 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -695,7 +695,7 @@ def initialize_logging(self): if log_level.upper() == "NONE": handler = logging.NullHandler() else: - handler = logging.FileHandler(os.path.expanduser(log_file)) + handler = logging.FileHandler(os.path.expanduser(log_file), encoding="utf-8") level_map = { "CRITICAL": logging.CRITICAL, diff --git a/tests/test_completion_refresher.py b/tests/test_completion_refresher.py index a5529d6b3..530dacdb7 100644 --- a/tests/test_completion_refresher.py +++ b/tests/test_completion_refresher.py @@ -1,3 +1,4 @@ +import builtins import time import pytest from unittest.mock import Mock, patch @@ -93,3 +94,37 @@ def test_refresh_with_callbacks(refresher): refresher.refresh(pgexecute, special, callbacks) time.sleep(1) # Wait for the thread to work. assert callbacks[0].call_count == 1 + + +@pytest.fixture +def cp1252_locale(monkeypatch): + """Make open() default to cp1252, as it does on Western European Windows.""" + from pgcli import completion_refresher + + def locale_open(file, mode="r", *args, **kwargs): + if "b" not in mode and len(args) < 2: + kwargs.setdefault("encoding", "cp1252") + return builtins.open(file, mode, *args, **kwargs) + + monkeypatch.setattr(completion_refresher, "open", locale_open, raising=False) + + +def test_refresh_casing_writes_casing_file_as_utf8(refresher, tmp_path, cp1252_locale): + casing_file = tmp_path / "casing" + completer = Mock(casing_file=str(casing_file), generate_casing_file=True) + executor = Mock(**{"casing.return_value": ["Café", "注文明細"]}) + + refresher.refreshers["casing"](completer, executor) + + assert casing_file.read_text(encoding="utf-8") == "Café\n注文明細" + completer.extend_casing.assert_called_once_with(["Café", "注文明細"]) + + +def test_refresh_casing_reads_casing_file_as_utf8(refresher, tmp_path, cp1252_locale): + casing_file = tmp_path / "casing" + casing_file.write_text("Café\n注文明細\n", encoding="utf-8") + completer = Mock(casing_file=str(casing_file), generate_casing_file=False) + + refresher.refreshers["casing"](completer, Mock()) + + completer.extend_casing.assert_called_once_with(["Café", "注文明細"]) diff --git a/tests/test_main.py b/tests/test_main.py index 57ba8afe8..78792c5c8 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -4,6 +4,7 @@ import re import tempfile import datetime +import logging from unittest import mock import pytest @@ -443,6 +444,28 @@ def test_logfile_works(executor): assert log_contents[2].strip() == "hello!" +def test_log_file_is_written_as_utf8(tmp_path): + # The log records queries and errors, which can hold any character; the + # locale encoding (e.g. cp1252 on Windows) cannot encode all of them. + cli = PGCli() + log_file = tmp_path / "pgcli.log" + cli.config["main"]["log_file"] = str(log_file) + cli.config["main"]["log_level"] = "DEBUG" + root_logger = logging.getLogger("pgcli") + handlers_before = list(root_logger.handlers) + cli.initialize_logging() + (handler,) = [h for h in root_logger.handlers if h not in handlers_before] + try: + assert handler.encoding == "utf-8" + root_logger.debug("SELECT * FROM 注文明細") + handler.flush() + assert "SELECT * FROM 注文明細" in log_file.read_text(encoding="utf-8") + finally: + root_logger.removeHandler(handler) + logging.getLogger("pgspecial").removeHandler(handler) + handler.close() + + @dbtest def test_logfile_unwriteable_file(executor): cli = PGCli(pgexecute=executor)