Skip to content

Commit 625fab3

Browse files
tempname(cleanup=true|false): add cleanup keyword to tempname
1 parent 788acce commit 625fab3

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ New library functions
2222

2323
* The `splitpath` function now accepts any `AbstractString` whereas previously it only accepted paths of type `String` ([#33012]).
2424
* The `tempname` function now takes an optional `parent::AbstractString` argument to give it a directory in which to attempt to produce a temporary path name ([#33090]).
25+
* The `tempname` function now takes a `cleanup::Bool` keyword argument defaulting to `false`; when set to `true` the process will try to ensure that any file or directory at the path returned by `tempname` is deleted upon process exit ([#33090]).
2526

2627

2728
Standard library changes

base/file.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ function mktemp(parent::AbstractString=tempdir(); cleanup::Bool=true)
500500
return (filename, Base.open(filename, "r+"))
501501
end
502502

503-
function tempname(parent::AbstractString=tempdir())
503+
function tempname(parent::AbstractString=tempdir(); cleanup::Bool=false)
504504
isdir(parent) || throw(ArgumentError("$(repr(parent)) is not a directory"))
505505
seed::UInt32 = rand(UInt32)
506506
while true
@@ -509,6 +509,7 @@ function tempname(parent::AbstractString=tempdir())
509509
end
510510
filename = _win_tempname(parent, seed)
511511
if !ispath(filename)
512+
cleanup && temp_cleanup_later(filename)
512513
return filename
513514
end
514515
seed += 1
@@ -518,12 +519,13 @@ end
518519
else # !windows
519520

520521
# Obtain a temporary filename.
521-
function tempname(parent::AbstractString=tempdir())
522+
function tempname(parent::AbstractString=tempdir(); cleanup::Bool=false)
522523
isdir(parent) || throw(ArgumentError("$(repr(parent)) is not a directory"))
523524
p = ccall(:tempnam, Cstring, (Cstring, Cstring), parent, temp_prefix)
524525
systemerror(:tempnam, p == C_NULL)
525526
s = unsafe_string(p)
526527
Libc.free(p)
528+
cleanup && temp_cleanup_later(s)
527529
return s
528530
end
529531

@@ -541,7 +543,7 @@ end # os-test
541543

542544

543545
"""
544-
tempname(parent=tempdir()) -> String
546+
tempname(parent=tempdir(); cleanup=false) -> String
545547
546548
Generate a temporary file path. This function only returns a path; no file is
547549
created. The path is likely to be unique, but this cannot be guaranteed due to
@@ -554,8 +556,15 @@ temporary name in the system temporary directory as given by `tempdir()`. If a
554556
`parent` directory argument is given, the temporary path will be in that
555557
directory instead.
556558
559+
The `cleanup` option controls whether the process attempts to delete the
560+
returned path automatically when the process exits. Note that the `tempname`
561+
function does not create any file or directory at the returned location, so
562+
there is nothing to cleanup unless you create a file or directory there. If
563+
you do and `clean` is `true` it will be deleted upon process termination.
564+
557565
!!! compat "Julia 1.4"
558-
The `parent` argument was added in 1.4.
566+
The `parent` and `cleanup` arguments were added in 1.4. Prior to Julia 1.4
567+
the path `tempname` would never be cleaned up at process termination.
559568
560569
!!! warning
561570

test/file.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ child_eval(code::String) = eval(Meta.parse(readchomp(`$(Base.julia_cmd()) -E $co
7979
# mktempdir with cleanup
8080
t = child_eval("t = mktempdir(); touch(joinpath(t, \"file.txt\")); t")
8181
@test !ispath(t)
82+
# tempname without cleanup
83+
t = child_eval("t = tempname(); touch(t); t")
84+
@test isfile(t)
85+
rm(t, force=true)
86+
@test !ispath(t)
87+
# tempname with cleanup
88+
t = child_eval("t = tempname(cleanup=true); touch(t); t")
89+
@test !ispath(t)
8290
end
8391

8492
import Base.Filesystem: TEMP_CLEANUP_MIN, TEMP_CLEANUP_MAX, TEMP_CLEANUP

0 commit comments

Comments
 (0)