Skip to content

Commit ca8c7c1

Browse files
chillenbsunqm
andauthored
Use weakref for automatic deletion of tmpfiles (to fix pyscf#2318) (pyscf#2320)
* Use weakref for automatic deletion of tmpfiles * github actions tests check number of leftover temporary files * temporary file check pytest exit status * Avoid h5f.flush() on closed file --------- Co-authored-by: Qiming Sun <[email protected]>
1 parent 9a9b2d5 commit ca8c7c1

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

.github/workflows/run_tests.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ export OMP_NUM_THREADS=1
33
export PYTHONPATH=$(pwd):$PYTHONPATH
44
ulimit -s 20000
55

6+
mkdir -p pyscftmpdir
67
echo 'pbc_tools_pbc_fft_engine = "NUMPY"' > .pyscf_conf.py
78
echo "dftd3_DFTD3PATH = './pyscf/lib/deps/lib'" >> .pyscf_conf.py
89
echo "scf_hf_SCF_mute_chkfile = True" >> .pyscf_conf.py
10+
echo 'TMPDIR = "./pyscftmpdir"' >> .pyscf_conf.py
911

1012
version=$(python -c 'import sys; print("{0}.{1}".format(*sys.version_info[:2]))')
1113
# pytest-cov on Python 3.12 consumes huge memory
@@ -15,3 +17,14 @@ if [ "$RUNNER_OS" == "Linux" ] && [ $version != "3.12" ]; then
1517
else
1618
pytest pyscf/ -s -c pytest.ini pyscf
1719
fi
20+
21+
pytest_status=$?
22+
23+
num_tmpfiles="$(ls -1 pyscftmpdir | wc -l)"
24+
echo "There are "$num_tmpfiles" leftover temporary files"
25+
rm -rf pyscftmpdir
26+
27+
# Test fails if pytest failed or if temporary files were left over.
28+
if test "$num_tmpfiles" -gt 0 || test "$pytest_status" -ne 0; then
29+
exit 1
30+
fi

pyscf/lib/misc.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import itertools
3232
import inspect
3333
import collections
34+
import weakref
3435
import ctypes
3536
import numpy
3637
import scipy
@@ -1144,7 +1145,7 @@ def _finished(self):
11441145
causes outcore DF to hang on an NFS filesystem.
11451146
'''
11461147
try:
1147-
if super().id:
1148+
if super().id and super().id.valid:
11481149
super().flush()
11491150
super().close()
11501151
except AttributeError: # close not defined in old h5py
@@ -1181,6 +1182,14 @@ def __init__(self, filename=None, mode='a', prefix='', suffix='',
11811182
if filename is None:
11821183
filename = H5TmpFile._gen_unique_name(dir, pre=prefix, suf=suffix)
11831184
self.delete_on_close = True
1185+
1186+
def _delete_with_check(fname, should_delete):
1187+
if should_delete and os.path.exists(fname):
1188+
os.remove(fname)
1189+
1190+
self._finalizer = weakref.finalize(self, _delete_with_check,
1191+
filename, self.delete_on_close)
1192+
11841193
super().__init__(filename, mode, *args, **kwargs)
11851194

11861195
# Python 3 stdlib does not have a way to just generate
@@ -1201,11 +1210,9 @@ def _gen_unique_name(directory, pre='', suf=''):
12011210
raise FileExistsError("No usable temporary file name found")
12021211

12031212
def close(self):
1204-
filename = self.filename
12051213
self._finished()
1206-
if self.delete_on_close:
1207-
if os.path.exists(filename):
1208-
os.remove(filename)
1214+
self._finalizer()
1215+
12091216
def __exit__(self, type, value, traceback):
12101217
self.close()
12111218

0 commit comments

Comments
 (0)