summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2003-06-15 22:05:58 (GMT)
committerTim Peters <tim.peters@gmail.com>2003-06-15 22:05:58 (GMT)
commit080da28bdbaf36677bf6a429f44d8f3a8eb44bf6 (patch)
tree5d1ad3bce499a26f86213019c3f2b99db382db1d
parentb1e63c0d05f1a2b137c37d51a24667637f7142ea (diff)
downloadcpython-080da28bdbaf36677bf6a429f44d8f3a8eb44bf6.zip
cpython-080da28bdbaf36677bf6a429f44d8f3a8eb44bf6.tar.gz
cpython-080da28bdbaf36677bf6a429f44d8f3a8eb44bf6.tar.bz2
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.
-rw-r--r--Lib/mimetools.py30
1 files changed, 22 insertions, 8 deletions
diff --git a/Lib/mimetools.py b/Lib/mimetools.py
index f1e20d4..67623ec 100644
--- a/Lib/mimetools.py
+++ b/Lib/mimetools.py
@@ -95,15 +95,31 @@ class Message(rfc822.Message):
# 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
def choose_boundary():
- """Return a random 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,
- so the caller needn't check the data it is packing for the
- occurrence of the boundary.
+ """Return a string usable as a multipart boundary.
+
+ The string chosen is unique within a single program run, and
+ incorporates the user id (if available), process id (if available),
+ 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."""
@@ -122,9 +138,7 @@ def choose_boundary():
except AttributeError:
pid = '1'
_prefix = hostid + '.' + uid + '.' + pid
- timestamp = '%.3f' % time.time()
- seed = `random.randint(0, 32767)`
- return _prefix + '.' + timestamp + '.' + seed
+ return "%s.%.3f.%d" % (_prefix, time.time(), _get_next_counter())
# Subroutines for decoding some common content-transfer-types