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

do block form of redirect_stdio #7022

Closed
vtjnash opened this issue May 29, 2014 · 10 comments
Closed

do block form of redirect_stdio #7022

vtjnash opened this issue May 29, 2014 · 10 comments
Labels
kind:julep Julia Enhancement Proposal

Comments

@vtjnash
Copy link
Sponsor Member

vtjnash commented May 29, 2014

Julip (with code!): do block form of redirect_stdio

Inspired by samuelcolvin/JuliaByExample#6 (comment)

Usage examples:

redirect_stdio() do
  println("this goes to stdout")
end
redirect_stdio(stdout=false) do
  println("this goes to old stdout")
  println(STDERR, "this goes to new stderr")
end

Implementation:

function redirect_stdio(fn; stdin::Union(Bool,IO)=true, stdout::Union(Bool,IO)=true, stderr::Union(Bool,IO)=true)
  orig = (STDIN,STDOUT,STDERR)
  stdin_out = stdout_in = stderr_in = false
  try
    if stdin === true
        stdin_out = redirect_stdin()[2]
    elseif isa(stdin,IO)
        stdin_out = redirect_stdin(stdin)[2]
    end
    if stdout === true
        stdout_in = redirect_stdout()[1]
    elseif isa(stdout,IO)
        stdout_in = redirect_stdout(stdout)[1]
    end
    if stderr === true
        stderr_in = redirect_stderr()[1]
    elseif isa(stderr,IO)
        stderr_in = redirect_stderr(stderr)[1]
    end
    fn()
  finally
    stdin_out !== false && redirect_stdin(orig[1])
    stdout_in !== false && redirect_stdout(orig[2])
    stderr_in !== false && redirect_stderr(orig[3])
    stdin === true && close(stdin_out)
    stdout === true && close(stdout_in)
    stderr === true && close(stderr_in)
  end
end

thoughts? bugs?

@vtjnash vtjnash added the julep label May 29, 2014
@kmsquire
Copy link
Member

In your example, I assume you mean

println(stderr, "this goes to new stderr")

@vtjnash
Copy link
Sponsor Member Author

vtjnash commented May 29, 2014

what? I can't have a program that even executes my sentences to find bugs? (fixed above)

@vtjnash
Copy link
Sponsor Member Author

vtjnash commented May 29, 2014

This needs to return stdin / stout / stderr

@vtjnash vtjnash added this to the 0.3 milestone Jun 18, 2014
@JeffBezanson
Copy link
Sponsor Member

Why is this a 0.3 issue?

@nolta nolta modified the milestone: 0.3 Jun 18, 2014
@vtjnash
Copy link
Sponsor Member Author

vtjnash commented Jun 19, 2014

because I can merge this PR if the concept is approved of

@JeffBezanson
Copy link
Sponsor Member

I'm not convinced.

@JeffBezanson JeffBezanson modified the milestones: 0.4, 0.3 Jun 19, 2014
@tkelman tkelman removed this from the 0.4.x milestone May 12, 2016
@tkelman
Copy link
Contributor

tkelman commented May 12, 2016

redirect_stdio doesn't seem to exist any more.

@tkelman tkelman closed this as completed May 12, 2016
@vtjnash vtjnash reopened this May 12, 2016
@vtjnash
Copy link
Sponsor Member Author

vtjnash commented May 12, 2016

it's shorthand for redirect_stderr / redirect_stdin / redirect_stdout

@tkelman
Copy link
Contributor

tkelman commented May 12, 2016

Misread, sorry. Not going to be backported and not release blocking, so not a milestone issue.

timholy added a commit that referenced this issue Aug 22, 2016
Fixes #7022. In particular, these simplify testing for warnings.
@timholy
Copy link
Sponsor Member

timholy commented Aug 23, 2016

Closed by #18165

@timholy timholy closed this as completed Aug 23, 2016
mfasi pushed a commit to mfasi/julia that referenced this issue Sep 5, 2016
Fixes JuliaLang#7022. In particular, these simplify testing for warnings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:julep Julia Enhancement Proposal
Projects
None yet
Development

No branches or pull requests

6 participants