Skip to content

Commit 56c5075

Browse files
open: deprecate positional flags for keywords (#25696)
1 parent f7b1033 commit 56c5075

File tree

3 files changed

+94
-26
lines changed

3 files changed

+94
-26
lines changed

base/deprecated.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,15 @@ end
15121512

15131513
@deprecate lexless isless
15141514

1515+
@deprecate(
1516+
open(filename::AbstractString, read::Bool, write::Bool, create::Bool, truncate::Bool, append::Bool),
1517+
open(filename, read = read, write = write, create = create, truncate = truncate, append = append)
1518+
)
1519+
@deprecate(
1520+
open(f::Function, filename::AbstractString, read::Bool, write::Bool, create::Bool, truncate::Bool, append::Bool),
1521+
open(f, filename, read = read, write = write, create = create, truncate = truncate, append = append)
1522+
)
1523+
15151524
@deprecate_binding iteratorsize IteratorSize
15161525
@deprecate_binding iteratoreltype IteratorEltype
15171526

base/iostream.jl

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,84 @@ function fdio(name::AbstractString, fd::Integer, own::Bool=false)
211211
end
212212
fdio(fd::Integer, own::Bool=false) = fdio(string("<fd ",fd,">"), fd, own)
213213

214+
"""
215+
open_flags(; keywords...) -> NamedTuple
216+
217+
Compute the `read`, `write`, `create`, `truncate`, `append` flag value for
218+
a given set of keyword arguments to `open` a `NamedTuple`.
219+
"""
220+
function open_flags(;
221+
read :: Union{Bool,Nothing} = nothing,
222+
write :: Union{Bool,Nothing} = nothing,
223+
create :: Union{Bool,Nothing} = nothing,
224+
truncate :: Union{Bool,Nothing} = nothing,
225+
append :: Union{Bool,Nothing} = nothing,
226+
)
227+
if write === true && read !== true && append !== true
228+
create === nothing && (create = true)
229+
truncate === nothing && (truncate = true)
230+
end
231+
232+
if truncate === true || append === true
233+
write === nothing && (write = true)
234+
create === nothing && (create = true)
235+
end
236+
237+
write === nothing && (write = false)
238+
read === nothing && (read = !write)
239+
create === nothing && (create = false)
240+
truncate === nothing && (truncate = false)
241+
append === nothing && (append = false)
242+
243+
return (
244+
read = read,
245+
write = write,
246+
create = create,
247+
truncate = truncate,
248+
append = append,
249+
)
250+
end
214251

215252
"""
216-
open(filename::AbstractString, [read::Bool, write::Bool, create::Bool, truncate::Bool, append::Bool]) -> IOStream
253+
open(filename::AbstractString; keywords...) -> IOStream
254+
255+
Open a file in a mode specified by five boolean keyword arguments:
256+
257+
| Keyword | Desciption | Default |
258+
|:-----------|:-----------------------|:----------------------------------------|
259+
| `read` | open for reading | `!write` |
260+
| `write` | open for writing | `truncate \\| append` |
261+
| `create` | create if non-existent | `!read & write \\| truncate \\| append` |
262+
| `truncate` | truncate to zero size | `!read & write` |
263+
| `append` | seek to end | `false` |
217264
218-
Open a file in a mode specified by five boolean arguments. The default is to open files for
219-
reading only. Return a stream for accessing the file.
265+
The default when no keywords are passed is to open files for reading only.
266+
Returns a stream for accessing the opened file.
220267
"""
221-
function open(fname::AbstractString, rd::Bool, wr::Bool, cr::Bool, tr::Bool, ff::Bool)
268+
function open(fname::AbstractString;
269+
read :: Union{Bool,Nothing} = nothing,
270+
write :: Union{Bool,Nothing} = nothing,
271+
create :: Union{Bool,Nothing} = nothing,
272+
truncate :: Union{Bool,Nothing} = nothing,
273+
append :: Union{Bool,Nothing} = nothing,
274+
)
275+
flags = open_flags(
276+
read = read,
277+
write = write,
278+
create = create,
279+
truncate = truncate,
280+
append = append,
281+
)
222282
s = IOStream(string("<file ",fname,">"))
223283
systemerror("opening file $fname",
224284
ccall(:ios_file, Ptr{Cvoid},
225285
(Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint),
226-
s.ios, fname, rd, wr, cr, tr) == C_NULL)
227-
if ff
286+
s.ios, fname, flags.read, flags.write, flags.create, flags.truncate) == C_NULL)
287+
if flags.append
228288
systemerror("seeking to end of file $fname", ccall(:ios_seek_end, Int64, (Ptr{Cvoid},), s.ios) != 0)
229289
end
230290
return s
231291
end
232-
open(fname::AbstractString) = open(fname, true, false, false, false, false)
233292

234293
"""
235294
open(filename::AbstractString, [mode::AbstractString]) -> IOStream
@@ -238,14 +297,14 @@ Alternate syntax for open, where a string-based mode specifier is used instead o
238297
booleans. The values of `mode` correspond to those from `fopen(3)` or Perl `open`, and are
239298
equivalent to setting the following boolean groups:
240299
241-
| Mode | Description |
242-
|:-----|:------------------------------|
243-
| r | read |
244-
| r+ | read, write |
245-
| w | write, create, truncate |
246-
| w+ | read, write, create, truncate |
247-
| a | write, create, append |
248-
| a+ | read, write, create, append |
300+
| Mode | Description | Keywords |
301+
|:-----|:------------------------------|:------------------------------------|
302+
| `r` | read | none |
303+
| `w` | write, create, truncate | `write = true` |
304+
| `a` | write, create, append | `append = true` |
305+
| `r+` | read, write | `read = true, write = true` |
306+
| `w+` | read, write, create, truncate | `truncate = true, read = true` |
307+
| `a+` | read, write, create, append | `append = true, read = true` |
249308
250309
# Examples
251310
```jldoctest
@@ -277,19 +336,19 @@ julia> rm("myfile.txt")
277336
```
278337
"""
279338
function open(fname::AbstractString, mode::AbstractString)
280-
mode == "r" ? open(fname, true , false, false, false, false) :
281-
mode == "r+" ? open(fname, true , true , false, false, false) :
282-
mode == "w" ? open(fname, false, true , true , true , false) :
283-
mode == "w+" ? open(fname, true , true , true , true , false) :
284-
mode == "a" ? open(fname, false, true , true , false, true ) :
285-
mode == "a+" ? open(fname, true , true , true , false, true ) :
339+
mode == "r" ? open(fname, read = true) :
340+
mode == "r+" ? open(fname, read = true, write = true) :
341+
mode == "w" ? open(fname, truncate = true) :
342+
mode == "w+" ? open(fname, truncate = true, read = true) :
343+
mode == "a" ? open(fname, append = true) :
344+
mode == "a+" ? open(fname, append = true, read = true) :
286345
throw(ArgumentError("invalid open mode: $mode"))
287346
end
288347

289348
"""
290-
open(f::Function, args...)
349+
open(f::Function, args...; kwargs....)
291350
292-
Apply the function `f` to the result of `open(args...)` and close the resulting file
351+
Apply the function `f` to the result of `open(args...; kwargs...)` and close the resulting file
293352
descriptor upon completion.
294353
295354
# Examples
@@ -304,8 +363,8 @@ julia> open(f->read(f, String), "myfile.txt")
304363
julia> rm("myfile.txt")
305364
```
306365
"""
307-
function open(f::Function, args...)
308-
io = open(args...)
366+
function open(f::Function, args...; kwargs...)
367+
io = open(args...; kwargs...)
309368
try
310369
f(io)
311370
finally

stdlib/REPL/src/REPL.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ function setup_interface(
831831
if repl.history_file
832832
try
833833
hist_path = find_hist_file()
834-
f = open(hist_path, true, true, true, false, false)
834+
f = open(hist_path, read=true, write=true, create=true)
835835
finalizer(replc) do replc
836836
close(f)
837837
end

0 commit comments

Comments
 (0)