mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Added TemporaryFile and wrapper class by Jim Fulton.
Look in more env vars (for NT: TEMP, TMP).
This commit is contained in:
parent
3fb1aea0ae
commit
ca54982b2a
1 changed files with 41 additions and 2 deletions
|
@ -32,8 +32,9 @@ def gettempdir():
|
||||||
attempdirs.insert(0, dirname)
|
attempdirs.insert(0, dirname)
|
||||||
except macfs.error:
|
except macfs.error:
|
||||||
pass
|
pass
|
||||||
if os.environ.has_key('TMPDIR'):
|
for envname in 'TMPDIR', 'TEMP', 'TMP':
|
||||||
attempdirs.insert(0, os.environ['TMPDIR'])
|
if os.environ.has_key(envname):
|
||||||
|
attempdirs.insert(0, os.environ[envname])
|
||||||
testfile = gettempprefix() + 'test'
|
testfile = gettempprefix() + 'test'
|
||||||
for dir in attempdirs:
|
for dir in attempdirs:
|
||||||
try:
|
try:
|
||||||
|
@ -82,3 +83,41 @@ def mktemp():
|
||||||
file = os.path.join(dir, pre + `counter`)
|
file = os.path.join(dir, pre + `counter`)
|
||||||
if not os.path.exists(file):
|
if not os.path.exists(file):
|
||||||
return file
|
return file
|
||||||
|
|
||||||
|
|
||||||
|
class TemporaryFileWrapper:
|
||||||
|
"""Temporary file wrapper
|
||||||
|
|
||||||
|
This class provides a wrapper around files opened for temporary use.
|
||||||
|
In particular, it seeks to automatically remove the file when it is
|
||||||
|
no longer needed.
|
||||||
|
"""
|
||||||
|
def __init__(self, file, path):
|
||||||
|
self.file=file
|
||||||
|
self.path=path
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.file.close()
|
||||||
|
os.unlink(self.path)
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
try: self.close()
|
||||||
|
except: pass
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
file=self.__dict__['file']
|
||||||
|
a=getattr(file, name)
|
||||||
|
setattr(self, name, a)
|
||||||
|
return a
|
||||||
|
|
||||||
|
|
||||||
|
def TemporaryFile(mode='w+b', bufsize=-1):
|
||||||
|
name=mktemp()
|
||||||
|
file=open(name,mode,bufsize)
|
||||||
|
try:
|
||||||
|
os.unlink(name)
|
||||||
|
except os.error:
|
||||||
|
# Non-unix -- can't unlink file that's still open, use wrapper
|
||||||
|
return TemporaryFileWrapper(file, name)
|
||||||
|
else:
|
||||||
|
return file
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue