From fa84634c9da6eb97f054e9c71c21de8b501be216 Mon Sep 17 00:00:00 2001 From: Ashnaa Seth Date: Sat, 28 Mar 2026 13:18:16 +0000 Subject: [PATCH] util: use context managers in addDummyToLef.py to fix fd leaks Replace bare open()/close() pairs with with-statements and add an input file existence check with a clear error message. Signed-off-by: Ashnaa Seth Signed-off-by: ashnaaseth2325-oss --- flow/util/addDummyToLef.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/flow/util/addDummyToLef.py b/flow/util/addDummyToLef.py index 3c0c828eec..2dc433f40e 100755 --- a/flow/util/addDummyToLef.py +++ b/flow/util/addDummyToLef.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import os import re import sys import argparse # argument parsing @@ -14,9 +15,12 @@ args = parser.parse_args() -f = open(args.inputLef) -content = f.read() -f.close() +if not os.path.isfile(args.inputLef): + print(f"Error: Input LEF not found: {args.inputLef}", file=sys.stderr) + sys.exit(1) + +with open(args.inputLef) as f: + content = f.read() # refMacro = "BUFH_X1M_A12TR" @@ -26,9 +30,8 @@ result, count = re.subn(pattern, replace, content, 1, re.S) if count > 0: - f = open(args.outputLef, "w") - f.write(result) - f.close() + with open(args.outputLef, "w") as f: + f.write(result) else: - print("Error: Pattern not found") + print("Error: Pattern not found", file=sys.stderr) sys.exit(1)