X Tutup
Skip to content

Commit 4c14bba

Browse files
committed
#7484: simplify quoteaddr: if parseaddr throws an error it is a bug.
As far as I can tell, the try/except was ancient code, from before the email package rewrite where the philosophy of never throwing parsing errors was adopted.
1 parent da7af4d commit 4c14bba

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

Lib/smtplib.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,18 @@ class SMTPAuthenticationError(SMTPResponseException):
133133
combination provided.
134134
"""
135135

136-
def quoteaddr(addr):
136+
def quoteaddr(addrstring):
137137
"""Quote a subset of the email addresses defined by RFC 821.
138138
139139
Should be able to handle anything email.utils.parseaddr can handle.
140140
"""
141-
m = (None, None)
142-
try:
143-
m = email.utils.parseaddr(addr)[1]
144-
except AttributeError:
145-
pass
146-
if m == (None, None): # Indicates parse failure or AttributeError
147-
# something weird here.. punt -ddm
148-
return "<%s>" % addr
149-
elif m is None:
150-
# the sender wants an empty return address
151-
return "<>"
152-
else:
153-
return "<%s>" % m
141+
displayname, addr = email.utils.parseaddr(addrstring)
142+
if (displayname, addr) == ('', ''):
143+
# parseaddr couldn't parse it, use it as is and hope for the best.
144+
if addrstring.strip().startswith('<'):
145+
return addrstring
146+
return "<%s>" % addrstring
147+
return "<%s>" % addr
154148

155149
def _addr_only(addrstring):
156150
displayname, addr = email.utils.parseaddr(addrstring)

0 commit comments

Comments
 (0)
X Tutup