[Python-checkins] python/dist/src/Lib mimetools.py,1.27,1.28

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sun, 15 Jun 2003 15:06:00 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv9193/Lib

Modified Files:
	mimetools.py 
Log Message:
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.


Index: mimetools.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/mimetools.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** mimetools.py	9 Aug 2002 16:37:33 -0000	1.27
--- mimetools.py	15 Jun 2003 22:05:58 -0000	1.28
***************
*** 96,108 ****
  # -----------------
  
  
  _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.
  
      The boundary contains dots so you have to quote it in the header."""
--- 96,124 ----
  # -----------------
  
+ 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 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."""
***************
*** 123,129 ****
              pid = '1'
          _prefix = hostid + '.' + uid + '.' + pid
!     timestamp = '%.3f' % time.time()
!     seed = `random.randint(0, 32767)`
!     return _prefix + '.' + timestamp + '.' + seed
  
  
--- 139,143 ----
              pid = '1'
          _prefix = hostid + '.' + uid + '.' + pid
!     return "%s.%.3f.%d" % (_prefix, time.time(), _get_next_counter())