From fd078d2f66a3ccbebafed2b35f9c67d34628f192 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Sun, 4 Jan 2009 11:39:14 +0100 Subject: [PATCH] Support nested requests for the same temp file path --- src/sugar/util.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/sugar/util.py b/src/sugar/util.py index ac97f9af..d375b87a 100644 --- a/src/sugar/util.py +++ b/src/sugar/util.py @@ -263,19 +263,31 @@ def timestamp_to_elapsed_string(timestamp, max_levels=2): return ELAPSED % time_period -class TempFilePath(str): +_tracked_paths = {} +class TempFilePath(str): def __new__(cls, path=None): if path is None: fd, path = tempfile.mkstemp() os.close(fd) 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) def __del__(self): - if os.path.exists(self): - os.unlink(self) - logging.debug('TempFilePath deleted %r' % self) + if _tracked_paths[self] == 1: + del _tracked_paths[self] + + if os.path.exists(self): + os.unlink(self) + logging.debug('TempFilePath deleted %r' % self) + else: + logging.warning('TempFilePath already deleted %r' % self) else: - logging.warning('TempFilePath already deleted %r' % self) + _tracked_paths[self] -= 1