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
12 changes: 9 additions & 3 deletions babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,16 @@ 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.
# Keep the previous/default value rather than crashing.
if value.strip():
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:
# Keep the value if it's not the default one.
# Some tools (e.g. Poedit) may also leave this header
# blank; keep the previous/default value in that case
# too, rather than crashing.
if 'YEAR' not in value and value.strip():
self.revision_date = _parse_datetime_header(value)

mime_headers = property(
Expand Down
30 changes: 30 additions & 0 deletions tests/messages/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,36 @@ def test_catalog_stores_datetime_correctly():
assert value == '2009-03-09 15:47-0700'


def test_catalog_handles_blank_revision_and_creation_date():
"""Regression test for
https://github.com/python-babel/babel/issues/1219

Some tools (e.g. Poedit) can leave PO-Revision-Date and/or
POT-Creation-Date blank in the header. Parsing such a header must
not raise, and must not silently store an unparseable value that
would corrupt the header on write-out.
"""
cat = catalog.Catalog()
default_creation_date = cat.creation_date
default_revision_date = cat.revision_date

cat[''] = catalog.Message(
'',
"POT-Creation-Date: \n"
"PO-Revision-Date: \n",
)

# The blank headers should not have overwritten the defaults with
# an unparseable value (e.g. the raw empty string) or crashed.
assert cat.creation_date == default_creation_date
assert cat.revision_date == default_revision_date

for key, value in cat.mime_headers:
if key in ('POT-Creation-Date', 'PO-Revision-Date'):
assert value is not None
assert 'None' not in str(value)


def test_catalog_mime_headers_contain_same_information_as_attributes():
cat = catalog.Catalog()
cat[''] = catalog.Message('',
Expand Down