Support nested requests for the same temp file path

master
Tomeu Vizoso 16 years ago
parent de2aa036df
commit fd078d2f66

@ -263,19 +263,31 @@ def timestamp_to_elapsed_string(timestamp, max_levels=2):
return ELAPSED % time_period return ELAPSED % time_period
class TempFilePath(str): _tracked_paths = {}
class TempFilePath(str):
def __new__(cls, path=None): def __new__(cls, path=None):
if path is None: if path is None:
fd, path = tempfile.mkstemp() fd, path = tempfile.mkstemp()
os.close(fd) os.close(fd)
logging.debug('TempFilePath created %r' % path) logging.debug('TempFilePath created %r' % path)
if path in _tracked_paths:
_tracked_paths[path] += 1
else:
_tracked_paths[path] = 1
return str.__new__(cls, path) return str.__new__(cls, path)
def __del__(self): def __del__(self):
if os.path.exists(self): if _tracked_paths[self] == 1:
os.unlink(self) del _tracked_paths[self]
logging.debug('TempFilePath deleted %r' % self)
if os.path.exists(self):
os.unlink(self)
logging.debug('TempFilePath deleted %r' % self)
else:
logging.warning('TempFilePath already deleted %r' % self)
else: else:
logging.warning('TempFilePath already deleted %r' % self) _tracked_paths[self] -= 1

Loading…
Cancel
Save