]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#7484: simplify quoteaddr: if parseaddr throws an error it is a bug.
authorR David Murray <rdmurray@bitdance.com>
Tue, 19 Jul 2011 01:59:53 +0000 (21:59 -0400)
committerR David Murray <rdmurray@bitdance.com>
Tue, 19 Jul 2011 01:59:53 +0000 (21:59 -0400)
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.

Lib/smtplib.py

index 55935d241eaa7898df5b5cc97fbd6c27610a8ea6..c7ff821bd67ed01c56205ffae19bbea4873ad4d0 100644 (file)
@@ -133,24 +133,18 @@ class SMTPAuthenticationError(SMTPResponseException):
     combination provided.
     """
 
-def quoteaddr(addr):
+def quoteaddr(addrstring):
     """Quote a subset of the email addresses defined by RFC 821.
 
     Should be able to handle anything email.utils.parseaddr can handle.
     """
-    m = (None, None)
-    try:
-        m = email.utils.parseaddr(addr)[1]
-    except AttributeError:
-        pass
-    if m == (None, None):  # Indicates parse failure or AttributeError
-        # something weird here.. punt -ddm
-        return "<%s>" % addr
-    elif m is None:
-        # the sender wants an empty return address
-        return "<>"
-    else:
-        return "<%s>" % m
+    displayname, addr = email.utils.parseaddr(addrstring)
+    if (displayname, addr) == ('', ''):
+        # parseaddr couldn't parse it, use it as is and hope for the best.
+        if addrstring.strip().startswith('<'):
+            return addrstring
+        return "<%s>" % addrstring
+    return "<%s>" % addr
 
 def _addr_only(addrstring):
     displayname, addr = email.utils.parseaddr(addrstring)