liblore 0.10.1__tar.gz → 0.10.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. {liblore-0.10.1/src/liblore.egg-info → liblore-0.10.2}/PKG-INFO +1 -1
  2. {liblore-0.10.1 → liblore-0.10.2}/pyproject.toml +1 -1
  3. {liblore-0.10.1 → liblore-0.10.2}/src/liblore/__init__.py +1 -1
  4. {liblore-0.10.1 → liblore-0.10.2}/src/liblore/series.py +1 -3
  5. {liblore-0.10.1 → liblore-0.10.2}/src/liblore/utils.py +35 -11
  6. {liblore-0.10.1 → liblore-0.10.2/src/liblore.egg-info}/PKG-INFO +1 -1
  7. {liblore-0.10.1 → liblore-0.10.2}/tests/test_email_utils.py +87 -9
  8. {liblore-0.10.1 → liblore-0.10.2}/LICENSES/GPL-2.0-or-later.txt +0 -0
  9. {liblore-0.10.1 → liblore-0.10.2}/README.md +0 -0
  10. {liblore-0.10.1 → liblore-0.10.2}/setup.cfg +0 -0
  11. {liblore-0.10.1 → liblore-0.10.2}/src/liblore/node.py +0 -0
  12. {liblore-0.10.1 → liblore-0.10.2}/src/liblore/py.typed +0 -0
  13. {liblore-0.10.1 → liblore-0.10.2}/src/liblore.egg-info/SOURCES.txt +0 -0
  14. {liblore-0.10.1 → liblore-0.10.2}/src/liblore.egg-info/dependency_links.txt +0 -0
  15. {liblore-0.10.1 → liblore-0.10.2}/src/liblore.egg-info/requires.txt +0 -0
  16. {liblore-0.10.1 → liblore-0.10.2}/src/liblore.egg-info/top_level.txt +0 -0
  17. {liblore-0.10.1 → liblore-0.10.2}/tests/test_auth_headers.py +0 -0
  18. {liblore-0.10.1 → liblore-0.10.2}/tests/test_cache.py +0 -0
  19. {liblore-0.10.1 → liblore-0.10.2}/tests/test_formatting.py +0 -0
  20. {liblore-0.10.1 → liblore-0.10.2}/tests/test_mbox.py +0 -0
  21. {liblore-0.10.1 → liblore-0.10.2}/tests/test_message.py +0 -0
  22. {liblore-0.10.1 → liblore-0.10.2}/tests/test_node.py +0 -0
  23. {liblore-0.10.1 → liblore-0.10.2}/tests/test_partial.py +0 -0
  24. {liblore-0.10.1 → liblore-0.10.2}/tests/test_series.py +0 -0
  25. {liblore-0.10.1 → liblore-0.10.2}/tests/test_thread.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: liblore
3
- Version: 0.10.1
3
+ Version: 0.10.2
4
4
  Summary: Shared library for public-inbox / lore.kernel.org access
5
5
  Author-email: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
6
6
  License-Expression: GPL-2.0-or-later
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
5
5
 
6
6
  [project]
7
7
  name = "liblore"
8
- version = "0.10.1"
8
+ version = "0.10.2"
9
9
  description = "Shared library for public-inbox / lore.kernel.org access"
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.9"
@@ -9,7 +9,7 @@ import email.policy
9
9
  import enum
10
10
  from email.message import EmailMessage
11
11
 
12
- __version__ = '0.10.1'
12
+ __version__ = '0.10.2'
13
13
 
14
14
  # Email policy used for parsing and serialising messages
15
15
  emlpolicy = email.policy.EmailPolicy(
@@ -849,11 +849,9 @@ def _first_date(msgs: Sequence[EmailMessage]) -> datetime.datetime | None:
849
849
 
850
850
 
851
851
  def _sort_key(when: datetime.datetime | None) -> datetime.datetime:
852
- """Sort undated items last, and make naive/aware datetimes comparable."""
852
+ """Sort undated items last."""
853
853
  if when is None:
854
854
  return datetime.datetime.max.replace(tzinfo=datetime.timezone.utc)
855
- if when.tzinfo is None:
856
- return when.replace(tzinfo=datetime.timezone.utc)
857
855
  return when
858
856
 
859
857
 
@@ -629,13 +629,37 @@ def msg_get_recipients(msg: EmailMessage) -> set[str]:
629
629
  return {x[1].lower() for x in pairs}
630
630
 
631
631
 
632
+ def _parse_utc_date(value: str) -> datetime.datetime | None:
633
+ """Parse an RFC 5322 date into an aware UTC datetime, or ``None``.
634
+
635
+ ``parsedate_to_datetime()`` returns a naive value for ``-0000``, which
636
+ RFC 5322 defines as UTC with an unknown local zone, so reading it as
637
+ UTC is correct. It does the same when the zone is missing or not
638
+ recognised, and UTC is the only neutral reading there too. Converting
639
+ everything to UTC gives one canonical form that always compares and
640
+ serialises consistently.
641
+ """
642
+ try:
643
+ parsed = email.utils.parsedate_to_datetime(value)
644
+ if parsed.tzinfo is None:
645
+ return parsed.replace(tzinfo=datetime.timezone.utc)
646
+ # OverflowError: a date at the edge of year 9999 can parse and
647
+ # still have no representable UTC equivalent.
648
+ return parsed.astimezone(datetime.timezone.utc)
649
+ except (ValueError, TypeError, OverflowError):
650
+ return None
651
+
652
+
632
653
  def msg_get_date(msg: EmailMessage) -> datetime.datetime | None:
633
654
  """Return the message timestamp, preferring Received over Date.
634
655
 
635
656
  Uses the latest ``Received`` header timestamp when one is present,
636
657
  because those are stamped by the receiving servers rather than by
637
658
  the sender, and falls back to the client-set ``Date`` header.
638
- Returns ``None`` when neither yields a parsable timestamp.
659
+
660
+ Returns an aware datetime in UTC, or ``None`` when neither header
661
+ yields a parsable timestamp. Timestamps marked ``-0000``, or with no
662
+ usable zone, are treated as UTC.
639
663
  """
640
664
  latest_rdt: datetime.datetime | None = None
641
665
  for rhdr in msg.get_all('Received', []):
@@ -643,18 +667,18 @@ def msg_get_date(msg: EmailMessage) -> datetime.datetime | None:
643
667
  chunks = str(rhdr).rsplit(';', maxsplit=1)
644
668
  if len(chunks) < 2:
645
669
  continue
646
- rdate = chunks[1].strip()
647
- try:
648
- rdt = email.utils.parsedate_to_datetime(rdate)
649
- except (ValueError, TypeError):
650
- continue
651
- if not latest_rdt or latest_rdt < rdt:
670
+ rdt = _parse_utc_date(chunks[1].strip())
671
+ if rdt is not None and (latest_rdt is None or latest_rdt < rdt):
652
672
  latest_rdt = rdt
653
- if not latest_rdt and msg.get('Date'):
673
+ if latest_rdt is None:
654
674
  try:
655
- latest_rdt = email.utils.parsedate_to_datetime(str(msg.get('Date')))
656
- except (ValueError, TypeError):
657
- pass
675
+ date_hdr = msg.get('Date')
676
+ except TypeError:
677
+ # Python 3.9's DateHeader raises on a garbage value when the
678
+ # header is read; newer versions record a defect instead.
679
+ return None
680
+ if date_hdr:
681
+ latest_rdt = _parse_utc_date(str(date_hdr))
658
682
  return latest_rdt
659
683
 
660
684
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: liblore
3
- Version: 0.10.1
3
+ Version: 0.10.2
4
4
  Summary: Shared library for public-inbox / lore.kernel.org access
5
5
  Author-email: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
6
6
  License-Expression: GPL-2.0-or-later
@@ -3,6 +3,8 @@
3
3
 
4
4
  from __future__ import annotations
5
5
 
6
+ import datetime
7
+ import email
6
8
  from email.message import EmailMessage
7
9
 
8
10
  import pytest
@@ -11,6 +13,7 @@ from conftest import MsgFactory
11
13
  from liblore import emlpolicy
12
14
  from liblore.utils import (
13
15
  msg_get_author,
16
+ msg_get_date,
14
17
  msg_get_payload,
15
18
  msg_get_recipients,
16
19
  msg_get_subject,
@@ -18,6 +21,8 @@ from liblore.utils import (
18
21
  strip_reply_prefixes,
19
22
  )
20
23
 
24
+ UTC = datetime.timezone.utc
25
+
21
26
 
22
27
  class TestMsgGetSubject:
23
28
  def test_plain_subject(self, make_msg: MsgFactory) -> None:
@@ -179,15 +184,19 @@ class TestMsgGetRecipients:
179
184
 
180
185
 
181
186
  class TestSortMsgsByReceived:
182
- def test_sorts_by_date(self, make_msg: MsgFactory) -> None:
183
- msg1 = make_msg(
184
- msgid='older@x.com',
185
- date='Mon, 01 Jan 2024 00:00:00 +0000',
186
- )
187
- msg2 = make_msg(
188
- msgid='newer@x.com',
189
- date='Tue, 02 Jan 2024 00:00:00 +0000',
190
- )
187
+ @pytest.mark.parametrize(
188
+ ('older', 'newer'),
189
+ [
190
+ ('Mon, 01 Jan 2024 00:00:00 +0000', 'Tue, 02 Jan 2024 00:00:00 +0000'),
191
+ # A -0000 (naive) date must sort against an aware one rather
192
+ # than raising TypeError.
193
+ ('Mon, 02 Nov 2026 10:00:00 -0000', 'Mon, 02 Nov 2026 10:00:01 +0000'),
194
+ ('Mon, 02 Nov 2026 10:00:00 +0000', 'Mon, 02 Nov 2026 10:00:01 -0000'),
195
+ ],
196
+ )
197
+ def test_sorts_by_date(self, make_msg: MsgFactory, older: str, newer: str) -> None:
198
+ msg1 = make_msg(msgid='older@x.com', date=older)
199
+ msg2 = make_msg(msgid='newer@x.com', date=newer)
191
200
  # Feed them in reverse order
192
201
  result = sort_msgs_by_received([msg2, msg1])
193
202
  assert result[0]['Message-Id'] == '<older@x.com>'
@@ -200,3 +209,72 @@ class TestSortMsgsByReceived:
200
209
  del msg['Date']
201
210
  result = sort_msgs_by_received([msg])
202
211
  assert result == []
212
+
213
+
214
+ class TestMsgGetDate:
215
+ """msg_get_date() must always return an aware UTC datetime or None.
216
+
217
+ Callers sort, compare and convert the result to timestamps; a naive
218
+ value would crash comparisons and be read as local time.
219
+ """
220
+
221
+ @pytest.mark.parametrize(
222
+ ('date', 'received', 'expected'),
223
+ [
224
+ # RFC 5322: -0000 means UTC with an unknown local zone.
225
+ pytest.param(
226
+ 'Mon, 2 Nov 2026 10:00:00 -0000',
227
+ [],
228
+ datetime.datetime(2026, 11, 2, 10, 0, tzinfo=UTC),
229
+ id='date-minus0000',
230
+ ),
231
+ pytest.param(
232
+ 'Mon, 2 Nov 2026 12:00:00 +0200',
233
+ [],
234
+ datetime.datetime(2026, 11, 2, 10, 0, tzinfo=UTC),
235
+ id='date-offset',
236
+ ),
237
+ pytest.param(
238
+ 'Mon, 2 Nov 2026 08:00:00 +0000',
239
+ [
240
+ 'from a by b; Mon, 2 Nov 2026 10:00:00 -0000',
241
+ 'from c by a; Mon, 2 Nov 2026 09:59:00 +0000',
242
+ ],
243
+ datetime.datetime(2026, 11, 2, 10, 0, tzinfo=UTC),
244
+ id='received-mixed-latest-wins',
245
+ ),
246
+ pytest.param(
247
+ 'Mon, 2 Nov 2026 08:00:00 +0000',
248
+ ['from c by a; Mon, 2 Nov 2026 11:30:00 +0100'],
249
+ datetime.datetime(2026, 11, 2, 10, 30, tzinfo=UTC),
250
+ id='received-offset',
251
+ ),
252
+ pytest.param(None, [], None, id='no-headers'),
253
+ pytest.param('not a date', ['from x by y; garbage'], None, id='garbage'),
254
+ # Past year 9999 in UTC, so no datetime can hold it.
255
+ pytest.param(
256
+ 'Fri, 31 Dec 9999 23:59:59 -0500',
257
+ ['from x by y; Fri, 31 Dec 9999 23:59:59 -0500'],
258
+ None,
259
+ id='out-of-range',
260
+ ),
261
+ ],
262
+ )
263
+ def test_returns_utc(
264
+ self,
265
+ date: str | None,
266
+ received: list[str],
267
+ expected: datetime.datetime | None,
268
+ ) -> None:
269
+ # Parse from bytes, as real mail arrives: building an EmailMessage
270
+ # directly makes Python 3.9 reject a garbage Date before liblore
271
+ # ever sees it.
272
+ hdrs = [f'Received: {hdr}' for hdr in received]
273
+ if date is not None:
274
+ hdrs.append(f'Date: {date}')
275
+ raw = '\n'.join([*hdrs, 'Subject: x', '', 'body', ''])
276
+ msg = email.message_from_bytes(raw.encode(), policy=emlpolicy)
277
+ result = msg_get_date(msg)
278
+ assert result == expected
279
+ if result is not None:
280
+ assert result.tzinfo is UTC
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes