choose_boundary(): Incorporated a threadsafe incrementing counter, so that

unique boundary strings within a program run are guaranteed.  On Windows,
duplicates were pretty likely, due to the coarse granularity of time.time.
Toned down the absurdly optimistic claims in the docstring.

Bugfix candidate.
This commit is contained in:
Tim Peters 2003-06-15 22:05:58 +00:00
parent b1e63c0d05
commit 080da28bdb

View file

@ -95,15 +95,31 @@ class Message(rfc822.Message):
# Utility functions # Utility functions
# ----------------- # -----------------
try:
import thread
except ImportError:
import dummy_thread as thread
_counter_lock = thread.allocate_lock()
del thread
_counter = 0
def _get_next_counter():
global _counter
_counter_lock.acquire()
_counter += 1
result = _counter
_counter_lock.release()
return result
_prefix = None _prefix = None
def choose_boundary(): def choose_boundary():
"""Return a random string usable as a multipart boundary. """Return a string usable as a multipart boundary.
The method used is so that it is *very* unlikely that the same
string of characters will every occur again in the Universe, The string chosen is unique within a single program run, and
so the caller needn't check the data it is packing for the incorporates the user id (if available), process id (if available),
occurrence of the boundary. and current time. So it's very unlikely the returned string appears
in message text, but there's no guarantee.
The boundary contains dots so you have to quote it in the header.""" The boundary contains dots so you have to quote it in the header."""
@ -122,9 +138,7 @@ def choose_boundary():
except AttributeError: except AttributeError:
pid = '1' pid = '1'
_prefix = hostid + '.' + uid + '.' + pid _prefix = hostid + '.' + uid + '.' + pid
timestamp = '%.3f' % time.time() return "%s.%.3f.%d" % (_prefix, time.time(), _get_next_counter())
seed = `random.randint(0, 32767)`
return _prefix + '.' + timestamp + '.' + seed
# Subroutines for decoding some common content-transfer-types # Subroutines for decoding some common content-transfer-types