[Python-checkins] cpython: #7484: simplify quoteaddr: if parseaddr throws an error it is a bug.

r.david.murray python-checkins at python.org
Tue Jul 19 04:00:09 CEST 2011


http://hg.python.org/cpython/rev/50b6c3053c30
changeset:   71420:50b6c3053c30
user:        R David Murray <rdmurray at bitdance.com>
date:        Mon Jul 18 21:59:53 2011 -0400
summary:
  #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.

files:
  Lib/smtplib.py |  22 ++++++++--------------
  1 files changed, 8 insertions(+), 14 deletions(-)


diff --git a/Lib/smtplib.py b/Lib/smtplib.py
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -133,24 +133,18 @@
     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)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list