diff -r 5e87dd117f74 Lib/smtplib.py --- a/Lib/smtplib.py Mon Apr 11 17:57:21 2011 -0700 +++ b/Lib/smtplib.py Wed Apr 13 15:22:11 2011 -0300 @@ -132,10 +132,9 @@ combination provided. """ -def quoteaddr(addr): - """Quote a subset of the email addresses defined by RFC 821. - - Should be able to handle anything email.utils.parseaddr can handle. +def _addrformat(addr, fmt="<%s>"): + """Extract a subset from the email addresses defined by RFC 821 + and return without according to specific format. """ m = (None, None) try: @@ -144,12 +143,25 @@ pass if m == (None, None): # Indicates parse failure or AttributeError # something weird here.. punt -ddm - return "<%s>" % addr + return fmt % addr elif m is None: # the sender wants an empty return address - return "<>" + return fmt % "" else: - return "<%s>" % m + return fmt % m + +def _addronly(addr): + """Extract a subset from the email addresses defined by RFC 821 + and return without pointed brackets. + """ + return _addrformat(addr, fmt="%s") + +def quoteaddr(addr): + """Quote a subset of the email addresses defined by RFC 821. + + Should be able to handle anything rfc822.parseaddr can handle. + """ + return _addrformat(addr) # Legacy method kept for backward compatibility. def quotedata(data): @@ -526,14 +538,14 @@ def verify(self, address): """SMTP 'verify' command -- checks for address validity.""" - self.putcmd("vrfy", quoteaddr(address)) + self.putcmd("vrfy", _addronly(address)) return self.getreply() # a.k.a. vrfy = verify def expn(self, address): """SMTP 'expn' command -- expands a mailing list.""" - self.putcmd("expn", quoteaddr(address)) + self.putcmd("expn", _addronly(address)) return self.getreply() # some useful methods diff -r 5e87dd117f74 Lib/test/test_smtplib.py --- a/Lib/test/test_smtplib.py Mon Apr 11 17:57:21 2011 -0700 +++ b/Lib/test/test_smtplib.py Wed Apr 13 15:22:11 2011 -0300 @@ -114,6 +114,27 @@ self.assertEqual(smtp.sock.gettimeout(), 30) smtp.close() + def testQuoteaddr(self): + addr = 'some@email.org' + addr_expected = '' + quoted_addr = smtplib.quoteaddr(addr) + self.assertEquals(addr_expected, quoted_addr) + + addr = 'Someone ' + addr_expected = '' + quoted_addr = smtplib.quoteaddr(addr) + self.assertEquals(addr_expected, quoted_addr) + + def testAddronly(self): + addr = 'some@email.org' + addr_expected = addr + quoted_addr = smtplib._addronly(addr) + self.assertEquals(addr_expected, quoted_addr) + + addr = 'Someone ' + addr_expected = 'some@email.org' + quoted_addr = smtplib._addronly(addr) + self.assertEquals(addr_expected, quoted_addr) # Test server thread using the specified SMTP server class def debugging_server(serv, serv_evt, client_evt): @@ -443,7 +464,7 @@ def smtp_VRFY(self, arg): raw_addr = email.utils.parseaddr(arg)[1] - quoted_addr = smtplib.quoteaddr(arg) + quoted_addr = smtplib._addronly(arg) if raw_addr in sim_users: self.push('250 %s %s' % (sim_users[raw_addr], quoted_addr)) else: @@ -454,7 +475,7 @@ if list_name in sim_lists: user_list = sim_lists[list_name] for n, user_email in enumerate(user_list): - quoted_addr = smtplib.quoteaddr(user_email) + quoted_addr = smtplib._addronly(user_email) if n < len(user_list) - 1: self.push('250-%s %s' % (sim_users[user_email], quoted_addr)) else: @@ -577,13 +598,13 @@ for email, name in sim_users.items(): expected_known = (250, bytes('%s %s' % - (name, smtplib.quoteaddr(email)), + (name, smtplib._addronly(email)), "ascii")) self.assertEqual(smtp.vrfy(email), expected_known) u = 'nobody@nowhere.com' expected_unknown = (550, ('No such user: %s' - % smtplib.quoteaddr(u)).encode('ascii')) + % smtplib._addronly(u)).encode('ascii')) self.assertEqual(smtp.vrfy(u), expected_unknown) smtp.quit() @@ -593,7 +614,7 @@ for listname, members in sim_lists.items(): users = [] for m in members: - users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m))) + users.append('%s %s' % (sim_users[m], smtplib._addronly(m))) expected_known = (250, bytes('\n'.join(users), "ascii")) self.assertEqual(smtp.expn(listname), expected_known)