Repository navigation
Restore pydicom reading validation mode after file validation - #303
mrbean-bremen merged 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #303 +/- ##
==========================================
+ Coverage 89.82% 90.42% +0.59%
==========================================
Files 15 21 +6
Lines 1632 2130 +498
==========================================
+ Hits 1466 1926 +460
- Misses 166 204 +38 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| file_path=file_path, | ||
| ).validate() | ||
| } | ||
| finally: |
There was a problem hiding this comment.
Thanks! I generally prefer to have the logic for restoring the mode in a separate small context manager and use it like:
with set_validation_mode(config.IGNORE):
# do stuffThere was a problem hiding this comment.
Implemented in daccc30. The restoration logic now lives in set_validation_mode(), and file validation uses with set_validation_mode(config.IGNORE). The tests cover restoration after normal completion, exceptions and nested contexts. Thanks for the suggestion.
| """Temporarily set pydicom's process-wide reading validation mode. | ||
|
|
||
| Restore the previous mode on exit, including when the body raises. | ||
| This does not change writing validation and is not thread-safe. |
There was a problem hiding this comment.
Probably better name it set_reading_validation_mode, so there is no need to clarify this in the comment.
Describe the changes
DicomFileValidator.validate_file permanently assigns pydicom's process-wide reading_validation_mode to IGNORE, affecting subsequent pydicom users in the same process even when validation raises.
Use a small
set_validation_mode(mode)context manager to save the incoming reading mode and restore it infinally. Wrap file reading, dataset validation, and result handling inwith set_validation_mode(config.IGNORE):, as requested in review. Keep IODValidator construction and validation in the inner try statement's else suite so IGNORE remains active for deferred value conversion and result handling, while preserving the original narrow InvalidDicomError catch around dcmread.Related historical context: #102 (already closed).
Validation
Tested on Linux with Python 3.12.14 and pydicom 3.0.2.
Context-manager review update
set_validation_mode(mode)and adopted the requestedwith set_validation_mode(config.IGNORE):call site.Compatibility
pydicom's setting remains process-wide. Restoring it fixes leakage after a call but does not make concurrent pydicom reads or concurrent validations thread-safe. The patch explicitly documents this limitation and does not claim thread isolation.
Tasks
The focused regression, formatting and documentation results are reused from the reviewed Linux preparation at the unchanged base. The standard-dependent checks above were completed separately on macOS; no full platform-matrix result is claimed.