Skip to content

Decode message context when reading MO files#1280

Open
sarathfrancis90 wants to merge 1 commit into
python-babel:masterfrom
sarathfrancis90:fix-mo-context-decoding
Open

Decode message context when reading MO files#1280
sarathfrancis90 wants to merge 1 commit into
python-babel:masterfrom
sarathfrancis90:fix-mo-context-decoding

Conversation

@sarathfrancis90

Copy link
Copy Markdown

When reading an MO file, read_mo left the message context as raw bytes while the msgid and msgstr got decoded to str with 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), so catalog.get('foo', context='fooctxt') returns None and you can only look it up by passing the context as bytes.

The fix decodes the context with the catalog charset right after the \x04 split, 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 str and that the message is retrievable with a str context. It fails before the change and passes after. Full tests/messages suite stays green.

Fixes #1147.

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.
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.

read_mo does not decode msgctxt

1 participant