Skip to content

Commit 18f800d

Browse files
authored
Merge pull request #2444 from pcbeingused333/fix/open-encoding-c4232
fix: read password file and contributor/sponsor/tip files as UTF-8
2 parents 3c5ce99 + dc760c8 commit 18f800d

5 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Bug Fixes
66
* Refresh completions after `CREATE`, `ALTER`, `DROP` or `RENAME` statements that start with a SQL comment.
77
* Reset completions after `USE`, `\u`, `\r` or `connect` statements that start with a SQL comment.
88
* Don't flag a single-table `UPDATE` with a `WHERE` clause as destructive when it starts with a `/* ... */` comment.
9+
* Read the password file and the contributors, sponsors and tips files as UTF-8 instead of the locale encoding.
910

1011

1112
Internal

‎mycli/main.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def get_password_from_file(password_file: str | None) -> str | None:
397397
if not password_file:
398398
return None
399399
try:
400-
with open(password_file) as fp:
400+
with open(password_file, encoding='utf-8') as fp:
401401
return fp.readline().removesuffix('\n')
402402
except FileNotFoundError:
403403
click.secho(f"Password file '{password_file}' not found", err=True, fg='red')

‎mycli/main_modes/repl.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ def _contributors_picker() -> str:
11701170
lines: str = ""
11711171

11721172
try:
1173-
with resources.files(mycli_package).joinpath("AUTHORS.txt").open('r') as f:
1173+
with resources.files(mycli_package).joinpath("AUTHORS.txt").open('r', encoding='utf-8') as f:
11741174
lines += f.read()
11751175
except FileNotFoundError:
11761176
pass
@@ -1186,7 +1186,7 @@ def _sponsors_picker() -> str:
11861186
lines: str = ""
11871187

11881188
try:
1189-
with resources.files(mycli_package).joinpath("SPONSORS.txt").open('r') as f:
1189+
with resources.files(mycli_package).joinpath("SPONSORS.txt").open('r', encoding='utf-8') as f:
11901190
lines += f.read()
11911191
except FileNotFoundError:
11921192
pass
@@ -1202,7 +1202,7 @@ def _tips_picker() -> str:
12021202
tips = []
12031203

12041204
try:
1205-
with resources.files(mycli_package).joinpath('TIPS.txt').open('r') as f:
1205+
with resources.files(mycli_package).joinpath('TIPS.txt').open('r', encoding='utf-8') as f:
12061206
for line in f:
12071207
if line.startswith("#"):
12081208
continue

‎mycli_test/pytests/test_main.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pathlib import Path
1111
import runpy
1212
import shutil
13+
import subprocess
1314
import sys
1415
from tempfile import NamedTemporaryFile
1516
from textwrap import dedent
@@ -1405,6 +1406,24 @@ def close(self, **args):
14051406
assert resolve_connect_password(MockMyCli.connect_args) == ('literal', 'cleartext_password')
14061407

14071408

1409+
def test_get_password_from_file_does_not_use_locale_encoding(tmp_path: Path) -> None:
1410+
# EncodingWarning is raised for any open() that falls back to the locale encoding.
1411+
password_file = tmp_path / 'password.txt'
1412+
password_file.write_text('p\u00e4ssw\u00f6rd\n', encoding='utf-8')
1413+
script = '\n'.join((
1414+
'import sys',
1415+
'from mycli.main import get_password_from_file',
1416+
'assert get_password_from_file(sys.argv[1]) == "p\u00e4ssw\u00f6rd"',
1417+
))
1418+
result = subprocess.run(
1419+
[sys.executable, '-X', 'warn_default_encoding', '-W', 'error::EncodingWarning', '-c', script, str(password_file)],
1420+
capture_output=True,
1421+
encoding='utf8',
1422+
errors='replace',
1423+
)
1424+
assert result.returncode == 0, result.stderr
1425+
1426+
14081427
def test_password_option_overrides_password_file_and_mysql_pwd(monkeypatch):
14091428
class Formatter:
14101429
format_name = None

‎mycli_test/pytests/test_main_modes_repl.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from io import StringIO
77
import os
88
from pathlib import Path
9+
import subprocess
10+
import sys
911
from threading import Event
1012
from types import SimpleNamespace
1113
from typing import Any, Literal, cast
@@ -134,7 +136,7 @@ def __init__(self, files: dict[str, str], path: str | None = None) -> None:
134136
def joinpath(self, path: str) -> 'FakeResourceTree':
135137
return FakeResourceTree(self.files, path)
136138

137-
def open(self, mode: str) -> StringIO:
139+
def open(self, mode: str, encoding: str | None = None) -> StringIO:
138140
assert self.path is not None
139141
if self.path not in self.files:
140142
raise FileNotFoundError(self.path)
@@ -820,6 +822,23 @@ def test_repl_picker_helpers_cover_present_and_missing_resources(monkeypatch: py
820822
assert repl_mode._tips_picker() == r'/? or /help for help!'
821823

822824

825+
def test_repl_picker_helpers_do_not_use_locale_encoding() -> None:
826+
# EncodingWarning is raised for any open() that falls back to the locale encoding.
827+
script = '\n'.join((
828+
'from mycli.main_modes import repl',
829+
'repl._contributors_picker()',
830+
'repl._sponsors_picker()',
831+
'repl._tips_picker()',
832+
))
833+
result = subprocess.run(
834+
[sys.executable, '-X', 'warn_default_encoding', '-W', 'error::EncodingWarning', '-c', script],
835+
capture_output=True,
836+
encoding='utf8',
837+
errors='replace',
838+
)
839+
assert result.returncode == 0, result.stderr
840+
841+
823842
def test_configure_editor_uses_configured_editor(monkeypatch: pytest.MonkeyPatch) -> None:
824843
cli = make_repl_cli()
825844
cli.config['editor']['editor_command'] = 'nano'

0 commit comments

Comments
 (0)