diff --git a/changelog.md b/changelog.md index 1fb9eb5f..e63f7734 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,7 @@ Bug Fixes * Refresh completions after `CREATE`, `ALTER`, `DROP` or `RENAME` statements that start with a SQL comment. * Reset completions after `USE`, `\u`, `\r` or `connect` statements that start with a SQL comment. * Don't flag a single-table `UPDATE` with a `WHERE` clause as destructive when it starts with a `/* ... */` comment. +* Read the password file and the contributors, sponsors and tips files as UTF-8 instead of the locale encoding. Internal diff --git a/mycli/main.py b/mycli/main.py index d7616416..b6991b01 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -397,7 +397,7 @@ def get_password_from_file(password_file: str | None) -> str | None: if not password_file: return None try: - with open(password_file) as fp: + with open(password_file, encoding='utf-8') as fp: return fp.readline().removesuffix('\n') except FileNotFoundError: click.secho(f"Password file '{password_file}' not found", err=True, fg='red') diff --git a/mycli/main_modes/repl.py b/mycli/main_modes/repl.py index 7d666a31..e2c35b3d 100644 --- a/mycli/main_modes/repl.py +++ b/mycli/main_modes/repl.py @@ -1170,7 +1170,7 @@ def _contributors_picker() -> str: lines: str = "" try: - with resources.files(mycli_package).joinpath("AUTHORS.txt").open('r') as f: + with resources.files(mycli_package).joinpath("AUTHORS.txt").open('r', encoding='utf-8') as f: lines += f.read() except FileNotFoundError: pass @@ -1186,7 +1186,7 @@ def _sponsors_picker() -> str: lines: str = "" try: - with resources.files(mycli_package).joinpath("SPONSORS.txt").open('r') as f: + with resources.files(mycli_package).joinpath("SPONSORS.txt").open('r', encoding='utf-8') as f: lines += f.read() except FileNotFoundError: pass @@ -1202,7 +1202,7 @@ def _tips_picker() -> str: tips = [] try: - with resources.files(mycli_package).joinpath('TIPS.txt').open('r') as f: + with resources.files(mycli_package).joinpath('TIPS.txt').open('r', encoding='utf-8') as f: for line in f: if line.startswith("#"): continue diff --git a/mycli_test/pytests/test_main.py b/mycli_test/pytests/test_main.py index 1afcb78d..4be37a35 100644 --- a/mycli_test/pytests/test_main.py +++ b/mycli_test/pytests/test_main.py @@ -10,6 +10,7 @@ from pathlib import Path import runpy import shutil +import subprocess import sys from tempfile import NamedTemporaryFile from textwrap import dedent @@ -1405,6 +1406,24 @@ def close(self, **args): assert resolve_connect_password(MockMyCli.connect_args) == ('literal', 'cleartext_password') +def test_get_password_from_file_does_not_use_locale_encoding(tmp_path: Path) -> None: + # EncodingWarning is raised for any open() that falls back to the locale encoding. + password_file = tmp_path / 'password.txt' + password_file.write_text('p\u00e4ssw\u00f6rd\n', encoding='utf-8') + script = '\n'.join(( + 'import sys', + 'from mycli.main import get_password_from_file', + 'assert get_password_from_file(sys.argv[1]) == "p\u00e4ssw\u00f6rd"', + )) + result = subprocess.run( + [sys.executable, '-X', 'warn_default_encoding', '-W', 'error::EncodingWarning', '-c', script, str(password_file)], + capture_output=True, + encoding='utf8', + errors='replace', + ) + assert result.returncode == 0, result.stderr + + def test_password_option_overrides_password_file_and_mysql_pwd(monkeypatch): class Formatter: format_name = None diff --git a/mycli_test/pytests/test_main_modes_repl.py b/mycli_test/pytests/test_main_modes_repl.py index 3884fb1c..08327d48 100644 --- a/mycli_test/pytests/test_main_modes_repl.py +++ b/mycli_test/pytests/test_main_modes_repl.py @@ -6,6 +6,8 @@ from io import StringIO import os from pathlib import Path +import subprocess +import sys from threading import Event from types import SimpleNamespace from typing import Any, Literal, cast @@ -134,7 +136,7 @@ def __init__(self, files: dict[str, str], path: str | None = None) -> None: def joinpath(self, path: str) -> 'FakeResourceTree': return FakeResourceTree(self.files, path) - def open(self, mode: str) -> StringIO: + def open(self, mode: str, encoding: str | None = None) -> StringIO: assert self.path is not None if self.path not in self.files: raise FileNotFoundError(self.path) @@ -820,6 +822,23 @@ def test_repl_picker_helpers_cover_present_and_missing_resources(monkeypatch: py assert repl_mode._tips_picker() == r'/? or /help for help!' +def test_repl_picker_helpers_do_not_use_locale_encoding() -> None: + # EncodingWarning is raised for any open() that falls back to the locale encoding. + script = '\n'.join(( + 'from mycli.main_modes import repl', + 'repl._contributors_picker()', + 'repl._sponsors_picker()', + 'repl._tips_picker()', + )) + result = subprocess.run( + [sys.executable, '-X', 'warn_default_encoding', '-W', 'error::EncodingWarning', '-c', script], + capture_output=True, + encoding='utf8', + errors='replace', + ) + assert result.returncode == 0, result.stderr + + def test_configure_editor_uses_configured_editor(monkeypatch: pytest.MonkeyPatch) -> None: cli = make_repl_cli() cli.config['editor']['editor_command'] = 'nano'