Skip to content

Commit 33d1978

Browse files
authored
Merge pull request #4067 from ashnaaseth2325-oss/fix/addDummyToLef-fd-leaks
fix(addDummyToLef): prevent fd leaks and add input file validation with clear error handling
2 parents 53ad587 + fa84634 commit 33d1978

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

flow/util/addDummyToLef.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
import os
23
import re
34
import sys
45
import argparse # argument parsing
@@ -14,9 +15,12 @@
1415
args = parser.parse_args()
1516

1617

17-
f = open(args.inputLef)
18-
content = f.read()
19-
f.close()
18+
if not os.path.isfile(args.inputLef):
19+
print(f"Error: Input LEF not found: {args.inputLef}", file=sys.stderr)
20+
sys.exit(1)
21+
22+
with open(args.inputLef) as f:
23+
content = f.read()
2024

2125
# refMacro = "BUFH_X1M_A12TR"
2226

@@ -26,9 +30,8 @@
2630
result, count = re.subn(pattern, replace, content, 1, re.S)
2731

2832
if count > 0:
29-
f = open(args.outputLef, "w")
30-
f.write(result)
31-
f.close()
33+
with open(args.outputLef, "w") as f:
34+
f.write(result)
3235
else:
33-
print("Error: Pattern not found")
36+
print("Error: Pattern not found", file=sys.stderr)
3437
sys.exit(1)

0 commit comments

Comments
 (0)