Decode message context when reading MO files#1280
Open
sarathfrancis90 wants to merge 1 commit into
Open
Conversation
read_mo left the context as raw bytes while the msgid and msgstr were decoded to str using the catalog charset. That made the context inconsistent with the rest of the message and with the standard library gettext parser, and broke round-tripping: a catalog written with write_mo (which encodes the context) could not be looked up again with a str context, since the key was stored as (id, bytes). Decode the context with the catalog charset, matching the handling of the msgid and msgstr. Fixes python-babel#1147.
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.
When reading an MO file,
read_moleft the message context as raw bytes while the msgid and msgstr got decoded tostrwith the catalog charset. That made the context inconsistent with the rest of the message, and it differs from the standard library's gettext parser, which decodes the whole entry (context included).The practical effect is a broken round-trip: a catalog written with
write_mo(which.encode()s the context) reads back with the key stored as(id, bytes), socatalog.get('foo', context='fooctxt')returnsNoneand you can only look it up by passing the context as bytes.The fix decodes the context with the catalog charset right after the
\x04split, the same way the msgid/msgstr are handled a few lines down.I added a test that writes a catalog with a context, reads it back, and checks that the context is a
strand that the message is retrievable with astrcontext. It fails before the change and passes after. Fulltests/messagessuite stays green.Fixes #1147.