mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 02:24:51 -05:00
Pass encoding to AtomicWriter in write_utf8_file_atomic (#164015)
This commit is contained in:
@@ -32,8 +32,11 @@ def write_utf8_file_atomic(
|
||||
Using this function frequently will significantly
|
||||
negatively impact performance.
|
||||
"""
|
||||
encoding = "utf-8" if "b" not in mode else None
|
||||
try:
|
||||
with AtomicWriter(filename, mode=mode, overwrite=True).open() as fdesc:
|
||||
with AtomicWriter( # type: ignore[call-arg] # atomicwrites-stubs is outdated, encoding is a valid kwarg
|
||||
filename, mode=mode, overwrite=True, encoding=encoding
|
||||
).open() as fdesc:
|
||||
if not private:
|
||||
os.fchmod(fdesc.fileno(), 0o644)
|
||||
fdesc.write(utf8_data)
|
||||
|
||||
@@ -83,6 +83,19 @@ def test_write_utf8_file_fails_at_rename_and_remove(
|
||||
assert "File replacement cleanup failed" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize("func", [write_utf8_file, write_utf8_file_atomic])
|
||||
def test_write_utf8_file_with_non_ascii_content(tmp_path: Path, func) -> None:
|
||||
"""Test files with non-ASCII content can be written even when locale is ASCII."""
|
||||
test_file = tmp_path / "test.json"
|
||||
non_ascii_data = '{"name":"自动化","emoji":"🏠"}'
|
||||
|
||||
with patch("locale.getpreferredencoding", return_value="ascii"):
|
||||
func(test_file, non_ascii_data, False)
|
||||
|
||||
file_text = test_file.read_text(encoding="utf-8")
|
||||
assert file_text == non_ascii_data
|
||||
|
||||
|
||||
def test_write_utf8_file_atomic_fails(tmpdir: py.path.local) -> None:
|
||||
"""Test OSError from write_utf8_file_atomic is rethrown as WriteError."""
|
||||
test_dir = tmpdir.mkdir("files")
|
||||
|
||||
Reference in New Issue
Block a user