From 9da353e60d22523b62f4173aa84b00ca01b95664 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 21 Jan 2025 12:49:00 -0500 Subject: [PATCH] Add explicit text mode to zopen calls (monty warning) --- src/maggma/stores/file_store.py | 2 +- src/maggma/stores/mongolike.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/maggma/stores/file_store.py b/src/maggma/stores/file_store.py index 54fb6d8a4..4d6fbd8b4 100644 --- a/src/maggma/stores/file_store.py +++ b/src/maggma/stores/file_store.py @@ -442,7 +442,7 @@ def query( # type: ignore # TODO - could add more logic for detecting different file types # and more nuanced exception handling try: - with zopen(d["path"], "r", encoding=self.encoding) as f: + with zopen(d["path"], "rt", encoding=self.encoding) as f: data = f.read() except Exception as e: data = f"Unable to read: {e}" diff --git a/src/maggma/stores/mongolike.py b/src/maggma/stores/mongolike.py index a3c607057..34d50f33f 100644 --- a/src/maggma/stores/mongolike.py +++ b/src/maggma/stores/mongolike.py @@ -684,7 +684,7 @@ def connect(self, force_reset: bool = False): # create the .json file if it does not exist if not self.read_only and not Path(self.paths[0]).exists(): - with zopen(self.paths[0], "w", encoding=self.encoding) as f: + with zopen(self.paths[0], "wt", encoding=self.encoding) as f: data: list[dict] = [] bytesdata = orjson.dumps(data) f.write(bytesdata.decode("utf-8")) @@ -711,7 +711,7 @@ def read_json_file(self, path) -> list: Args: path: Path to the JSON file to be read """ - with zopen(path, "r") as f: + with zopen(path, "rt") as f: data = f.read() data = data.decode() if isinstance(data, bytes) else data objects = bson.json_util.loads(data) if "$oid" in data else orjson.loads(data) @@ -761,7 +761,7 @@ def update_json_file(self): """ Updates the json file when a write-like operation is performed. """ - with zopen(self.paths[0], "w", encoding=self.encoding) as f: + with zopen(self.paths[0], "wt", encoding=self.encoding) as f: data = list(self.query()) for d in data: d.pop("_id")