Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add suffix kwarg to tempname #53474

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ New library functions
New library features
--------------------

* `tempname` can now take a suffix string to allow the file name to include a suffix and include that suffix in
the uniquing checking ([#])

Standard library changes
------------------------

Expand Down
12 changes: 8 additions & 4 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@ end


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

prefix = joinpath(parent, temp_prefix)
filename = nothing
for i in 1:max_tries
filename = string(prefix, _rand_filename())
filename = string(prefix, _rand_filename(), suffix)
if ispath(filename)
filename = nothing
else
Expand Down Expand Up @@ -682,7 +682,7 @@ end # os-test


"""
tempname(parent=tempdir(); cleanup=true) -> String
tempname(parent=tempdir(); cleanup=true, suffix="") -> String

Generate a temporary file path. This function only returns a path; no file is
created. The path is likely to be unique, but this cannot be guaranteed due to
Expand All @@ -693,7 +693,8 @@ existing at the time of the call to `tempname`.
When called with no arguments, the temporary name will be an absolute path to a
temporary name in the system temporary directory as given by `tempdir()`. If a
`parent` directory argument is given, the temporary path will be in that
directory instead.
directory instead. If a suffix is given the tempname will end with that suffix
and be tested for uniqueness with that suffix.

The `cleanup` option controls whether the process attempts to delete the
returned path automatically when the process exits. Note that the `tempname`
Expand All @@ -705,6 +706,9 @@ you do and `cleanup` is `true` it will be deleted upon process termination.
The `parent` and `cleanup` arguments were added in 1.4. Prior to Julia 1.4
the path `tempname` would never be cleaned up at process termination.

!!! compat "Julia 1.12"
The `suffix` keyword argument was added in Julia 1.12.

!!! warning

This can lead to security holes if another process obtains the same
Expand Down
3 changes: 3 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ end
end
@test_throws ArgumentError tempname(randstring())
end
@testset "tempname with suffix" begin
@test !isfile(tempname(suffix = "_foo.txt"))
end

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

Expand Down