Skip to content

Ignore blank PO-Revision-Date/POT-Creation-Date header values - #1317

Open
agu2347 wants to merge 1 commit into
python-babel:masterfrom
agu2347:fix-blank-datetime-header
Open

Ignore blank PO-Revision-Date/POT-Creation-Date header values#1317
agu2347 wants to merge 1 commit into
python-babel:masterfrom
agu2347:fix-blank-datetime-header

Conversation

@agu2347

@agu2347 agu2347 commented Aug 16, 2026

Copy link
Copy Markdown

Summary

pybabel update/compile crashes with an unhandled ValueError when a .po file has a blank PO-Revision-Date (or POT-Creation-Date) header value, as generated by some tools (e.g. Poedit) instead of the conventional YEAR-MO-DA HO:MI+ZONE placeholder or omitting the header entirely.

File ".../babel/messages/catalog.py", line 109, in _parse_datetime_header
    dt = datetime.datetime.strptime(match.group('datetime'), '%Y-%m-%d %H:%M')
ValueError: time data '' does not match format '%Y-%m-%d %H:%M'

Root cause

_parse_datetime_header() unconditionally passes its input to datetime.strptime(). The po-revision-date call site already special-cases the literal 'YEAR-MO-DA HO:MI+ZONE' placeholder to avoid parsing it, but doesn't handle a blank value, and the pot-creation-date call site has no such guard at all.

Fix

_parse_datetime_header() now returns None for a blank (or whitespace-only) value instead of raising. Both call sites in Catalog._set_mime_headers() only assign creation_date/revision_date when the parse actually returns a value, so a blank header now falls back to the existing defaults instead of crashing -- the same behavior the 'YEAR-MO-DA HO:MI+ZONE' placeholder already gets.

Testing

  • Added test_datetime_parsing_blank_value (unit test for _parse_datetime_header) and test_set_mime_headers_ignores_blank_dates (integration test through Catalog._set_mime_headers) to tests/messages/test_catalog.py.
  • tests/messages/test_catalog.py: 46 passed.
  • tests/messages/ (full, with CLDR data built via scripts/download_import_cldr.py): 383 passed, 1 skipped (the 3 apparent failures without CLDR/cwd setup are pre-existing and unrelated -- they pass once run from the repo root).
  • Reverting only babel/messages/catalog.py (keeping the new tests) reproduces the exact reported error: ValueError: time data '' does not match format '%Y-%m-%d %H:%M'.
  • ruff check clean on both changed files.

Fixes #1219

_parse_datetime_header() unconditionally passed the header value to
datetime.strptime(), so a blank date (left empty by tools such as
Poedit instead of the conventional 'YEAR-MO-DA HO:MI+ZONE' placeholder
or omitting the header) raised an unhandled ValueError, crashing
`pybabel update`/`compile` while parsing an otherwise valid catalog.

Make _parse_datetime_header() return None for a blank (or
whitespace-only) value, and skip assigning creation_date/revision_date
in that case instead of raising, matching the existing handling of the
literal 'YEAR-MO-DA HO:MI+ZONE' placeholder for PO-Revision-Date.

Fixes python-babel#1219
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exception raised if the revision date is blank

1 participant