Skip to content

Commit

Permalink
JRUBY-4821: Clear last error when calling #close during IO#open block
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksieger committed May 24, 2010
1 parent f9a3a41 commit 5338319
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@ public static IRubyObject open(ThreadContext context, IRubyObject recv, IRubyObj
RubyException rubyEx = re.getException();
if (rubyEx.kind_of_p(context, runtime.getStandardError()).isTrue()) {
// MRI behavior: swallow StandardErorrs
runtime.getGlobalVariables().clear("$!");
} else {
throw re;
}
Expand Down
6 changes: 5 additions & 1 deletion src/org/jruby/internal/runtime/GlobalVariables.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ public IRubyObject set(String name, IRubyObject value) {
variable.trace(value);
return result;
}


public IRubyObject clear(String name) {
return set(name, runtime.getNil());
}

public void setTraceVar(String name, RubyProc proc) {
assert name != null;
assert name.startsWith("$");
Expand Down
2 changes: 1 addition & 1 deletion src/org/jruby/runtime/load/LoadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ public void trySearch(SearchState state) {
state.library = new ClassExtensionLibrary(theClass);
} catch (Exception ee) {
state.library = null;
runtime.getGlobalVariables().set("$!", runtime.getNil());
runtime.getGlobalVariables().clear("$!");
}

// If there was a good library before, we go back to that
Expand Down
8 changes: 8 additions & 0 deletions test/test_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ def test_tty_leak
end
end

# JRUBY-4821
def test_clear_dollar_bang_after_open_block
open(__FILE__) do |io|
io.close
end
assert_nil $!
end

private
def ensure_files(*files)
files.each {|f| File.open(f, "w") {|g| g << " " } }
Expand Down

0 comments on commit 5338319

Please sign in to comment.