From f5cfd1ff22fa031ccec54e2045e8e8ca693d50d0 Mon Sep 17 00:00:00 2001 From: Loic Huder Date: Fri, 11 Oct 2024 09:26:35 +0200 Subject: [PATCH] Ensure that JSON files are proprely closed in localization --- orangecanvas/utils/localization/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/orangecanvas/utils/localization/__init__.py b/orangecanvas/utils/localization/__init__.py index 8e80319d..aa8a722a 100644 --- a/orangecanvas/utils/localization/__init__.py +++ b/orangecanvas/utils/localization/__init__.py @@ -39,6 +39,9 @@ def pl(n: int, forms: str) -> str: # pylint: disable=invalid-name word = word.upper() return word +def _load_json(path): + with open(path) as handle: + return json.load(handle) @lru_cache def get_languages(package=None): @@ -52,7 +55,7 @@ def get_languages(package=None): for name, ext in map(os.path.splitext, os.listdir(msgs_path)): if ext == ".json": try: - msgs = json.load(open(os.path.join(msgs_path, name + ext))) + msgs = _load_json(os.path.join(msgs_path, name + ext)) except json.JSONDecodeError: warnings.warn("Invalid language file " + os.path.join(msgs_path, name + ext)) @@ -102,7 +105,7 @@ def __init__(self, package, organization="biolab.si", application="Orange"): if not os.path.exists(path): path = os.path.join(package_path, "i18n", f"{DEFAULT_LANGUAGE}.json") assert os.path.exists(path), f"Missing language file {path}" - self.m = _list(json.load(open(path))) + self.m = _list(_load_json(path)) # Extra argument(s) can give the original string or any other relevant data def c(self, idx, *_):