Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import datetime
import re
from collections.abc import Iterable, Iterator
from contextlib import suppress
from copy import copy
from difflib import SequenceMatcher
from email import message_from_string
Expand Down Expand Up @@ -582,11 +583,19 @@ def _set_mime_headers(self, headers: Iterable[tuple[str, str]]) -> None:
self._num_plurals = int(params.get('nplurals', 2))
self._plural_expr = params.get('plural', '(n != 1)')
elif name == 'pot-creation-date':
self.creation_date = _parse_datetime_header(value)
# Some tools (e.g. Poedit) may leave this header blank or
# otherwise malformed; rather than crashing, just ignore it
# and keep the existing value in that case.
with suppress(ValueError):
self.creation_date = _parse_datetime_header(value)
elif name == 'po-revision-date':
# Keep the value if it's not the default one
if 'YEAR' not in value:
self.revision_date = _parse_datetime_header(value)
# Some tools (e.g. Poedit) may leave this header blank or
# otherwise malformed; rather than crashing, just ignore
# it and keep the existing value in that case.
with suppress(ValueError):
self.revision_date = _parse_datetime_header(value)

@property
def mime_headers(self) -> list[tuple[str, str]]:
Expand Down
16 changes: 16 additions & 0 deletions tests/messages/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,22 @@ def test_catalog_update_po_keeps_po_revision_date():
assert localized_catalog.revision_date == fake_rev_date


def test_catalog_set_mime_headers_ignores_blank_dates():
# Some tools (e.g. Poedit) can leave the PO-Revision-Date and/or
# POT-Creation-Date headers blank instead of omitting them or using the
# "YEAR-MO-DA HO:MI+ZONE" placeholder. This used to raise a ValueError
# instead of being handled gracefully.
cat = catalog.Catalog()
original_creation_date = cat.creation_date
original_revision_date = cat.revision_date
cat._set_mime_headers([
('POT-Creation-Date', ''),
('PO-Revision-Date', ''),
])
assert cat.creation_date == original_creation_date
assert cat.revision_date == original_revision_date


def test_catalog_stores_datetime_correctly():
localized = catalog.Catalog()
localized.locale = 'de_DE'
Expand Down