From 69635b809a146b5d0985c58e05d5305d1e3febed Mon Sep 17 00:00:00 2001 From: Philipp Wullstein-Kammler <111539239+phiwuu@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:14:58 +0200 Subject: [PATCH] Catch `UnicodeDecodeError` in `lobster-json` (#103) The `UnicodeDecodeError` is catched, and an error message is printed. Afterwards `lobster-json` exits. Issue: #102 --- lobster/tools/json/json.py | 17 +++++++---------- setup.cfg | 1 + 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lobster/tools/json/json.py b/lobster/tools/json/json.py index 6cd70142..b71b6355 100755 --- a/lobster/tools/json/json.py +++ b/lobster/tools/json/json.py @@ -53,14 +53,11 @@ def get_item(root, path, required): elif required: raise Malformed_Input("object does not contain %s" % field, root) - else: - return None + return None elif required: raise Malformed_Input("not an object", root) - - else: - return None + return None def syn_test_name(file_name): @@ -107,15 +104,15 @@ def process_tool_options(self, options, work_list): @classmethod def process(cls, options, file_name): - with open(file_name, "r", encoding="UTF-8") as fd: - data = json.load(fd) - - # First we follow the test-list items to get the actual data - # we're interested in. try: + with open(file_name, "r", encoding="UTF-8") as fd: + data = json.load(fd) data = get_item(root = data, path = options.test_list, required = True) + except UnicodeDecodeError as decode_error: + print("%s: File is not encoded in utf-8: %s" % (file_name, decode_error)) + return False, [] except Malformed_Input as err: pprint(err.data) print("%s: malformed input: %s" % (file_name, err.msg)) diff --git a/setup.cfg b/setup.cfg index 15f2b3c4..905048ba 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,3 @@ [pycodestyle] ignore = E203, W504, E221, E251, E129, E266, E127 +max-line-length = 88