From 7173ddee5f979a3c54be7905d6c00aac29a90fda Mon Sep 17 00:00:00 2001 From: Alessandro Gatti Date: Sat, 20 Dec 2025 17:03:18 +0100 Subject: [PATCH] tests/run-natmodtests.py: Explicitly open prelude file. This change lets the natmod test runner report status information on session end if a prelude script file is chosen. The script serialises its input data as part of the end of session reporting data, but since the prelude file is not a serialisable object serialisation would fail (it's a file handle as far as the argument container structure is aware). Now the file is explicitly open by the script rather than relying on argparse's file handle argument class wrapper. Signed-off-by: Alessandro Gatti --- tests/run-natmodtests.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/run-natmodtests.py b/tests/run-natmodtests.py index 50e8d12729..e35e580dc1 100755 --- a/tests/run-natmodtests.py +++ b/tests/run-natmodtests.py @@ -133,13 +133,6 @@ def detect_architecture(target): def run_tests(target_truth, target, args, resolved_arch): - global injected_import_hook_code - - prelude = "" - if args.begin: - prelude = args.begin.read() - injected_import_hook_code = injected_import_hook_code.replace("{import_prelude}", prelude) - test_results = [] for test_file in args.files: # Find supported test @@ -223,6 +216,8 @@ def run_tests(target_truth, target, args, resolved_arch): def main(): + global injected_import_hook_code + cmd_parser = argparse.ArgumentParser( description="Run dynamic-native-module tests under MicroPython", epilog=run_tests_module.test_instance_epilog, @@ -240,7 +235,7 @@ def main(): cmd_parser.add_argument( "-b", "--begin", - type=argparse.FileType("rt"), + metavar="PROLOGUE", default=None, help="prologue python file to execute before module import", ) @@ -253,6 +248,12 @@ def main(): cmd_parser.add_argument("files", nargs="*", help="input test files") args = cmd_parser.parse_args() + prologue = "" + if args.begin: + with open(args.begin, "rt") as source: + prologue = source.read() + injected_import_hook_code = injected_import_hook_code.replace("{import_prelude}", prologue) + target_truth = TargetSubprocess([CPYTHON3]) target = run_tests_module.get_test_instance(