gen_bcpack_runtime_tables: limit TypeError/KeyError catch to --profile path

This commit is contained in:
kirigayakazima
2026-11-27 19:56:51 +08:00
parent 8dfb5cd585
commit 8b2839dd5f
2 changed files with 26 additions and 6 deletions

View File

@ -146,5 +146,16 @@ class GenBcpackRuntimeTablesTests(unittest.TestCase):
self.assertEqual(sorted(os.listdir(tmp)), ["cal.json", "profile.bin"])
def test_generate_bugs_propagate_instead_of_exiting_2(self):
with tempfile.TemporaryDirectory() as tmp:
out_inc = os.path.join(tmp, "out.inc")
for exc in (TypeError("internal bug"), KeyError("missing_key")):
with self.subTest(exc=type(exc).__name__):
with mock.patch.object(gen, "generate", side_effect=exc):
with self.assertRaises(type(exc)):
gen.main([out_inc])
self.assertFalse(os.path.exists(out_inc))
if __name__ == "__main__":
unittest.main()

View File

@ -157,14 +157,23 @@ def main(argv=None) -> int:
parser.error("cannot combine positional output with --profile")
if not args.profile and not args.output:
parser.error("name the tables file to write, or use --profile")
try:
if args.profile:
if args.profile:
try:
write_profile(*args.profile)
else:
except (OSError, ValueError, KeyError, TypeError, RecursionError) as e:
print(f"gen_bcpack_runtime_tables: {e}", file=sys.stderr)
return 2
else:
try:
_validate_output_path(args.output, "output")
except ValueError as e:
print(f"gen_bcpack_runtime_tables: {e}", file=sys.stderr)
return 2
try:
_atomic_write(args.output, generate, binary=False, label="output")
except (OSError, ValueError, KeyError, TypeError, RecursionError) as e:
print(f"gen_bcpack_runtime_tables: {e}", file=sys.stderr)
return 2
except OSError as e:
print(f"gen_bcpack_runtime_tables: {e}", file=sys.stderr)
return 2
return 0