Skip to content

Commit

Permalink
Merge pull request numpy#6695 from juliantaylor/tmpdir-leak
Browse files Browse the repository at this point in the history
BUG: fix removing tempdirs created during build
  • Loading branch information
charris committed Nov 17, 2015
2 parents 4ee1ed5 + e67156e commit 93f1fe8
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions numpy/distutils/misc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
except ImportError:
from dummy_threading import local as tlocal

# stores temporary directory of each thread to only create one per thread
_tdata = tlocal()

# store all created temporary directories so they can be deleted on exit
_tmpdirs = []
def clean_up_temporary_directory():
for d in _tmpdirs:
try:
shutil.rmtree(d)
except OSError:
pass

atexit.register(clean_up_temporary_directory)

try:
set
except NameError:
Expand Down Expand Up @@ -283,26 +297,13 @@ def gpaths(paths, local_path='', include_non_existing=True):
paths = (paths,)
return _fix_paths(paths, local_path, include_non_existing)


def clean_up_temporary_directory():
tdata = tlocal()
_temporary_directory = getattr(tdata, 'tempdir', None)
if not _temporary_directory:
return
try:
shutil.rmtree(_temporary_directory)
except OSError:
pass
_temporary_directory = None

def make_temp_file(suffix='', prefix='', text=True):
tdata = tlocal()
if not hasattr(tdata, 'tempdir'):
tdata.tempdir = tempfile.mkdtemp()
atexit.register(clean_up_temporary_directory)
if not hasattr(_tdata, 'tempdir'):
_tdata.tempdir = tempfile.mkdtemp()
_tmpdirs.append(_tdata.tempdir)
fid, name = tempfile.mkstemp(suffix=suffix,
prefix=prefix,
dir=tdata.tempdir,
dir=_tdata.tempdir,
text=text)
fo = os.fdopen(fid, 'w')
return fo, name
Expand Down

0 comments on commit 93f1fe8

Please sign in to comment.