Skip to content

Commit ebc9d02

Browse files
committed
fix: make idf_size.py compatible with python3.8
Previous 6caa4a1 ("fix: display correct help in the idf_size.py wrapper") introduced a regression, because it uses exit_on_error parameter for argparse.ArgumentParser, which was added in python3.9, making idf_size.py incompatible with idf.py minimal required python3.8. The objective is to inspect the arguments of idf_size.py using a wrapper argparse to determine whether the legacy or refactored version should be initiated, while always displaying help for the underlying version. The exit_on_error function was previously utilized to prevent argparse from exiting and displaying help/usage. This replaces exit_on_error with a workaround that makes the --format argument optional. Since this is the sole instance where the wrapper argparse might fail, it achieves the same outcome as using exit_on_error. Fixes: 6caa4a1 ("fix: display correct help in the idf_size.py wrapper") Signed-off-by: Frantisek Hrbata <frantisek.hrbata@espressif.com>
1 parent ec50cd7 commit ebc9d02

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

tools/idf_size.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
# Here the argparse is used only to "peek" into arguments if
1414
# legacy version is requested or if old json format is specified.
1515
# In these two cases the esp_idf_size legacy version is spawned.
16-
parser = argparse.ArgumentParser(exit_on_error=False, add_help=False)
17-
parser.add_argument('--format')
16+
parser = argparse.ArgumentParser(add_help=False)
17+
# Make the --format arg optional, so this argparse instance does not
18+
# fail with an error and the proper underlying help is displayed.
19+
# Note that exit_on_error is supported from python3.9, so this is
20+
# a workaround how to make sure that the args parsing doesn't fail here if idf_size.py
21+
# is invoked e.g. without specifying the format, like "idf_size.py --format".
22+
parser.add_argument('--format', nargs='?')
1823
parser.add_argument('-l', '--legacy', action='store_true', default=os.environ.get('ESP_IDF_SIZE_LEGACY', '0') == '1')
1924

2025
# The sys.argv is parsed with "exit_on_error", but the argparse.ArgumentError
@@ -34,7 +39,9 @@
3439
os.environ['ESP_IDF_SIZE_NG'] = '1'
3540
if not rest or '-h' in rest or '--help' in rest:
3641
print(('Note: legacy esp_idf_size version can be invoked by specifying the -l/--legacy '
37-
'option or by setting the ESP_IDF_SIZE_LEGACY environment variable.'))
42+
'option or by setting the ESP_IDF_SIZE_LEGACY environment variable. Additionally, the '
43+
'legacy version is automatically employed when the JSON format is specified for '
44+
'compatibility with previous versions.'))
3845

3946
if args.format is not None:
4047
rest = ['--format', args.format] + rest

0 commit comments

Comments
 (0)