Skip to content

Commit 44bd6ca

Browse files
committed
gh-148797: Add ReferencesHeader.message_ids
1 parent 49a344e commit 44bd6ca

5 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎Doc/library/email.headerregistry.rst‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ variant, :attr:`~.BaseHeader.max_count` is set to 1.
306306
``quoted-printable``. See :rfc:`2045` for more information.
307307

308308

309+
.. class:: ReferencesHeader
310+
311+
Handles the :mailheader:`In-Reply-To` and :mailheader:`References` headers.
312+
313+
.. attribute:: message_ids
314+
315+
A tuple of the message identifiers in the header, in order. Each one is
316+
in its ``<id-left@id-right>`` form, without the comments and folding
317+
white space that may surround it in the header, so it can be compared
318+
with the value of another message's :mailheader:`Message-ID` header or
319+
with the result of :func:`email.utils.make_msgid`. Identifiers that
320+
cannot be parsed are left out and reported in :attr:`~BaseHeader.defects`.
321+
322+
.. versionadded:: next
323+
324+
309325

310326
.. class:: HeaderRegistry(base_class=BaseHeader, \
311327
default_class=UnstructuredHeader, \
@@ -343,6 +359,8 @@ variant, :attr:`~.BaseHeader.max_count` is set to 1.
343359
:content-disposition: ContentDispositionHeader
344360
:content-transfer-encoding: ContentTransferEncodingHeader
345361
:message-id: MessageIDHeader
362+
:in-reply-to: ReferencesHeader
363+
:references: ReferencesHeader
346364

347365
``HeaderRegistry`` has the following methods:
348366

‎Doc/whatsnew/3.16.rst‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,16 @@ difflib
499499
(Contributed by Tomasz Kazimierczak in :gh:`118150`.)
500500

501501

502+
email
503+
-----
504+
505+
* :class:`email.headerregistry.ReferencesHeader`, used for the
506+
:mailheader:`In-Reply-To` and :mailheader:`References` headers, has a new
507+
:attr:`~email.headerregistry.ReferencesHeader.message_ids` attribute: a tuple
508+
of the message identifiers the header holds.
509+
(Contributed by Sudeep Gowda in :gh:`148797`.)
510+
511+
502512
encodings
503513
---------
504514

‎Lib/email/headerregistry.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,17 @@ def parse(cls, value, kwds):
543543
kwds['decoded'] = str(parse_tree)
544544
kwds['defects'].extend(parse_tree.all_defects)
545545

546+
def init(self, *args, **kw):
547+
super().init(*args, **kw)
548+
# Comments and folding whitespace around a msg-id are not part of it.
549+
self._message_ids = tuple(
550+
''.join(str(token) for token in msg_id if token.token_type != 'cfws')
551+
for msg_id in self._parse_tree.message_ids)
552+
553+
@property
554+
def message_ids(self):
555+
return self._message_ids
556+
546557

547558
# The header factory #
548559

‎Lib/test/test_email/test_headerregistry.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import unittest
44
from email import errors
55
from email import policy
6+
from email import utils
7+
from email import message_from_string
68
from email.message import Message
79
from test.test_email import TestEmailBase, parameterize
810
from email import headerregistry
@@ -1688,6 +1690,59 @@ def test_group_comparison(self):
16881690
self.assertTrue(g == ALWAYS_EQ)
16891691

16901692

1693+
class TestReferencesHeader(TestHeaderBase):
1694+
1695+
def test_references_message_ids(self):
1696+
h = self.make_header('References',
1697+
'<a@example.com> (the first) <b@example.com>')
1698+
self.assertEqual(h.message_ids, ('<a@example.com>', '<b@example.com>'))
1699+
self.assertDefectsEqual(h.defects, [])
1700+
1701+
def test_in_reply_to_message_ids(self):
1702+
h = self.make_header('In-Reply-To', ' <a@example.com> ')
1703+
self.assertEqual(h.message_ids, ('<a@example.com>',))
1704+
1705+
def test_message_ids_match_make_msgid(self):
1706+
msgid = utils.make_msgid()
1707+
h = self.make_header('In-Reply-To', msgid)
1708+
self.assertEqual(h.message_ids, (msgid,))
1709+
1710+
def test_message_ids_id_right_forms(self):
1711+
h = self.make_header('References',
1712+
'<a@[127.0.0.1]> <b@xn--fa-hia.example>')
1713+
self.assertEqual(h.message_ids,
1714+
('<a@[127.0.0.1]>', '<b@xn--fa-hia.example>'))
1715+
1716+
def test_message_ids_comma_separated(self):
1717+
h = self.make_header('References', '<a@example.com>,<b@example.com>')
1718+
self.assertEqual(h.message_ids, ('<a@example.com>', '<b@example.com>'))
1719+
self.assertDefectsEqual(h.defects, [errors.InvalidHeaderDefect])
1720+
1721+
def test_message_ids_skip_invalid(self):
1722+
h = self.make_header('References', '<a@example.com> not-a-msg-id')
1723+
self.assertEqual(h.message_ids, ('<a@example.com>',))
1724+
self.assertDefectsEqual(h.defects, [errors.InvalidHeaderDefect])
1725+
1726+
def test_message_ids_from_parsed_message(self):
1727+
msg = message_from_string(textwrap.dedent('''\
1728+
In-Reply-To: <b@example.com>
1729+
References: <a@example.com>
1730+
<b@example.com>
1731+
1732+
body
1733+
'''), policy=policy.default)
1734+
[in_reply_to] = msg['In-Reply-To'].message_ids
1735+
*_, last_reference = msg['References'].message_ids
1736+
self.assertEqual(in_reply_to, last_reference)
1737+
self.assertEqual(msg['References'].message_ids,
1738+
('<a@example.com>', '<b@example.com>'))
1739+
1740+
def test_message_ids_read_only(self):
1741+
h = self.make_header('References', '<a@example.com>')
1742+
with self.assertRaises(AttributeError):
1743+
h.message_ids = ()
1744+
1745+
16911746
class TestFolding(TestHeaderBase):
16921747

16931748
def test_address_display_names(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :attr:`email.headerregistry.ReferencesHeader.message_ids`, a tuple of the
2+
message identifiers in an :mailheader:`In-Reply-To` or :mailheader:`References`
3+
header.

0 commit comments

Comments
 (0)