diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 145954a64..21af7dfe1 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -592,10 +592,13 @@ 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) + # Keep last value instead of crashing on blank values + 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: + # and don't crash trying to parse blank values + if 'YEAR' not in value and value.strip(): self.revision_date = _parse_datetime_header(value) @property diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py index bf41c1976..caa7562c8 100644 --- a/tests/messages/test_catalog.py +++ b/tests/messages/test_catalog.py @@ -343,6 +343,27 @@ def test_catalog_stores_datetime_correctly(): assert value == '2009-03-09 15:47-0700' +def test_catalog_handles_blank_revision_and_creation_date(): + 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", + ) + + # We didn't crash and we retained the original values + 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('',