Gets seems cached the $stdin value


#1

Hi,

I am trying to make use of the jirb_swing console so that if I
encountered an exception in my code, I can launch jirb_swing for
debugging purpose. Unfortunately, the current jirb_swing hijacks the io
stream and does not return to io back to the original console after the
frame has been disposed. So what I did is I add code to save the
original io stream first and restore it after the frame has been
disposed:

stdin = JRuby.runtime.global_variables.get("$stdin")
stdout = JRuby.runtime.global_variables.get("$stdout")
stderr = JRuby.runtime.global_variables.get("$stderr")
Readline.module_eval do
  alias readline_old readline
  module_function :readline_old
end
# code start the jirb_swing

JRuby.runtime.global_variables.set("$stdin", stdin)
JRuby.runtime.global_variables.set("$stdout", stdout)
JRuby.runtime.global_variables.set("$stderr", stderr)
Readline.module_eval do
  alias readline readline_old
  module_function :readline
end

This all works fine but I noticed if I used “gets” in my jirb_swing
console, after the jirb_swing frame is disposed, if I call gets again,
it will no longer take any of my inputs. After digging through the
jruby source codes, I noticed the cause of the problem is that “gets”
implementation seems to cache the $stdin value when it is invoked the
first time. Even though I have restored my $stdin back, the gets
function is still using the old $stdin value. I came up with a short
ruby code to prove this theory:

old_stdin=$stdin
$stdin=StringIO.new("HelloWorld")
puts "type in some thing:";
puts gets;

$stdin=old_stdin
puts gets

The second gets will return “nil” instead of asking for user input. The
same code seems to work properly on Ruby.
The code is tested on JRuby 1.1.6.

Is this a bug in the jruby implementation? Any possible way I might be
able to work around it?

Thanks,

–Gary


#2

Gary L. wrote:

This all works fine but I noticed if I used “gets” in my jirb_swing
console, after the jirb_swing frame is disposed, if I call gets again,
it will no longer take any of my inputs. After digging through the
jruby source codes, I noticed the cause of the problem is that “gets”
implementation seems to cache the $stdin value when it is invoked the
first time. Even though I have restored my $stdin back, the gets
function is still using the old $stdin value. I came up with a short
ruby code to prove this theory:

Is this a bug in the jruby implementation? Any possible way I might be
able to work around it?

It definitely does sound like a bug, and you should file it with all
information you can provide. As for a workaround, I have no suggestions;
you can always check out from trunk and modify the code yourself, and of
course once we have a fix it will be on trunk and available.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email