Skip to content

Commit

Permalink
(1.0) Fix and test for JRUBY-1049, missing IO.open.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.codehaus.org/jruby/branches/jruby-1_0@4814 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
headius committed Oct 30, 2007
1 parent 76818b1 commit d79c99e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/org/jruby/RubyIO.java
Expand Up @@ -286,6 +286,7 @@ public static RubyClass createIOClass(Ruby runtime) {
// we could invoke jruby differently to allow stdin to return true
// on this. This would allow things like cgi.rb to work properly.

ioMetaClass.defineMethod("open", callbackFactory.getOptSingletonMethod("open"));
ioMetaClass.defineMethod("foreach", callbackFactory.getOptSingletonMethod("foreach"));
ioMetaClass.defineMethod("read", callbackFactory.getOptSingletonMethod("read"));
ioMetaClass.defineMethod("readlines", callbackFactory.getOptSingletonMethod("readlines"));
Expand Down Expand Up @@ -588,6 +589,23 @@ public IRubyObject initialize(IRubyObject[] args, Block unusedBlock) {
return this;
}

public static IRubyObject open(IRubyObject recv, IRubyObject[] args, Block block) {
Ruby runtime = recv.getRuntime();
RubyIO io = new RubyIO(runtime, (RubyClass) recv);
io.initialize(args, block);

if (block.isGiven()) {
try {
return block.yield(runtime.getCurrentContext(), io);
} finally {
io.close();
}
}

return io;
}


// This appears to be some windows-only mode. On a java platform this is a no-op
public IRubyObject binmode() {
return this;
Expand Down
30 changes: 30 additions & 0 deletions test/test_io.rb
Expand Up @@ -169,6 +169,36 @@ def test_file_read
f.close
end

def test_open
ensure_files @file

assert_raises(ArgumentError) { io = IO.open }

f = File.open(@file)
assert_raises(ArgumentError) { io = IO.open(f.fileno, "r", :gratuitous) }
io = IO.open(f.fileno, "r")
assert_equal(f.fileno, io.fileno)
assert(!io.closed?)
io.close
assert(io.closed?)

assert(!f.closed?)
assert_raises(Errno::EBADF) { f.close }
end

def test_open_with_block
ensure_files @file

f = File.open(@file)
IO.open(f.fileno, "r") do |io|
assert_equal(f.fileno, io.fileno)
assert(!io.closed?)
end

assert(!f.closed?)
assert_raises(Errno::EBADF) { f.close }
end

def test_delete
ensure_files @file, @file2, @file3
# Test deleting files
Expand Down

0 comments on commit d79c99e

Please sign in to comment.