Repository navigation
fix: restore stdout when print_confusion_matrix output_file write fails - #394
Conversation
Use contextlib.redirect_stdout to ensure sys.stdout is always restored, even when an error occurs during file writing. Previously, any exception between stdout reassignment and restoration would leave stdout pointing at a closed file, breaking all subsequent print() calls in the process. Also catch only OSError from open() instead of all exceptions, and add missing bulk_info_summary to file output branch. Fixes awslabs#385
Remove OSError narrowing and bulk_info_summary addition from the previous commit to focus solely on fixing the stdout restoration bug described in issue awslabs#385.
sromoam
left a comment
There was a problem hiding this comment.
Thanks @hippi345, this fixes what #385 reported for print_confusion_matrix. With redirect_stdout, stdout is restored when a write fails, and the new test tells the old code from the new: the old handler raised ValueError, the new code raises the real re.error. Approving.
The same manual sys.stdout swap is still in print_non_matches (pretty_print.py:951), print_confusion_matrix_html (:1364) and print_evaluation_results (:1282), and the file branch still skips the bulk summary. All of that predates this PR, so I'll open a follow-up issue rather than hold this one. If you'd like to take it, it's yours.
Optional:
pretty_print.py:349:except Exceptionalso catches non-I/O errors such as aTypeErrorfrom a bad path type.except OSErrorwould report only real open failures.test_confusion_matrix_printing.py:104: pytest'stmp_pathfixture would clean up after itself.NamedTemporaryFile(delete=False)leaves a file behind.
I'll add the CHANGELOG line when we cut the release.
Security Scan ResultsASH Security Scan Report
Scan Metadata
SummaryScanner ResultsThe table below shows findings by scanner, with status based on severity thresholds and dependencies:
Top 2 HotspotsFiles with the highest number of security findings:
Detailed FindingsShow 4 actionable findingsFinding 1: SECRET-HEX-HIGH-ENTROPY-STRING
Description: Code Snippet: Finding 2: B108
Description: Code Snippet: Finding 3: B307
Description: Code Snippet: Finding 4: B307
Description: Code Snippet: Report generated by Automated Security Helper (ASH) at 2026-10-19T16:02:38+00:00 |
fix: restore stdout when print_confusion_matrix output_file write fails
Pull Request
Issue Reference
Fixes #385
Description of Changes
When
print_confusion_matrix(..., output_file=...)is called with an invalidfield_filterregex or encounters any error during file writing,sys.stdoutis left pointing at a closed file, breaking all subsequentprint()calls in the process.Changes Made
sys.stdoutreassignment withcontextlib.redirect_stdout()to ensure stdout is always restored on any exit (success or exception)Why These Changes Are Needed
The bug made it impossible to recover from errors in
print_confusion_matrixwhen usingoutput_file. After the error, the entire Python process could not print to stdout, making debugging extremely difficult.Possible follow-up
The file-output branch skips
_print_bulk_info_summary(), which the console-output branch includes, so file output for a bulk result leaves out the processing time, field count, and error count. This could be addressed separately.Testing
test_print_confusion_matrix_output_file_error_restores_stdoutthat uses invalid regex to triggerre.errormid-writeuv sync --all-extras): 2797 passed, 2 skipped, 1 xfailedLint checks pass for modified files.
Checklist
CHANGELOG.mdentry added under[Unreleased], or not needed becausenothing user-visible changed (see RELEASING.md) - Maintainer to decide if entry needed for bugfix
Additional Notes
This contribution was prepared with AI assistance (Cursor Agent).