From 7577ec2f4b1674878263039982d63f854627b383 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Wed, 22 Nov 2017 21:29:22 -0500 Subject: [PATCH 1/2] Deprecate `readandwrite` There was a remaining todo in the code to deprecate this function. Do so. --- base/deprecated.jl | 15 +++++++++++++++ base/exports.jl | 1 - base/libgit2/gitcredential.jl | 13 ++++++------- base/process.jl | 13 ------------- test/repl.jl | 6 +++--- test/spawn.jl | 10 +++++----- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index bc7776350873c..9014d43098127 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -2801,6 +2801,21 @@ function Filesystem.tempname(uunique::UInt32) end end +""" + readandwrite(command) + +Starts running a command asynchronously, and returns a tuple (stdout,stdin,process) of the +output stream and input stream of the process, and the process object itself. +""" +function readandwrite(cmds::AbstractCmd) + depwarn("""`readandwrite(::Cmd)` is deprecated in favor of `open(::Cmd, \"r+\"). + You may read/write the returned process object for access to stdio.""", + :readandwrite) + processes = open(cmds, "r+") + return (processes.out, processes.in, processes) +end +export readandwrite + # END 0.7 deprecations # BEGIN 1.0 deprecations diff --git a/base/exports.jl b/base/exports.jl index b7734faeefdcd..cf80f9313d23e 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1072,7 +1072,6 @@ export kill, process_exited, process_running, - readandwrite, run, setenv, spawn, diff --git a/base/libgit2/gitcredential.jl b/base/libgit2/gitcredential.jl index 0c47a05dddd3b..942d3e8792a79 100644 --- a/base/libgit2/gitcredential.jl +++ b/base/libgit2/gitcredential.jl @@ -168,17 +168,16 @@ end function run!(helper::GitCredentialHelper, operation::AbstractString, cred::GitCredential) cmd = `$(helper.cmd) $operation` - output, input, p = readandwrite(cmd) + p = open(cmd, "r+") # Provide the helper with the credential information we know - write(input, cred) - write(input, "\n") - t = @async close(input) + write(p, cred) + write(p, "\n") + t = @async close(p.in) # Process the response from the helper - Base.read!(output, cred) - close(output) - wait(t) + Base.read!(p, cred) + wait(p) return cred end diff --git a/base/process.jl b/base/process.jl index ca2ebb9ca5c3d..7b5dacf02a7d6 100644 --- a/base/process.jl +++ b/base/process.jl @@ -636,19 +636,6 @@ function open(f::Function, cmds::AbstractCmd, args...) return ret end -# TODO: deprecate this - -""" - readandwrite(command) - -Starts running a command asynchronously, and returns a tuple (stdout,stdin,process) of the -output stream and input stream of the process, and the process object itself. -""" -function readandwrite(cmds::AbstractCmd) - processes = open(cmds, "r+") - return (processes.out, processes.in, processes) -end - function read(cmd::AbstractCmd) procs = open(cmd, "r", DevNull) bytes = read(procs.out) diff --git a/test/repl.jl b/test/repl.jl index ae39f0cf2b9a6..31d0867802fcf 100644 --- a/test/repl.jl +++ b/test/repl.jl @@ -669,9 +669,9 @@ let exename = Base.julia_cmd() end # Test stream mode - outs, ins, p = readandwrite(`$exename --startup-file=no -q`) - write(ins, "1\nquit()\n") - @test read(outs, String) == "1\n" + p = open(`$exename --startup-file=no -q`, "r+") + write(p, "1\nquit()\n") + @test read(p, String) == "1\n" end # let exename # issue #19864: diff --git a/test/spawn.jl b/test/spawn.jl index 4921b2d0cb072..98cfe3dc8da70 100644 --- a/test/spawn.jl +++ b/test/spawn.jl @@ -138,16 +138,16 @@ let pathA = readchomp(setenv(`$shcmd -c "pwd -P"`;dir="..")), end end -let str = "", stdin, stdout, proc, str2, file +let str = "", proc, str2, file for i = 1:1000 str = "$str\n $(randstring(10))" end # Here we test that if we close a stream with pending writes, we don't lose the writes. - stdout, stdin, proc = readandwrite(`$catcmd -`) - write(stdin, str) - close(stdin) - str2 = read(stdout, String) + proc = open(`$catcmd -`, "r+") + write(proc, str) + close(proc.in) + str2 = read(proc, String) @test str2 == str # This test hangs if the end-of-run-walk-across-uv-streams calls shutdown on a stream that is shutting down. From 1755a3cf73d021746800b22099a60d6b71ad1c93 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Wed, 22 Nov 2017 21:29:55 -0500 Subject: [PATCH 2/2] Add a docstring showing supported usage of `Pipe()` It was brought up on discourse that this function doesn't have any docs. --- base/stream.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/base/stream.jl b/base/stream.jl index cb800a0728dc2..6f68418c74948 100644 --- a/base/stream.jl +++ b/base/stream.jl @@ -574,6 +574,22 @@ mutable struct Pipe <: AbstractPipe in::PipeEndpoint # writable out::PipeEndpoint # readable end + +""" +Construct an uninitialized Pipe object. + +The appropriate end of the pipe will be automatically initialized if +the object is used in process spawning. This can be useful to easily +obtain references in process pipelines, e.g.: + +``` +julia> err = Pipe() + +# After this `err` will be initialized and you may read `foo`'s +# stderr from the `err` pipe. +julia> spawn(pipeline(pipeline(`foo`, stderr=err), `cat`)) +``` +""" Pipe() = Pipe(PipeEndpoint(), PipeEndpoint()) pipe_reader(p::Pipe) = p.out pipe_writer(p::Pipe) = p.in