bpo-30681: Support invalid date format or value by timb07 · Pull Request #2254 · python/cpython

In email.utils.parsedate_to_datetime(), check failure to parse date, or invalid date components (such as hour outside 0..23), and return None rather than raising an exception.

Add tests to test_email/test_utils.py to confirm this behaviour.

In email.headerregistry.DateHeader.parse(), check when parsedate_to_datetime() returns None and add a defect; preserve the invalid value as the string value of the header, but set the datetime attribute to None.

Add tests to test_email/test_headerregistry.py to confirm this behaviour; also added test to test_email/test_inversion.py to confirm emails with such defective date headers round trip successfully.

This pull request incorporates feedback gratefully received from @bitdancer, @brettcannon and @Mariatta.

https://bugs.python.org/issue30681