from pathlib import Path p=Path('SYS/pipeline.py') s=p.read_text(encoding='utf-8') lines=s.splitlines() stack=[] for i,l in enumerate(lines,1): stripped=l.strip() # Skip commented lines if stripped.startswith('#'): continue # compute indent as leading spaces (tabs are converted) indent = len(l) - len(l.lstrip(' ')) if stripped.startswith('try:'): stack.append((indent, i)) if stripped.startswith('except ') or stripped=='except:' or stripped.startswith('finally:'): # find the most recent try with same indent for idx in range(len(stack)-1, -1, -1): if stack[idx][0] == indent: stack.pop(idx) break else: # no matching try at same indent print(f"Found {stripped.split()[0]} at line {i} with no matching try at same indent") print('Unmatched try count', len(stack)) if stack: print('Unmatched try positions (indent, line):', stack) for indent, lineno in stack: start = max(1, lineno - 10) end = min(len(lines), lineno + 10) print(f"Context around line {lineno}:") for i in range(start, end + 1): print(f"{i:5d}: {lines[i-1]}") else: print("All try statements appear matched")