From d79c99e382e781301e4bc293e686175f4f5f7687 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 30 Oct 2007 22:15:53 +0000 Subject: [PATCH] (1.0) Fix and test for JRUBY-1049, missing IO.open. git-svn-id: http://svn.codehaus.org/jruby/branches/jruby-1_0@4814 961051c9-f516-0410-bf72-c9f7e237a7b7 --- src/org/jruby/RubyIO.java | 18 ++++++++++++++++++ test/test_io.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/org/jruby/RubyIO.java b/src/org/jruby/RubyIO.java index 45097a53296..c8d7b3d054c 100644 --- a/src/org/jruby/RubyIO.java +++ b/src/org/jruby/RubyIO.java @@ -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")); @@ -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; diff --git a/test/test_io.rb b/test/test_io.rb index 260e8a2dfb0..b975ff94ca0 100644 --- a/test/test_io.rb +++ b/test/test_io.rb @@ -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