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

Open keywords #25684

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,17 @@ export readandwrite
# PR #25196
@deprecate_binding ObjectIdDict IdDict{Any,Any}

@deprecate open(filename::AbstractString, read::Bool) open(filename, read = read)
@deprecate open(filename::AbstractString, read::Bool, write::Bool) open(filename, read = read, write = write)
@deprecate open(filename::AbstractString, read::Bool, write::Bool, create::Bool) open(filename, read = read, write = write, create = create)
@deprecate open(filename::AbstractString, read::Bool, write::Bool, create::Bool, truncate::Bool) open(filename, read = read, write = write, create = create, truncate = truncate)
@deprecate open(filename::AbstractString, read::Bool, write::Bool, create::Bool, truncate::Bool, append::Bool) open(filename, read = read, write = write, create = craete, truncate = truncate, append = append)
@deprecate open(f::Function, filename::AbstractString, read::Bool) open(f, filename, read = read)
@deprecate open(f::Function, filename::AbstractString, read::Bool, write::Bool) open(f, filename, read = read, write = write)
@deprecate open(f::Function, filename::AbstractString, read::Bool, write::Bool, create::Bool) open(f, filename, read = read, write = write, create = create)
@deprecate open(f::Function, filename::AbstractString, read::Bool, write::Bool, create::Bool, truncate::Bool) open(f, filename, read = read, write = write, create = create, truncate = truncate)
@deprecate open(f::Function, filename::AbstractString, read::Bool, write::Bool, create::Bool, truncate::Bool, append::Bool) open(f, filename, read = read, write = write, create = craete, truncate = truncate, append = append)

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
30 changes: 15 additions & 15 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,23 +213,23 @@ fdio(fd::Integer, own::Bool=false) = fdio(string("<fd ",fd,">"), fd, own)


"""
open(filename::AbstractString, [read::Bool, write::Bool, create::Bool, truncate::Bool, append::Bool]) -> IOStream
open(filename::AbstractString; write::Bool = true, read::Bool = !write, create::Bool = true, truncate::Bool = true, append::Bool = true) -> IOStream
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to wrap the line here.


Open a file in a mode specified by five boolean arguments. The default is to open files for
reading only. Return a stream for accessing the file.
"""
function open(fname::AbstractString, rd::Bool, wr::Bool, cr::Bool, tr::Bool, ff::Bool)
function open(fname::AbstractString; write::Bool = false, read::Bool = !write, create::Bool = false, truncate::Bool = false, append::Bool = false)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be some relationship between truncate and append. Probably truncate = !append. Otherwise the behavior of open(file, append=true) is going to be rather surprising. I also suspect that write = append makes sense. Similarly, create typically default to true if write is set. I think you'll need to draw a graph of the relationships here to figure this one out – this is why this change hasn't been done yet.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and did it, although this probably could have been done without the image:
open_keywords
So it looks like the signature should be something like this:

open(fname::AbstractString;
    append::Bool = false,
    write::Bool = append,
    read::Bool = !write,
    create::Bool = write,
    truncate::Bool = !append,
)

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that's not quite right since you want truncate = write && !append.

Copy link
Contributor Author

@bramtayl bramtayl Jan 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so

open(fname::AbstractString;
    append::Bool = false,
    write::Bool = append,
    read::Bool = !write,
    create::Bool = write,
    truncate::Bool = write && !append,
)

?

s = IOStream(string("<file ",fname,">"))
systemerror("opening file $fname",
ccall(:ios_file, Ptr{Cvoid},
(Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint),
s.ios, fname, rd, wr, cr, tr) == C_NULL)
if ff
s.ios, fname, read, write, create, truncate) == C_NULL)
if append
systemerror("seeking to end of file $fname", ccall(:ios_seek_end, Int64, (Ptr{Cvoid},), s.ios) != 0)
end
return s
end
open(fname::AbstractString) = open(fname, true, false, false, false, false)
open(fname::AbstractString) = open(fname; read = true, write = false, create = false, truncate = false, append = false)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overwrites the above method with a different set of keyword arguments.


"""
open(filename::AbstractString, [mode::AbstractString]) -> IOStream
Expand Down Expand Up @@ -277,19 +277,19 @@ julia> rm("myfile.txt")
```
"""
function open(fname::AbstractString, mode::AbstractString)
mode == "r" ? open(fname, true , false, false, false, false) :
mode == "r+" ? open(fname, true , true , false, false, false) :
mode == "w" ? open(fname, false, true , true , true , false) :
mode == "w+" ? open(fname, true , true , true , true , false) :
mode == "a" ? open(fname, false, true , true , false, true ) :
mode == "a+" ? open(fname, true , true , true , false, true ) :
mode == "r" ? open(fname; read = true , write = false, create = false, truncate = false, append = false) :
mode == "r+" ? open(fname, read = true , write = true , create = false, truncate = false, append = false) :
mode == "w" ? open(fname, read = false, write = true , create = true , truncate = true , append = false) :
mode == "w+" ? open(fname, read = true , write = true , create = true , truncate = true , append = false) :
mode == "a" ? open(fname, read = false, write = true , create = true , truncate = false, append = true ) :
mode == "a+" ? open(fname, read = true , write = true , create = true , truncate = false, append = true ) :
throw(ArgumentError("invalid open mode: $mode"))
end

"""
open(f::Function, args...)
open(f::Function, args...; kwargs....)

Apply the function `f` to the result of `open(args...)` and close the resulting file
Apply the function `f` to the result of `open(args...; kwargs...)` and close the resulting file
descriptor upon completion.

# Examples
Expand All @@ -304,8 +304,8 @@ julia> open(f->read(f, String), "myfile.txt")
julia> rm("myfile.txt")
```
"""
function open(f::Function, args...)
io = open(args...)
function open(f::Function, args...; kwargs...)
io = open(args...; kwargs...)
try
f(io)
finally
Expand Down
2 changes: 1 addition & 1 deletion stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ function setup_interface(
if repl.history_file
try
hist_path = find_hist_file()
f = open(hist_path, true, true, true, false, false)
f = open(hist_path, read = true, write = true, create = true, truncate = false, append = false)
finalizer(replc) do replc
close(f)
end
Expand Down