Skip to content

Commit 44b55d1

Browse files
authored
make sure TOML.parsefile error when used on a directory (#38078)
1 parent 444aa87 commit 44b55d1

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

stdlib/TOML/src/TOML.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ module Internals
1717
end
1818
end
1919

20+
# https://github.com/JuliaLang/julia/issues/36605
21+
readstring(f::AbstractString) = isfile(f) ? read(f, String) : error(repr(f), ": No such file")
22+
2023
"""
2124
Parser()
2225
@@ -38,9 +41,9 @@ Parse file `f` and return the resulting table (dictionary). Throw a
3841
See also: [`TOML.tryparsefile`](@ref)
3942
"""
4043
parsefile(f::AbstractString) =
41-
Internals.parse(Parser(read(f, String); filepath=abspath(f)))
44+
Internals.parse(Parser(readstring(f); filepath=abspath(f)))
4245
parsefile(p::Parser, f::AbstractString) =
43-
Internals.parse(Internals.reinit!(p, read(f, String); filepath=abspath(f)))
46+
Internals.parse(Internals.reinit!(p, readstring(f); filepath=abspath(f)))
4447

4548
"""
4649
tryparsefile(f::AbstractString)
@@ -52,9 +55,9 @@ Parse file `f` and return the resulting table (dictionary). Return a
5255
See also: [`TOML.parsefile`](@ref)
5356
"""
5457
tryparsefile(f::AbstractString) =
55-
Internals.tryparse(Parser(read(f, String); filepath=abspath(f)))
58+
Internals.tryparse(Parser(readstring(f); filepath=abspath(f)))
5659
tryparsefile(p::Parser, f::AbstractString) =
57-
Internals.tryparse(Internals.reinit!(p, read(f, String); filepath=abspath(f)))
60+
Internals.tryparse(Internals.reinit!(p, readstring(f); filepath=abspath(f)))
5861

5962
"""
6063
parse(x::Union{AbstractString, IO})

stdlib/TOML/test/parse.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ using TOML: ParserError
3838
@test_throws ParserError TOML.parsefile(SubString(invalid_path))
3939
@test_throws ParserError TOML.parsefile(p, invalid_path)
4040
@test_throws ParserError TOML.parsefile(p, SubString(invalid_path))
41+
@test_throws ErrorException TOML.parsefile(homedir())
42+
@test_throws ErrorException TOML.parsefile(p, homedir())
4143
# TOML.tryparsefile
4244
@test TOML.tryparsefile(path) == TOML.tryparsefile(SubString(path)) ==
4345
TOML.tryparsefile(p, path) == TOML.tryparsefile(p, SubString(path)) == dict
4446
@test TOML.tryparsefile(invalid_path) isa ParserError
4547
@test TOML.tryparsefile(SubString(invalid_path)) isa ParserError
4648
@test TOML.tryparsefile(p, invalid_path) isa ParserError
4749
@test TOML.tryparsefile(p, SubString(invalid_path)) isa ParserError
50+
@test_throws ErrorException TOML.tryparsefile(homedir())
51+
@test_throws ErrorException TOML.tryparsefile(p, homedir())
4852
end

0 commit comments

Comments
 (0)