Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
tempfile Namespace Reference

Data Structures

class  TemporaryFileWrapper
 
class  _ThreadSafeCounter
 
class  _DummyMutex
 

Functions

def gettempdir
 
def gettempprefix
 
def mktemp
 
def TemporaryFile
 

Variables

list __all__ = ["mktemp", "TemporaryFile", "tempdir", "gettempprefix"]
 
 tempdir = None
 
 template = None
 
tuple _tempdir_lock = _DummyMutex()
 
tuple _counter = _ThreadSafeCounter(thread.allocate_lock())
 

Detailed Description

Temporary files and filenames.

Function Documentation

def tempfile.gettempdir ( )
Function to calculate the directory to use.

Definition at line 15 of file tempfile.py.

References os.getenv(), gettempprefix(), and aifc.open().

15 
16 def gettempdir():
17  """Function to calculate the directory to use."""
18  global tempdir
19  if tempdir is not None:
20  return tempdir
21 
22  # _gettempdir_inner deduces whether a candidate temp dir is usable by
23  # trying to create a file in it, and write to it. If that succeeds,
24  # great, it closes the file and unlinks it. There's a race, though:
25  # the *name* of the test file it tries is the same across all threads
26  # under most OSes (Linux is an exception), and letting multiple threads
27  # all try to open, write to, close, and unlink a single file can cause
28  # a variety of bogus errors (e.g., you cannot unlink a file under
29  # Windows if anyone has it open, and two threads cannot create the
30  # same file in O_EXCL mode under Unix). The simplest cure is to serialize
31  # calls to _gettempdir_inner. This isn't a real expense, because the
32  # first thread to succeed sets the global tempdir, and all subsequent
33  # calls to gettempdir() reuse that without trying _gettempdir_inner.
34  _tempdir_lock.acquire()
35  try:
36  return _gettempdir_inner()
37  finally:
38  _tempdir_lock.release()
def tempfile.gettempprefix ( )
Function to calculate a prefix of the filename to use.

This incorporates the current process id on systems that support such a
notion, so that concurrent processes don't generate the same prefix.

Definition at line 123 of file tempfile.py.

124 def gettempprefix():
125  """Function to calculate a prefix of the filename to use.
126 
127  This incorporates the current process id on systems that support such a
128  notion, so that concurrent processes don't generate the same prefix.
129  """
130 
131  global template
132  if template is None:
133  return '@' + `os.getpid()` + '.'
134  else:
135  return template
136 
def tempfile.mktemp (   suffix = "")
User-callable function to return a unique temporary file name.

Definition at line 137 of file tempfile.py.

References gettempdir(), gettempprefix(), and locale.str().

138 def mktemp(suffix=""):
139  """User-callable function to return a unique temporary file name."""
140  dir = gettempdir()
141  pre = gettempprefix()
142  while 1:
143  i = _counter.get_next()
144  file = os.path.join(dir, pre + str(i) + suffix)
145  if not os.path.exists(file):
146  return file
147 
def tempfile.TemporaryFile (   mode = 'w+b',
  bufsize = -1,
  suffix = "" 
)
Create and return a temporary file (opened read-write by default).

Definition at line 184 of file tempfile.py.

References mktemp(), and aifc.open().

185 def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
186  """Create and return a temporary file (opened read-write by default)."""
187  name = mktemp(suffix)
188  if os.name == 'posix':
189  # Unix -- be very careful
190  fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
191  try:
192  os.unlink(name)
193  return os.fdopen(fd, mode, bufsize)
194  except:
195  os.close(fd)
196  raise
197  else:
198  # Non-unix -- can't unlink file that's still open, use wrapper
199  file = open(name, mode, bufsize)
200  return TemporaryFileWrapper(file, name)
201 
202 # In order to generate unique names, mktemp() uses _counter.get_next().
203 # This returns a unique integer on each call, in a threadsafe way (i.e.,
204 # multiple threads will never see the same integer). The integer will
205 # usually be a Python int, but if _counter.get_next() is called often
206 # enough, it will become a Python long.
207 # Note that the only names that survive this next block of code
208 # are "_counter" and "_tempdir_lock".

Variable Documentation

list __all__ = ["mktemp", "TemporaryFile", "tempdir", "gettempprefix"]

Definition at line 9 of file tempfile.py.

tuple _counter = _ThreadSafeCounter(thread.allocate_lock())

Definition at line 240 of file tempfile.py.

tuple _tempdir_lock = _DummyMutex()

Definition at line 236 of file tempfile.py.

tempdir = None

Definition at line 12 of file tempfile.py.

string template = None

Definition at line 13 of file tempfile.py.