diff --git a/.gitignore b/.gitignore index 31cf48ed4..579989dcd 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ tests/messages/data/project/i18n/en_US tests/messages/data/project/i18n/fi_BUGGY/LC_MESSAGES/*.mo tests/messages/data/project/i18n/long_messages.pot tests/messages/data/project/i18n/temp* +.cie/ +.venv/ +.forge/ diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 5e6c28255..1c55487f4 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -103,8 +103,12 @@ def _has_python_brace_format(string: str) -> bool: return field_name_seen -def _parse_datetime_header(value: str) -> datetime.datetime: +def _parse_datetime_header(value: str) -> datetime.datetime | None: match = re.match(r'^(?P.*?)(?P[+-]\d{4})?$', value) + if not match.group('datetime'): + # Some tools (e.g. Poedit) emit a blank date header instead of + # eliding it altogether; leave the date unset in that case. + return None dt = datetime.datetime.strptime(match.group('datetime'), '%Y-%m-%d %H:%M') diff --git a/tests/test_forge_1219.py b/tests/test_forge_1219.py new file mode 100644 index 000000000..eed7be439 --- /dev/null +++ b/tests/test_forge_1219.py @@ -0,0 +1,34 @@ +"""Regression test for blank PO-Revision-Date header. + +When a .po file has a blank PO-Revision-Date field (as produced by some +tools like Poedit), pybabel raises a ValueError from +``_parse_datetime_header`` because it tries to parse an empty string. +The revision date should simply be left unset in that case. +""" + +import pytest + +from babel.messages.catalog import Catalog + + +def test_blank_po_revision_date_does_not_raise(): + """A blank PO-Revision-Date should not raise an exception.""" + catalog = Catalog() + # Simulate the headers that a .po file with a blank PO-Revision-Date + # would contain. + headers = [ + ("PO-Revision-Date", ""), + ] + # This should not raise; revision_date should remain None. + catalog._set_mime_headers(headers) + assert catalog.revision_date is None + + +def test_blank_pot_creation_date_does_not_raise(): + """A blank POT-Creation-Date should not raise an exception either.""" + catalog = Catalog() + headers = [ + ("POT-Creation-Date", ""), + ] + catalog._set_mime_headers(headers) + assert catalog.creation_date is None \ No newline at end of file