diff -r 6446fc0a0a99 -r 88b5c7ab7a03 Lib/smtplib.py --- a/Lib/smtplib.py Sat Jul 16 23:56:45 2011 +0200 +++ b/Lib/smtplib.py Sun Jul 17 14:31:06 2011 +0200 @@ -133,10 +133,8 @@ 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): + """Helper that parses an address and returns it formatted according to fmt. """ m = (None, None) try: @@ -145,12 +143,22 @@ 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): + 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 email.utils.parseaddr can handle. + """ + return _addrformat(addr, fmt='<%s>') # Legacy method kept for backward compatibility. def quotedata(data): @@ -507,14 +515,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 6446fc0a0a99 -r 88b5c7ab7a03 Lib/test/test_smtplib.py --- a/Lib/test/test_smtplib.py Sat Jul 16 23:56:45 2011 +0200 +++ b/Lib/test/test_smtplib.py Sun Jul 17 14:31:06 2011 +0200 @@ -114,6 +114,28 @@ 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): @@ -565,13 +587,13 @@ def smtp_VRFY(self, arg): raw_addr = email.utils.parseaddr(arg)[1] quoted_addr = smtplib.quoteaddr(arg) - if raw_addr in sim_users: + if arg in sim_users: self.push('250 %s %s' % (sim_users[raw_addr], quoted_addr)) else: self.push('550 No such user: %s' % arg) def smtp_EXPN(self, arg): - list_name = email.utils.parseaddr(arg)[1].lower() + list_name = arg.lower() if list_name in sim_lists: user_list = sim_lists[list_name] for n, user_email in enumerate(user_list): @@ -703,8 +725,7 @@ self.assertEqual(smtp.vrfy(email), expected_known) u = 'nobody@nowhere.com' - expected_unknown = (550, ('No such user: %s' - % smtplib.quoteaddr(u)).encode('ascii')) + expected_unknown = (550, ('No such user: %s' % u).encode('ascii')) self.assertEqual(smtp.vrfy(u), expected_unknown) smtp.quit()