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

RFC: deprecate reading functions that accept both a cmd and stdin #22762

Merged
merged 1 commit into from
Jul 12, 2017
Merged
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ Deprecated or removed
and `is_windows`, have been deprecated in favor of `Sys.islinux`, `Sys.isbsd`, `Sys.isapple`,
`Sys.isunix`, and `Sys.iswindows`, respectively ([#22182]).

* The forms of `read`, `readstring`, and `eachline` that accepted both a `Cmd` object and an
input stream are deprecated. Use e.g. `read(pipeline(stdin, cmd))` instead ([#22762]).


Julia v0.6.0 Release Notes
==========================
Expand Down
4 changes: 4 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,10 @@ end
@deprecate is_unix Sys.isunix
@deprecate is_windows Sys.iswindows

@deprecate read(cmd::AbstractCmd, stdin::Redirectable) read(pipeline(stdin, cmd))
@deprecate readstring(cmd::AbstractCmd, stdin::Redirectable) readstring(pipeline(stdin, cmd))
@deprecate eachline(cmd::AbstractCmd, stdin; chomp::Bool=true) eachline(pipeline(stdin, cmd), chomp=chomp)

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
19 changes: 5 additions & 14 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -553,16 +553,15 @@ spawn_opts_inherit(in::Redirectable=RawFD(0), out::Redirectable=RawFD(1), err::R
spawn(cmds::AbstractCmd, args...; chain::Nullable{ProcessChain}=Nullable{ProcessChain}()) =
spawn(cmds, spawn_opts_swallow(args...)...; chain=chain)

function eachline(cmd::AbstractCmd, stdin; chomp::Bool=true)
function eachline(cmd::AbstractCmd; chomp::Bool=true)
stdout = Pipe()
processes = spawn(cmd, (stdin,stdout,STDERR))
processes = spawn(cmd, (DevNull,stdout,STDERR))
close(stdout.in)
out = stdout.out
# implicitly close after reading lines, since we opened
return EachLine(out, chomp=chomp,
ondone=()->(close(out); success(processes) || pipeline_error(processes)))::EachLine
end
eachline(cmd::AbstractCmd; chomp::Bool=true) = eachline(cmd, DevNull, chomp=chomp)

# return a Process object to read-to/write-from the pipeline
"""
Expand Down Expand Up @@ -642,22 +641,14 @@ function readandwrite(cmds::AbstractCmd)
return (processes.out, processes.in, processes)
end

function read(cmd::AbstractCmd, stdin::Redirectable=DevNull)
procs = open(cmd, "r", stdin)
function read(cmd::AbstractCmd)
procs = open(cmd, "r", DevNull)
bytes = read(procs.out)
success(procs) || pipeline_error(procs)
return bytes
end

function readstring(cmd::AbstractCmd, stdin::Redirectable=DevNull)
return String(read(cmd, stdin))
end

function writeall(cmd::AbstractCmd, stdin::AbstractString, stdout::Redirectable=DevNull)
Copy link
Contributor

Choose a reason for hiding this comment

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

incorporate #19975 then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

open(cmd, "w", stdout) do io
write(io, stdin)
end
end
readstring(cmd::AbstractCmd) = String(read(cmd))

"""
run(command, args...)
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/running-external-programs.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ will attempt to store the data in the kernel's buffers while waiting for a reade
Another common solution is to separate the reader and writer of the pipeline into separate Tasks:

```julia
writer = @async writeall(process, "data")
writer = @async write(process, "data")
reader = @async do_compute(readstring(process))
wait(process)
fetch(reader)
Expand Down