[Python-checkins] python/dist/src/Lib tempfile.py,1.49,1.50

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
2002年8月17日 07:50:26 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv17942
Modified Files:
	tempfile.py 
Log Message:
Get rid of _once(); inlining it takes less code. :-)
Also, don't call gettempdir() in the default expression for the 'dir'
argument to various functions; use 'dir=None' for the default and
insert 'if dir is None: dir = gettemptir()' in the bodies. That way
the work done by gettempdir is postponed until needed.
Index: tempfile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/tempfile.py,v
retrieving revision 1.49
retrieving revision 1.50
diff -C2 -d -r1.49 -r1.50
*** tempfile.py	14 Aug 2002 15:41:26 -0000	1.49
--- tempfile.py	17 Aug 2002 14:50:24 -0000	1.50
***************
*** 81,111 ****
 _once_lock = _allocate_lock()
 
- def _once(var, initializer):
- """Wrapper to execute an initialization operation just once,
- even if multiple threads reach the same point at the same time.
- 
- var is the name (as a string) of the variable to be entered into
- the current global namespace.
- 
- initializer is a callable which will return the appropriate initial
- value for variable. It will be called only if variable is not
- present in the global namespace, or its current value is None.
- 
- Do not call _once from inside an initializer routine, it will deadlock.
- """
- 
- vars = globals()
- # Check first outside the lock.
- if vars.get(var) is not None:
- return
- try:
- _once_lock.acquire()
- # Check again inside the lock.
- if vars.get(var) is not None:
- return
- vars[var] = initializer()
- finally:
- _once_lock.release()
- 
 class _RandomNameSequence:
 """An instance of _RandomNameSequence generates an endless
--- 81,84 ----
***************
*** 179,184 ****
 def _get_default_tempdir():
 """Calculate the default directory to use for temporary files.
! This routine should be called through '_once' (see above) as we
! do not want multiple threads attempting this calculation simultaneously.
 
 We determine whether or not a candidate temp dir is usable by
--- 152,156 ----
 def _get_default_tempdir():
 """Calculate the default directory to use for temporary files.
! This routine should be called exactly once.
 
 We determine whether or not a candidate temp dir is usable by
***************
*** 213,220 ****
 ("No usable temporary directory found in %s" % dirlist))
 
 def _get_candidate_names():
 """Common setup sequence for all user-callable interfaces."""
 
! _once('_name_sequence', _RandomNameSequence)
 return _name_sequence
 
--- 185,201 ----
 ("No usable temporary directory found in %s" % dirlist))
 
+ _name_sequence = None
+ 
 def _get_candidate_names():
 """Common setup sequence for all user-callable interfaces."""
 
! global _name_sequence
! if _name_sequence is None:
! _once_lock.acquire()
! try:
! if _name_sequence is None:
! _name_sequence = _RandomNameSequence()
! finally:
! _once_lock.release()
 return _name_sequence
 
***************
*** 246,255 ****
 return template
 
 def gettempdir():
 """Accessor for tempdir.tempdir."""
! _once('tempdir', _get_default_tempdir)
 return tempdir
 
! def mkstemp(suffix="", prefix=template, dir=gettempdir(), text=False):
 """mkstemp([suffix, [prefix, [dir, [text]]]])
 User-callable function to create and return a unique temporary
--- 227,245 ----
 return template
 
+ tempdir = None
+ 
 def gettempdir():
 """Accessor for tempdir.tempdir."""
! global tempdir
! if tempdir is None:
! _once_lock.acquire()
! try:
! if tempdir is None:
! tempdir = _get_default_tempdir()
! finally:
! _once_lock.release()
 return tempdir
 
! def mkstemp(suffix="", prefix=template, dir=None, text=False):
 """mkstemp([suffix, [prefix, [dir, [text]]]])
 User-callable function to create and return a unique temporary
***************
*** 278,281 ****
--- 268,274 ----
 """
 
+ if dir is None:
+ dir = gettempdir()
+ 
 if text:
 flags = _text_openflags
***************
*** 286,290 ****
 
 
! def mkdtemp(suffix="", prefix=template, dir=gettempdir()):
 """mkdtemp([suffix, [prefix, [dir]]])
 User-callable function to create and return a unique temporary
--- 279,283 ----
 
 
! def mkdtemp(suffix="", prefix=template, dir=None):
 """mkdtemp([suffix, [prefix, [dir]]])
 User-callable function to create and return a unique temporary
***************
*** 300,303 ****
--- 293,299 ----
 """
 
+ if dir is None:
+ dir = gettempdir()
+ 
 names = _get_candidate_names()
 
***************
*** 315,319 ****
 raise IOError, (_errno.EEXIST, "No usable temporary directory name found")
 
! def mktemp(suffix="", prefix=template, dir=gettempdir()):
 """mktemp([suffix, [prefix, [dir]]])
 User-callable function to return a unique temporary file name. The
--- 311,315 ----
 raise IOError, (_errno.EEXIST, "No usable temporary directory name found")
 
! def mktemp(suffix="", prefix=template, dir=None):
 """mktemp([suffix, [prefix, [dir]]])
 User-callable function to return a unique temporary file name. The
***************
*** 333,336 ****
--- 329,335 ----
 RuntimeWarning, stacklevel=2)
 
+ if dir is None:
+ dir = gettempdir()
+ 
 names = _get_candidate_names()
 for seq in xrange(TMP_MAX):
***************
*** 384,388 ****
 
 def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
! prefix=template, dir=gettempdir()):
 """Create and return a temporary file.
 Arguments:
--- 383,387 ----
 
 def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
! prefix=template, dir=None):
 """Create and return a temporary file.
 Arguments:
***************
*** 397,400 ****
--- 396,402 ----
 """
 
+ if dir is None:
+ dir = gettempdir()
+ 
 if 'b' in mode:
 flags = _bin_openflags
***************
*** 418,422 ****
 else:
 def TemporaryFile(mode='w+b', bufsize=-1, suffix="",
! prefix=template, dir=gettempdir()):
 """Create and return a temporary file.
 Arguments:
--- 420,424 ----
 else:
 def TemporaryFile(mode='w+b', bufsize=-1, suffix="",
! prefix=template, dir=None):
 """Create and return a temporary file.
 Arguments:
***************
*** 429,432 ****
--- 431,437 ----
 exist when it is closed.
 """
+ 
+ if dir is None:
+ dir = gettempdir()
 
 if 'b' in mode:

AltStyle によって変換されたページ (->オリジナル) /