Skip to content

Commit e3e7851

Browse files
committed
Back port from 2.4 branch:
Patch #1464708 from William McVey: fixed handling of nested comments in mail addresses. E.g. "Foo ((Foo Bar)) <foo@example.com>" Fixes for both rfc822.py and email package.
1 parent dc74e34 commit e3e7851

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

Lib/email/_parseaddr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ def getdelimited(self, beginchar, endchars, allowcomments=True):
365365
break
366366
elif allowcomments and self.field[self.pos] == '(':
367367
slist.append(self.getcomment())
368+
continue # have already advanced pos from getcomment
368369
elif self.field[self.pos] == '\\':
369370
quote = True
370371
else:

Lib/email/test/test_email.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,12 @@ def test_getaddresses_nasty(self):
20642064
['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
20652065
[('', ''), ('Jason R. Mastaler', 'jason@dom.ain')])
20662066

2067+
def test_getaddresses_embedded_comment(self):
2068+
"""Test proper handling of a nested comment"""
2069+
eq = self.assertEqual
2070+
addrs = Utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
2071+
eq(addrs[0][1], 'foo@bar.com')
2072+
20672073
def test_utils_quote_unquote(self):
20682074
eq = self.assertEqual
20692075
msg = Message()

Lib/rfc822.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ def getdelimited(self, beginchar, endchars, allowcomments = 1):
712712
break
713713
elif allowcomments and self.field[self.pos] == '(':
714714
slist.append(self.getcomment())
715+
continue # have already advanced pos from getcomment
715716
elif self.field[self.pos] == '\\':
716717
quote = 1
717718
else:

Lib/test/test_rfc822.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def check(self, msg, results):
4545
print 'extra parsed address:', repr(n), repr(a)
4646
continue
4747
i = i + 1
48+
self.assertEqual(mn, n,
49+
"Un-expected name: %s != %s" % (`mn`, `n`))
50+
self.assertEqual(ma, a,
51+
"Un-expected address: %s != %s" % (`ma`, `a`))
4852
if mn == n and ma == a:
4953
pass
5054
else:
@@ -129,6 +133,12 @@ def test_basic(self):
129133
'To: person@dom.ain (User J. Person)\n\n',
130134
[('User J. Person', 'person@dom.ain')])
131135

136+
def test_doublecomment(self):
137+
# The RFC allows comments within comments in an email addr
138+
self.check(
139+
'To: person@dom.ain ((User J. Person)), John Doe <foo@bar.com>\n\n',
140+
[('User J. Person', 'person@dom.ain'), ('John Doe', 'foo@bar.com')])
141+
132142
def test_twisted(self):
133143
# This one is just twisted. I don't know what the proper
134144
# result should be, but it shouldn't be to infloop, which is

0 commit comments

Comments
 (0)