From a91de43537f8a76a78d9576d7ab0e2378c374f07 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 16 Dec 2019 18:18:20 +1100 Subject: [PATCH] parttool: Avoid unnecessary exception backtrace for legitimate errors Previous version used exception handling for program control flow, which makes for long and confusing backtrace messages if an error occurs while parsing the CSV (as it also prints the phony error that occurred while trying to parse as a binary, then prints "During handling of the above exception, another exception occurred:", then prints the real error). Use the same heuristic that is applied in gen_esp32_part, instead. Also, avoid printing the entire backtrace if the error is a gen_esp32_part InputError, same as gen_esp32part does. Found while looking into https://github.com/espressif/esp-idf/issues/4474 --- components/partition_table/parttool.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/components/partition_table/parttool.py b/components/partition_table/parttool.py index 84a0d1eaf..6cef54add 100755 --- a/components/partition_table/parttool.py +++ b/components/partition_table/parttool.py @@ -93,10 +93,13 @@ class ParttoolTarget(): self.esptool_erase_args = parse_esptool_args(esptool_erase_args) if partition_table_file: - try: - with open(partition_table_file, "rb") as f: + partition_table = None + with open(partition_table_file, "rb") as f: + input_is_binary = (f.read(2) == gen.PartitionDefinition.MAGIC_BYTES) + if input_is_binary: partition_table = gen.PartitionTable.from_binary(f.read()) - except (gen.InputError, IOError, TypeError): + + if partition_table is None: with open(partition_table_file, "r") as f: f.seek(0) partition_table = gen.PartitionTable.from_csv(f.read()) @@ -331,7 +334,11 @@ def main(): except Exception: sys.exit(2) else: - op(**common_args) + try: + op(**common_args) + except gen.InputError as e: + print(e, file=sys.stderr) + sys.exit(2) if __name__ == '__main__':