From d0081d09f54aeed72c128ac63e8db03b7bc83a40 Mon Sep 17 00:00:00 2001 From: GalaxySnail Date: Sun, 17 Apr 2022 17:14:10 +0800 Subject: [PATCH 1/3] Fix some EncodingWarnings in python 3.10 (PEP 597) --- piptools/cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/piptools/cache.py b/piptools/cache.py index da325b27d..228b89e70 100644 --- a/piptools/cache.py +++ b/piptools/cache.py @@ -41,7 +41,7 @@ def __str__(self) -> str: def read_cache_file(cache_file_path: str) -> CacheDict: - with open(cache_file_path) as cache_file: + with open(cache_file_path, encoding="ascii") as cache_file: try: doc = json.load(cache_file) except json.JSONDecodeError: @@ -108,7 +108,7 @@ def as_cache_key(self, ireq: InstallRequirement) -> CacheKey: def write_cache(self) -> None: """Writes the cache to disk as JSON.""" doc = {"__format__": 1, "dependencies": self._cache} - with open(self._cache_file, "w") as f: + with open(self._cache_file, "w", encoding="ascii") as f: json.dump(doc, f, sort_keys=True) def clear(self) -> None: From 6c0658bce0c7aa2c9531d465164404688731b563 Mon Sep 17 00:00:00 2001 From: GalaxySnail Date: Fri, 7 Oct 2022 20:32:14 +0800 Subject: [PATCH 2/3] Use utf-8 instead of ascii for cache file --- piptools/cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/piptools/cache.py b/piptools/cache.py index 228b89e70..fd8a2cf2f 100644 --- a/piptools/cache.py +++ b/piptools/cache.py @@ -41,7 +41,7 @@ def __str__(self) -> str: def read_cache_file(cache_file_path: str) -> CacheDict: - with open(cache_file_path, encoding="ascii") as cache_file: + with open(cache_file_path, encoding="utf-8") as cache_file: try: doc = json.load(cache_file) except json.JSONDecodeError: @@ -108,7 +108,7 @@ def as_cache_key(self, ireq: InstallRequirement) -> CacheKey: def write_cache(self) -> None: """Writes the cache to disk as JSON.""" doc = {"__format__": 1, "dependencies": self._cache} - with open(self._cache_file, "w", encoding="ascii") as f: + with open(self._cache_file, "w", encoding="utf-8") as f: json.dump(doc, f, sort_keys=True) def clear(self) -> None: From 64fefa152f192131225b09da272f0fd6c780f626 Mon Sep 17 00:00:00 2001 From: GalaxySnail Date: Fri, 7 Oct 2022 20:43:39 +0800 Subject: [PATCH 3/3] Fix UnicodeDecodeError crash --- piptools/cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/piptools/cache.py b/piptools/cache.py index fd8a2cf2f..b6360a130 100644 --- a/piptools/cache.py +++ b/piptools/cache.py @@ -44,7 +44,7 @@ def read_cache_file(cache_file_path: str) -> CacheDict: with open(cache_file_path, encoding="utf-8") as cache_file: try: doc = json.load(cache_file) - except json.JSONDecodeError: + except (json.JSONDecodeError, UnicodeDecodeError): raise CorruptCacheError(cache_file_path) # Check version and load the contents