[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

I want to redirect stderr to StringIO.

ErMaker

5/20/2009 4:31:00 AM

[Note: parts of this message were removed to make it a legal post.]

>> IO.new(2).reopen(StringIO.new)
TypeError: can't convert StringIO into String
from (irb):1:in `reopen'
from (irb):1
>> IO.new(2).reopen(IO.new(1))
=> #<IO:0x2a81630>

StringIO also do as IO, but IO#reopen fails.

I want to redirect stderr to StringIO, but it doesn't work.

12 Answers

orionca

6/15/2008 4:32:00 AM

0

On Sat, 14 Jun 2008 20:51:52 -0700, "Chip Reeves"
<creeves@socal.rr.com> wrote:

>
><orionca@earthlink.net> wrote in message
>news:32t854hfrnsbagcf8ld2o66dc8qi53bi6f@4ax.com...
>> On Sat, 14 Jun 2008 18:25:29 -0400, "Enviromentally Whacko" <whacko
>> @Democrap.org> wrote:
>>
>>>http://www.breitbart.tv/html/1...
>>
>> Interesting. The company didn't say they were going to stop making it
>> in their statement, either.
>>
>> Oh, well. Anyone who's ever compared GW Bush to a chimp can't
>> credibly complain about this.
>
>Uh, sure they can. If you don't see a difference between calling a white man
>a monkey and calling a black man one, try it in a white neighborhood and
>then a black one.
>
>You have to explain everyting to these dumb as dirt rrepugs.

So you're saying blacks are stupid thugs with poor impulse control?
You're quite a racist, did you know that?
--
"On this Memorial Day, as our nation honors its unbroken
line of fallen heroes -- and I see many of them in the
audience here today -- "

- from Obama's 2008 Memorial Day address,
aka the "I See Dead People!" speech.

Update: "I am not going to denounce this church [Trinity],
it is not a church worthy of denouncing."

- A clearly rattled Obama, asked by a reporter if he was
going to criticize comments at his church that finally
forced him to resign from it. This guy is supposed to be
a brilliant orator? See the video at

http://gatewaypundit.blogspot.com/2008/05/obama-gives-up-trinity-c...

Patriot Games

6/15/2008 1:52:00 PM

0

On Sat, 14 Jun 2008 20:51:52 -0700, "Chip Reeves"
<creeves@socal.rr.com> wrote:
>Uh, sure they can. If you don't see a difference between calling a white man
>a monkey and calling a black man one, try it in a white neighborhood and
>then a black one.

Explain that difference.

Kickin' Ass and Takin' Names

6/15/2008 2:03:00 PM

0

On Jun 15, 6:42 am, Patrick Henry <phen...@revolution.now> wrote:
> On Sat, 14 Jun 2008 21:32:18 -0700, orionca wrote:
> > On Sat, 14 Jun 2008 20:51:52 -0700, "Chip Reeves" <cree...@socal.rr.com>
> > wrote:
>
> >><orio...@earthlink.net> wrote in message
> >>news:32t854hfrnsbagcf8ld2o66dc8qi53bi6f@4ax.com...
> >>> On Sat, 14 Jun 2008 18:25:29 -0400, "Enviromentally Whacko" <whacko
> >>> @Democrap.org> wrote:
>
> >>>>http://www.breitbart.tv/html/1...
>
> >>> Interesting.  The company didn't say they were going to stop making it
> >>> in their statement, either.
>
> >>> Oh, well.  Anyone who's ever compared GW Bush to a chimp can't
> >>> credibly complain about this.
>
> >>Uh, sure they can. If you don't see a difference between calling a white
> >>man a monkey and calling a black man one, try it in a white neighborhood
> >>and then a black one.
>
> >>You have to explain everyting to these dumb as dirt rrepugs.
>
> > So you're saying blacks are stupid thugs with poor impulse control?
> > You're quite a racist, did you know that?
>
> Are all conservatives ignorant racist fucks?- Hide quoted text -

All the ones I know are.

Eric Hodel

5/20/2009 4:39:00 AM

0

On May 19, 2009, at 21:30, ErMaker wrote:

>>> IO.new(2).reopen(StringIO.new)
> TypeError: can't convert StringIO into String
> from (irb):1:in `reopen'
> from (irb):1
>>> IO.new(2).reopen(IO.new(1))
> => #<IO:0x2a81630>
>
> StringIO also do as IO, but IO#reopen fails.
>
> I want to redirect stderr to StringIO, but it doesn't work.

Always use reopen as a last resort.

http://blog.segment7.net/articles/2006/08/17/stdout...

Robert Klemme

5/20/2009 6:14:00 AM

0

2009/5/20 Eric Hodel <drbrain@segment7.net>:
> On May 19, 2009, at 21:30, ErMaker wrote:
>
>>>> IO.new(2).reopen(StringIO.new)
>>
>> TypeError: can't convert StringIO into String
>> =A0 =A0 =A0 from (irb):1:in `reopen'
>> =A0 =A0 =A0 from (irb):1
>>>>
>>>> IO.new(2).reopen(IO.new(1))
>>
>> =3D> #<IO:0x2a81630>
>>
>> StringIO also do as IO, but IO#reopen fails.
>>
>> I want to redirect stderr to StringIO, but it doesn't work.
>
> Always use reopen as a last resort.
>
> http://blog.segment7.net/articles/2006/08/17/stdout...

I am not sure I agree. Actually, if you want the redirection to be
permanent for sub processes you have to use $stdout.reopen. And this
is not a "last resort" but the proper solution.

Btw, in 1.8 there is also $defout - I believe _that- is the stream
used by Kernel#puts and the like in those versions:

08:13:00 ~$ ruby -r stringio -e 'puts 1; $defout=3DStringIO.new; puts 2'
1
-e:1: warning: $defout is obsolete; use $stdout instead
08:13:30 ~$

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Brian Candler

5/20/2009 10:30:00 AM

0

ErMaker wrote:
>>> IO.new(2).reopen(StringIO.new)
> TypeError: can't convert StringIO into String
> from (irb):1:in `reopen'
> from (irb):1
>>> IO.new(2).reopen(IO.new(1))
> => #<IO:0x2a81630>
>
> StringIO also do as IO, but IO#reopen fails.
>
> I want to redirect stderr to StringIO, but it doesn't work.

The problem is that a StringIO cannot exist in the O/S's file descriptor
table. STDERR.reopen(...) at the low level does a dup() or dup2() to
copy one file descriptor to another.

You have two options:

(1) $stderr = StringIO.new

Then any program which writes to $stderr will be fine. But anything
which writes to STDERR will still go to file descriptor 2 (that is, your
process' stderr file)

(2) reopen STDERR with something which exists in the O/S file descriptor
table: e.g. a file or a pipe.

So for example, you can fork and connect a pipe to the child process'
STDERR, and then in the parent have a thread which reads from this and
copies all data read into a StringIO object. Anything written to STDERR
in the child process will be collected into the StringIO via the pipe.

See open3.rb in the standard library for an example of how to do this.
--
Posted via http://www.ruby-....

Eric Hodel

5/20/2009 7:01:00 PM

0

On May 19, 2009, at 23:14, Robert Klemme wrote:
> 2009/5/20 Eric Hodel <drbrain@segment7.net>:
>> On May 19, 2009, at 21:30, ErMaker wrote:
>>
>>>>> IO.new(2).reopen(StringIO.new)
>>>
>>> TypeError: can't convert StringIO into String
>>> from (irb):1:in `reopen'
>>> from (irb):1
>>>>>
>>>>> IO.new(2).reopen(IO.new(1))
>>>
>>> => #<IO:0x2a81630>
>>>
>>> StringIO also do as IO, but IO#reopen fails.
>>>
>>> I want to redirect stderr to StringIO, but it doesn't work.
>>
>> Always use reopen as a last resort.
>>
>> http://blog.segment7.net/articles/2006/08/17/stdout...
>
> I am not sure I agree. Actually, if you want the redirection to be
> permanent for sub processes you have to use $stdout.reopen. And this
> is not a "last resort" but the proper solution.

Most of the time people capture IO from sub processes via Kernel#` or
IO::popen instead of handling the sub process themselves. Ruby's nice
features make use of #reopen an exceptional circumstance, but there
are times where it's appropriate.

> Btw, in 1.8 there is also $defout - I believe _that- is the stream
> used by Kernel#puts and the like in those versions:
>
> 08:13:00 ~$ ruby -r stringio -e 'puts 1; $defout=StringIO.new; puts 2'
> 1
> -e:1: warning: $defout is obsolete; use $stdout instead

This warning says otherwise. They happen to be the same object though:

$ ruby -e 'p $stdout.object_id, $defout.object_id'
97260
97260


Robert Klemme

5/21/2009 8:31:00 AM

0

On 20.05.2009 21:01, Eric Hodel wrote:
> On May 19, 2009, at 23:14, Robert Klemme wrote:
>> 2009/5/20 Eric Hodel <drbrain@segment7.net>:

>>> http://blog.segment7.net/articles/2006/08/17/stdout...
>> I am not sure I agree. Actually, if you want the redirection to be
>> permanent for sub processes you have to use $stdout.reopen. And this
>> is not a "last resort" but the proper solution.
>
> Most of the time people capture IO from sub processes via Kernel#` or
> IO::popen instead of handling the sub process themselves. Ruby's nice
> features make use of #reopen an exceptional circumstance, but there
> are times where it's appropriate.

You do not make that point on the blog entry which provoked my remark.
(I wanted to place it there but comments are closed.)

>> Btw, in 1.8 there is also $defout - I believe _that- is the stream
>> used by Kernel#puts and the like in those versions:
>>
>> 08:13:00 ~$ ruby -r stringio -e 'puts 1; $defout=StringIO.new; puts 2'
>> 1
>> -e:1: warning: $defout is obsolete; use $stdout instead
>
> This warning says otherwise.

Please do not let yourself be distracted by the warning. The crucial
point is that after $defout has been reassigned the "2" does not appear
any more on the screen => this is the object which is used by #puts.

> They happen to be the same object though:
>
> $ ruby -e 'p $stdout.object_id, $defout.object_id'
> 97260
> 97260

Yes - until you reassign any of them. :-)

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Brian Candler

5/22/2009 7:35:00 AM

0

Robert Klemme wrote:
> Please do not let yourself be distracted by the warning. The crucial
> point is that after $defout has been reassigned the "2" does not appear
> any more on the screen => this is the object which is used by #puts.

Just do what the warning says, and use $stdout instead of $defout:

$ ruby -r stringio -e 'puts 1; $stdout=StringIO.new; puts 2'
1
$

However this doesn't help the OP who wanted to redirect STDERR. You can
redirect $stderr, but lots of code writes to STDERR instead of $stderr.
--
Posted via http://www.ruby-....

Eric Hodel

5/22/2009 3:58:00 PM

0

On May 21, 2009, at 21:27, Robert Klemme wrote:
> On 20.05.2009 21:01, Eric Hodel wrote:
>> On May 19, 2009, at 23:14, Robert Klemme wrote:
>>> Btw, in 1.8 there is also $defout - I believe _that- is the stream
>>> used by Kernel#puts and the like in those versions:
>>>
>>> 08:13:00 ~$ ruby -r stringio -e 'puts 1; $defout=StringIO.new;
>>> puts 2'
>>> 1
>>> -e:1: warning: $defout is obsolete; use $stdout instead
>> This warning says otherwise.
>
> Please do not let yourself be distracted by the warning. The
> crucial point is that after $defout has been reassigned the "2" does
> not appear any more on the screen => this is the object which is
> used by #puts.

Other than the warning, the behavior is the same with $stdout and
$defout as they are the same variable.

>> They happen to be the same object though:
>> $ ruby -e 'p $stdout.object_id, $defout.object_id'
>> 97260
>> 97260
>
> Yes - until you reassign any of them. :-)

If you reassign either of them they're still the same object:

$ ruby -r stringio -e '$stdout=StringIO.new; $stderr.puts [$stdout,
$defout].inspect'
[#<StringIO:0x28a50>, #<StringIO:0x28a50>]

From io.c:

rb_define_hooked_variable("$stdout", &rb_stdout, 0, stdout_setter);
[...]
rb_define_hooked_variable("$defout", &rb_stdout, 0, defout_setter);