python / python/cpython

Some edge cases in `email.utils.parsedate_to_datetime` seem to differ from RFC2822 spec

Đang mở
#126,845 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib topic-email type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

While tinkering around with email.utils.parsedate_to_datetime, I found some behavior that may be worth adjusting.

1. low-number years aren't handled according to spec:

The year is any numeric year 1900 or later. [section 3.3]

[section 4.3] The syntax for the obsolete date format allows a 2 digit year.
[..]
Where a two or three digit year occurs in a date, the year is to be
interpreted as follows: If a two digit year is encountered whose
value is between 00 and 49, the year is interpreted by adding 2000,
ending up with a value between 2000 and 2049. If a two digit year is
encountered with a value between 50 and 99, or any three digit year
is encountered, the year is interpreted by adding 1900.

>>> parsedate_to_datetime("Sat, 15 Aug 0001 23:12:09 +0500")
datetime.datetime(2001, 8, 15, 23, 12, 9, ...)

expected: either year 1, or a parsing failure. Neither the new or old format interpret 4-digit years this way.

2. offset minutes larger than 59 don't lead to parsing failure

>>> parsedate_to_datetime('Sat, 15 Aug 0001 23:12:09 +0590')
datetime.datetime(2001, 8, 15, 23, 12, 9, tzinfo=datetime.timezone(datetime.timedelta(seconds=23400)))

expected: parse failure. Instead, the "90 minutes" component is parsed without issue (0590 being equal to 0630). The spec is actually not explicit about this, although "A date-time specification MUST be semantically valid". Note that a "90" value as minute in the time component does give the appropriate parsing failure.

Note: datetime.fromisoformat() has the same behavior. Also in this case, I can't determine whether ISO8601 explicitly disallows it. RFC3339 is clear on disallowing this.

3. Invalid day-of-week doesn't lead to parsing failure

>>> parsedate_to_datetime('Sun, 15 Aug 0001 23:12:09 +0520')  # actually a saturday

expected: parsing failure

A date-time specification MUST be semantically valid. That is, the
day-of-the-week (if included) MUST be the day implied by the date,

4. Non-ASCII digits don't lead to parsing failure

If I'm reading the RFC correctly, only ASCII characters are valid.

>>> parsedate_to_datetime('Sat, 15 Aug 01 𝟚𝟛:𝟝𝟛:𝟛𝟛 +0500')  # note the fancy numbers
datetime.datetime(2001, 8, 15, 23, 53, 33, ...)

expected: parsing failure

5. Handling of the -0000 case may be inconsistent with drive to eliminate the practice of "naive UTC" datetimes.

Lately, the datetime module appears to discourage the usage of naive datetimes to mean UTC, as evidenced by the deprecation of utcnow() and other methods.

However, parsedate_to_datetime will return a naive datetime in the -0000 case.

>>> parsedate_to_datetime("Sat, 15 Aug 01 23:53:33 -0000")
datetime.datetime(2001, 8, 15, 23, 53, 33)

expected: tzinfo=UTC

The spec says:

"-0000" also indicates Universal Time, it is
used to indicate that the time was generated on a system that may be
in a local time zone other than Universal Time and therefore
indicates that the date-time contains no information about the local
time zone.

The spec again is a bit fuzzy, but my reading here is that -0000 means "UTC, with no offset known". In contrast, +0000 means "UTC offset known to be 0". My impression would be that only omission of the offset should result in a naive datetime. What do you think?

CPython versions tested on:

3.13

Operating systems tested on:

macOS

edit: typo

Linked PRs
  • gh-134311
  • gh-134350
  • gh-134438
  • gh-150864

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu từ email.utils.parsedate_to_datetime và so sánh cách xử lý năm ví dụ của nó với các yêu cầu RFC2822 được trích dẫn. Issue không nêu tệp hoặc test nào; để hoàn tất, cần thống nhất hành vi đối với việc phân tích năm, offset, thứ trong tuần, chữ số không phải ASCII và -0000, cùng với độ bao phủ cho các trường hợp được chấp nhận và bị từ chối.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.