Skip to content
Closed
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
6 changes: 5 additions & 1 deletion babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<datetime>.*?)(?P<tzoffset>[+-]\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')

Expand Down
34 changes: 34 additions & 0 deletions tests/test_forge_1219.py
Original file line number Diff line number Diff line change
@@ -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