Fix UnicodeEncodeError from non-ASCII EXIF metadata on export (#452)#454
Open
RP2 wants to merge 1 commit into
Open
Fix UnicodeEncodeError from non-ASCII EXIF metadata on export (#452)#454RP2 wants to merge 1 commit into
RP2 wants to merge 1 commit into
Conversation
_decode_ascii's str branch returned non-ASCII characters unchanged.
When this string reached tifffile.imwrite(description=...), tifffile's
.encode('ascii') raised UnicodeEncodeError, crashing export.
The bytes branch had the same issue -- decode('ascii', 'replace')
produces U+FFFD (replacement character), which is also non-ASCII and
would crash tifffile.
Fix both branches by round-tripping through encode/decode ASCII with
replacement, producing pure ASCII output with '?' as fallback.
Adds 7 tests covering both branches, edge cases, and the exact crash
scenario (non-ASCII in EXIF ImageDescription through embed_metadata).
Fixes marcinz606#452
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #452 -- export crashes with UnicodeEncodeError when source EXIF metadata contains non-ASCII characters like the multiplication sign.
Root cause
_decode_asciiinnegpy/features/metadata/writer.pyis the single chokepoint for all EXIF string values flowing intotifffile.imwrite. Its str branch returned non-ASCII characters unchanged, and its bytes branch produced U+FFFD (also non-ASCII). When either reachedtifffile.imwrite(description=...), tifffile's bare.encode('ascii')raisedUnicodeEncodeError.The error appeared as "Failed to load file" because the export worker's error signal connects to
_on_render_error, which prefixes all messages with that string.Fix
Make both branches round-trip through
encode("ascii", "replace")thendecode("ascii"), replacing non-ASCII characters with?. Consistent with the existing_exif_asciihelper used for NegPy-authored metadata.Files changed:
negpy/features/metadata/writer.py-- the fixtests/metadata/test_writer.py-- 7 new testsVerification
embed_metadata)