diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 9a9739a72..e42d34c5b 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -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( diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py index 191a2a498..522d9a625 100644 --- a/tests/messages/test_catalog.py +++ b/tests/messages/test_catalog.py @@ -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('',