diff -r bb67b810aac1 Lib/email/utils.py --- a/Lib/email/utils.py Mon Feb 23 07:56:13 2015 -0800 +++ b/Lib/email/utils.py Tue Feb 24 21:08:20 2015 +0200 @@ -202,24 +202,23 @@ def format_datetime(dt, usegmt=False): def make_msgid(idstring=None, domain=None): """Returns a string suitable for RFC 2822 compliant Message-ID, e.g: - <20020201195627.33539.96671@nightshade.la.mastaler.com> + <142480216486.20800.16526388040877946887@nightshade.la.mastaler.com> Optional idstring if given is a string used to strengthen the uniqueness of the message id. Optional domain if given provides the portion of the message id after the '@'. It defaults to the locally defined hostname. """ - timeval = time.time() - utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval)) + timeval = int(time.time()*100) pid = os.getpid() - randint = random.randrange(100000) + randint = random.getrandbits(64) if idstring is None: idstring = '' else: idstring = '.' + idstring if domain is None: domain = socket.getfqdn() - msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, domain) + msgid = '<%d.%d.%d%s@%s>' % (timeval, pid, randint, idstring, domain) return msgid diff -r bb67b810aac1 Lib/test/test_email/test_email.py --- a/Lib/test/test_email/test_email.py Mon Feb 23 07:56:13 2015 -0800 +++ b/Lib/test/test_email/test_email.py Tue Feb 24 21:08:20 2015 +0200 @@ -11,6 +11,10 @@ import textwrap from io import StringIO, BytesIO from itertools import chain from random import choice +try: + from threading import Thread +except ImportError: + from dummy_threading import Thread import email import email.policy @@ -3156,6 +3160,29 @@ Foo addrs = utils.getaddresses(['User ((nested comment)) ']) eq(addrs[0][1], 'foo@bar.com') + def test_make_msgid_collisions(self): + # Test make_msgid uniqueness, even with multiple threads + + class MsgidsThread(Thread): + def run(self): + # generate msgids for 3 seconds + self.msgids = [] + append = self.msgids.append + make_msgid = utils.make_msgid + clock = time.clock + tfin = clock() + 3.0 + while clock() < tfin: + append(make_msgid()) + + eq = self.assertEqual + threads = [MsgidsThread() for i in range(5)] + for t in threads: + t.start() + for t in threads: + t.join() + all_ids = sum([t.msgids for t in threads], []) + eq(len(set(all_ids)), len(all_ids)) + def test_utils_quote_unquote(self): eq = self.assertEqual msg = Message()