[Python-checkins] CVS: python/dist/src/Lib tempfile.py,1.22,1.23

Tim Peters python-dev@python.org
Fri, 12 Jan 2001 02:02:50 -0800


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

Modified Files:
	tempfile.py 
Log Message:
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tested on Windows.  Should be tested on Linux.  Should also be
tested on some platform without threads (I simulated that by
making the "import thread" fail, but that's not the same as
actually doing it!).


Index: tempfile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/tempfile.py,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -r1.22 -r1.23
*** tempfile.py	2000/08/29 14:55:03	1.22
--- tempfile.py	2001/01/12 10:02:46	1.23
***************
*** 5,18 ****
  # systems so it may have to be changed...
  
- 
  import os
  
- 
  # Parameters that the caller may set to override the defaults
- 
  tempdir = None
  template = None
  
- 
  def gettempdir():
      """Function to calculate the directory to use."""
--- 5,14 ----
***************
*** 93,109 ****
  
  
- # Counter for generating unique names
- 
- counter = 0
- 
- 
  def mktemp(suffix=""):
      """User-callable function to return a unique temporary file name."""
-     global counter
      dir = gettempdir()
      pre = gettempprefix()
      while 1:
!         counter = counter + 1
!         file = os.path.join(dir, pre + `counter` + suffix)
          if not os.path.exists(file):
              return file
--- 89,99 ----
  
  
  def mktemp(suffix=""):
      """User-callable function to return a unique temporary file name."""
      dir = gettempdir()
      pre = gettempprefix()
      while 1:
!         i = _counter.get_next()
!         file = os.path.join(dir, pre + str(i) + suffix)
          if not os.path.exists(file):
              return file
***************
*** 153,154 ****
--- 143,187 ----
          file = open(name, mode, bufsize)
          return TemporaryFileWrapper(file, name)
+ 
+ # In order to generate unique names, mktemp() uses _counter.get_next().
+ # This returns a unique integer on each call, in a threadsafe way (i.e.,
+ # multiple threads will never see the same integer).  The integer will
+ # usually be a Python int, but if _counter.get_next() is called often
+ # enough, it will become a Python long.
+ # Note that the only name that survives this next block of code
+ # is "_counter".
+ 
+ class _ThreadSafeCounter:
+     def __init__(self, mutex, initialvalue=0):
+         self.mutex = mutex
+         self.i = initialvalue
+ 
+     def get_next(self):
+         self.mutex.acquire()
+         result = self.i
+         try:
+             newi = result + 1
+         except OverflowError:
+             newi = long(result) + 1
+         self.i = newi
+         self.mutex.release()
+         return result
+ 
+ try:
+     import thread
+ 
+ except ImportError:
+     class _DummyMutex:
+         def acquire(self):
+             pass
+ 
+         release = acquire
+ 
+     _counter = _ThreadSafeCounter(_DummyMutex())
+     del _DummyMutex
+ 
+ else:
+     _counter = _ThreadSafeCounter(thread.allocate_lock())
+     del thread
+ 
+ del _ThreadSafeCounter