Made utils.file.mktemp a little more stochastic.

This commit is contained in:
Jeremy Fincher 2005-02-18 07:04:48 +00:00
parent 8756b79cc4
commit 6aca01bafb
2 changed files with 13 additions and 3 deletions

View File

@ -42,10 +42,14 @@ def mktemp(suffix=''):
m = md5.md5(suffix)
r.seed(time.time())
s = str(r.getstate())
for x in xrange(0, random.randrange(400), random.randrange(1, 5)):
m.update(str(x))
period = random.random()
now = start = time.time()
while start + period < now:
time.sleep() # Induce a context switch, if possible.
now = time.time()
m.update(str(random.random()))
m.update(s)
m.update(str(time.time()))
m.update(str(now))
s = m.hexdigest()
return sha.sha(s + str(time.time())).hexdigest() + suffix

View File

@ -458,6 +458,12 @@ class FileTest(SupyTestCase):
self.assertEqual(list(utils.file.nonCommentNonEmptyLines(L)),
['foo', 'bar', 'biff'])
def testMktemp(self):
# This is mostly to test that it actually halts.
self.failUnless(utils.file.mktemp())
self.failUnless(utils.file.mktemp())
self.failUnless(utils.file.mktemp())
class NetTest(SupyTestCase):
def testEmailRe(self):