Skip to content

fix(export): move EXIF tag parsing out of the finally block - #907

Merged
marcinz606 merged 1 commit into
marcinz606:mainfrom
MohammedAlkindi:fix/exif-meta-return-in-finally
Aug 19, 2026
Merged

fix(export): move EXIF tag parsing out of the finally block#907
marcinz606 merged 1 commit into
marcinz606:mainfrom
MohammedAlkindi:fix/exif-meta-return-in-finally

Conversation

@MohammedAlkindi

Copy link
Copy Markdown
Contributor

_read_source_meta_exif silences the tifffile logger, opens the embedded TIFF block in a try, then does all of its parsing inside the finally and returns from there.

Two consequences. A return in finally discards any in-flight exception, so Python 3.14 emits SyntaxWarning: 'return' in a 'finally' block on every import of this module. And the parsing reads tif, which is unbound when TiffFile() raises, so a malformed EXIF block came back as UnboundLocalError rather than the real tifffile error:

UnboundLocalError: cannot access local variable 'tif'

The outer except Exception caught either way, so callers still got an empty _SourceMeta and no behaviour changes here. The real cause was just unreachable.

The finally now only restores the logger level, and the parsing sits after it.

TestSourceMetaExifFallback is new; this function had no coverage. The SyntaxWarning assertion fails against the current source and passes after; the other three pass both ways.

Windows 11, Python 3.14.7, your [dev] group: 4306 passed, 10 skipped on tests/ with -m "not slow and not metrics". The 2 failures are test_backend_registry.py plustek cases needing the optional extra, failing identically without this change. ruff check and ruff format --check clean.

Your CI is ubuntu-latest on one Python, so the warning would not surface there yet.

`_read_source_meta_exif` silences the tifffile logger, opens the embedded
TIFF block in a `try`, and then does all of its parsing inside the `finally`,
ending with a `return`.

Two consequences. A `return` in `finally` discards any in-flight exception, so
Python 3.14 emits `SyntaxWarning: 'return' in a 'finally' block` on every
import of this module. And the parsing reads `tif`, which is unbound when
`TiffFile()` raises, so a malformed EXIF block surfaced as `UnboundLocalError`
instead of the real tifffile error. The outer `except Exception` swallowed
either way, so callers still got an empty `_SourceMeta`, but the real cause was
unreachable.

The `finally` now does only what it exists for, restoring the logger level.
The parsing and the `return` sit after it, so a decode failure propagates
normally.

Behaviour is unchanged: a good EXIF block parses as before, a bad one still
returns an empty `_SourceMeta`.

Adds `TestSourceMetaExifFallback`, which had no coverage. The
SyntaxWarning-free assertion fails against the current source and passes after.
@marcinz606
marcinz606 merged commit 19f704d into marcinz606:main Aug 19, 2026
1 check passed
@MohammedAlkindi
MohammedAlkindi deleted the fix/exif-meta-return-in-finally branch August 19, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants