Skip to content

Commit a796a41

Browse files
add suffix kwarg to tempname (#53474)
`tempname` checks that the name its returning is not already a file, however if you want that filename to contain other information, like a file extension, that can mean the uniquing isn't complete. This adds `tempname(suffix = "_foo.txt")` to include a suffix in the name and uniquing check. --------- Co-authored-by: Jameson Nash <vtjnash@gmail.com>
1 parent 4634c74 commit a796a41

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ New library functions
2727
New library features
2828
--------------------
2929

30+
* `tempname` can now take a suffix string to allow the file name to include a suffix and include that suffix in
31+
the uniquing checking ([#53474])
32+
3033
Standard library changes
3134
------------------------
3235

base/file.jl

+8-4
Original file line numberDiff line numberDiff line change
@@ -620,13 +620,13 @@ end
620620

621621

622622
# Obtain a temporary filename.
623-
function tempname(parent::AbstractString=tempdir(); max_tries::Int = 100, cleanup::Bool=true)
623+
function tempname(parent::AbstractString=tempdir(); max_tries::Int = 100, cleanup::Bool=true, suffix::AbstractString="")
624624
isdir(parent) || throw(ArgumentError("$(repr(parent)) is not a directory"))
625625

626626
prefix = joinpath(parent, temp_prefix)
627627
filename = nothing
628628
for i in 1:max_tries
629-
filename = string(prefix, _rand_filename())
629+
filename = string(prefix, _rand_filename(), suffix)
630630
if ispath(filename)
631631
filename = nothing
632632
else
@@ -682,7 +682,7 @@ end # os-test
682682

683683

684684
"""
685-
tempname(parent=tempdir(); cleanup=true) -> String
685+
tempname(parent=tempdir(); cleanup=true, suffix="") -> String
686686
687687
Generate a temporary file path. This function only returns a path; no file is
688688
created. The path is likely to be unique, but this cannot be guaranteed due to
@@ -693,7 +693,8 @@ existing at the time of the call to `tempname`.
693693
When called with no arguments, the temporary name will be an absolute path to a
694694
temporary name in the system temporary directory as given by `tempdir()`. If a
695695
`parent` directory argument is given, the temporary path will be in that
696-
directory instead.
696+
directory instead. If a suffix is given the tempname will end with that suffix
697+
and be tested for uniqueness with that suffix.
697698
698699
The `cleanup` option controls whether the process attempts to delete the
699700
returned path automatically when the process exits. Note that the `tempname`
@@ -705,6 +706,9 @@ you do and `cleanup` is `true` it will be deleted upon process termination.
705706
The `parent` and `cleanup` arguments were added in 1.4. Prior to Julia 1.4
706707
the path `tempname` would never be cleaned up at process termination.
707708
709+
!!! compat "Julia 1.12"
710+
The `suffix` keyword argument was added in Julia 1.12.
711+
708712
!!! warning
709713
710714
This can lead to security holes if another process obtains the same

test/file.jl

+3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ end
124124
end
125125
@test_throws ArgumentError tempname(randstring())
126126
end
127+
@testset "tempname with suffix" begin
128+
@test !isfile(tempname(suffix = "_foo.txt"))
129+
end
127130

128131
child_eval(code::String) = eval(Meta.parse(readchomp(`$(Base.julia_cmd()) -E $code`)))
129132

0 commit comments

Comments
 (0)