Skip to content

Commit 2cc70a2

Browse files
areuschAndrew Reusch
authored andcommitted
Don't remove() TemporaryFile in __del__.
* Not needed due at atexit mechanism at top of file. * Can't rely on complex functions from __del__ anyway. * Fixes warning message on my box: Exception ignored in: <function TempDirectory.__del__ at 0x12be10680> Traceback (most recent call last): File ".../tvm/python/tvm/contrib/util.py", line 55, in __del__ File ".../tvm/python/tvm/contrib/util.py", line 51, in remove File "/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 509, in rmtree AttributeError: 'NoneType' object has no attribute 'path'
1 parent 56941fb commit 2cc70a2

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

python/tvm/contrib/util.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
"""Common system utilities"""
18+
import atexit
1819
import os
1920
import tempfile
2021
import shutil
@@ -23,18 +24,25 @@
2324
except ImportError:
2425
fcntl = None
2526

26-
2727
class TempDirectory(object):
2828
"""Helper object to manage temp directory during testing.
2929
3030
Automatically removes the directory when it went out of scope.
3131
"""
32+
33+
TEMPDIRS = []
34+
@classmethod
35+
def remove_tempdirs(cls):
36+
for path in cls.TEMPDIRS:
37+
shutil.rmtree(path, ignore_errors=True)
38+
3239
def __init__(self, custom_path=None):
3340
if custom_path:
3441
os.mkdir(custom_path)
3542
self.temp_dir = custom_path
3643
else:
3744
self.temp_dir = tempfile.mkdtemp()
45+
self.TEMPDIRS.append(self.temp_dir)
3846
self._rmtree = shutil.rmtree
3947

4048
def remove(self):
@@ -43,9 +51,6 @@ def remove(self):
4351
self._rmtree(self.temp_dir, ignore_errors=True)
4452
self.temp_dir = None
4553

46-
def __del__(self):
47-
self.remove()
48-
4954
def relpath(self, name):
5055
"""Relative path in temp dir
5156
@@ -72,6 +77,9 @@ def listdir(self):
7277
return os.listdir(self.temp_dir)
7378

7479

80+
atexit.register(TempDirectory.remove_tempdirs)
81+
82+
7583
def tempdir(custom_path=None):
7684
"""Create temp dir which deletes the contents when exit.
7785

0 commit comments

Comments
 (0)