1- clean_python_tmp_dir <- function () {
1+
2+ trace_tempfile_mkdtemp <- function () {
3+
4+ py_created_tempdirs <- NULL
5+
6+ on_py_init <- function () {
7+
8+ py_created_tempdirs <<-
9+ reticulate :: py_run_string(
10+ local = TRUE , convert = FALSE , "
11+ def patch_mkdtemp():
12+ from functools import wraps
13+ from os.path import abspath
14+ import tempfile
15+
16+ created_tempdirs = []
17+ og_mkdtemp = tempfile.mkdtemp
18+
19+ @wraps(og_mkdtemp)
20+ def mkdtemp(*args, **kwargs):
21+ out = og_mkdtemp(*args, **kwargs)
22+ created_tempdirs.append(abspath(out))
23+ return out
24+
25+ tempfile.mkdtemp = mkdtemp
26+ return created_tempdirs
27+
28+ " )$ patch_mkdtemp()
29+
30+ }
31+
32+
33+ if (reticulate :: py_available()) on_py_init()
34+ else setHook(" reticulate.onPyInit" , on_py_init )
35+
36+ function () py_created_tempdirs
37+ }
38+
39+ get_py_created_tempdirs <- trace_tempfile_mkdtemp()
40+
41+ clean_python_tempdirs <- function () {
242
343 if (! reticulate :: py_available())
444 # python never initialized, nothing to do
@@ -17,6 +57,10 @@ clean_python_tmp_dir <- function() {
1757 )
1858
1959 unlink(detritus , recursive = TRUE )
60+
61+ unlink(unlist(py_to_r(get_py_created_tempdirs())),
62+ recursive = TRUE )
63+
2064}
2165
22- withr :: defer(clean_python_tmp_dir (), teardown_env())
66+ withr :: defer(clean_python_tempdirs (), teardown_env())
0 commit comments