Skip to content
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Contributors:
* Wei-Lun Li (b84330808)

* jackwalkerlabs (Codex-assisted)
* Alex Castillo González (pcbeingused333)

Creator:
--------
Expand Down
2 changes: 2 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
---------
Expand Down
4 changes: 2 additions & 2 deletions pgcli/completion_refresher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])


Expand Down
2 changes: 1 addition & 1 deletion pgcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions tests/test_completion_refresher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import builtins
import time
import pytest
from unittest.mock import Mock, patch
Expand Down Expand Up @@ -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é", "注文明細"])
23 changes: 23 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import tempfile
import datetime
import logging
from unittest import mock

import pytest
Expand Down Expand Up @@ -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)
Expand Down
Loading