Skip to content

Commit 2f62e6e

Browse files
committed
Fix default encoding in cacheprovider
1 parent 33bf914 commit 2f62e6e

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

changelog/9910.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix default encoding warning (``EncodingWarning``) in ``cacheprovider``

src/_pytest/cacheprovider.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def get(self, key: str, default):
157157
"""
158158
path = self._getvaluepath(key)
159159
try:
160-
with path.open("r") as f:
160+
with path.open("r", encoding="UTF-8") as f:
161161
return json.load(f)
162162
except (ValueError, OSError):
163163
return default
@@ -184,9 +184,9 @@ def set(self, key: str, value: object) -> None:
184184
return
185185
if not cache_dir_exists_already:
186186
self._ensure_supporting_files()
187-
data = json.dumps(value, indent=2)
187+
data = json.dumps(value, ensure_ascii=False, indent=2)
188188
try:
189-
f = path.open("w")
189+
f = path.open("w", encoding="UTF-8")
190190
except OSError:
191191
self.warn("cache could not write path {path}", path=path, _ispytest=True)
192192
else:
@@ -196,7 +196,7 @@ def set(self, key: str, value: object) -> None:
196196
def _ensure_supporting_files(self) -> None:
197197
"""Create supporting files in the cache dir that are not really part of the cache."""
198198
readme_path = self._cachedir / "README.md"
199-
readme_path.write_text(README_CONTENT)
199+
readme_path.write_text(README_CONTENT, encoding="UTF-8")
200200

201201
gitignore_path = self._cachedir.joinpath(".gitignore")
202202
msg = "# Created by pytest automatically.\n*\n"

0 commit comments

Comments
 (0)