Fix default encoding in cacheprovider

This commit is contained in:
Peyman Salehi 2022-05-03 20:56:46 +04:30
parent 33bf91482e
commit 2f62e6ec96
No known key found for this signature in database
GPG Key ID: 027629CB37ABAD00
2 changed files with 5 additions and 4 deletions

View File

@ -0,0 +1 @@
Fix default encoding warning (``EncodingWarning``) in ``cacheprovider``

View File

@ -157,7 +157,7 @@ class Cache:
""" """
path = self._getvaluepath(key) path = self._getvaluepath(key)
try: try:
with path.open("r") as f: with path.open("r", encoding="UTF-8") as f:
return json.load(f) return json.load(f)
except (ValueError, OSError): except (ValueError, OSError):
return default return default
@ -184,9 +184,9 @@ class Cache:
return return
if not cache_dir_exists_already: if not cache_dir_exists_already:
self._ensure_supporting_files() self._ensure_supporting_files()
data = json.dumps(value, indent=2) data = json.dumps(value, ensure_ascii=False, indent=2)
try: try:
f = path.open("w") f = path.open("w", encoding="UTF-8")
except OSError: except OSError:
self.warn("cache could not write path {path}", path=path, _ispytest=True) self.warn("cache could not write path {path}", path=path, _ispytest=True)
else: else:
@ -196,7 +196,7 @@ class Cache:
def _ensure_supporting_files(self) -> None: def _ensure_supporting_files(self) -> None:
"""Create supporting files in the cache dir that are not really part of the cache.""" """Create supporting files in the cache dir that are not really part of the cache."""
readme_path = self._cachedir / "README.md" readme_path = self._cachedir / "README.md"
readme_path.write_text(README_CONTENT) readme_path.write_text(README_CONTENT, encoding="UTF-8")
gitignore_path = self._cachedir.joinpath(".gitignore") gitignore_path = self._cachedir.joinpath(".gitignore")
msg = "# Created by pytest automatically.\n*\n" msg = "# Created by pytest automatically.\n*\n"