From: Motohiro KOSAKI Date: 2010-02-27T01:45:54+09:00 Subject: [ruby-dev:40509] [Bug #1582] IO.new Raises Other Errors between 1.8 and 1.9 チケット #1582 が更新されました。 (by Motohiro KOSAKI) すいません、regressionは言い過ぎというか間違っていて修正不十分ぐらいが適当でした。 まず、Linuxでは以下の結果になります。 % ./ruby -e 'f1 = File.open("foo.txt", "w"); f1.puts "foo"; f2 = IO.new(f1.fileno); p f2.gets' -e:1:in `gets': not opened for reading (IOError) from -e:1:in `
' % ./ruby -e 'f1 = File.open("foo.txt", "w"); f1.puts "foo"; f2 = IO.new(f1.fi leno, "r"); p f2.gets' -e:1:in `initialize': Invalid argument (Errno::EINVAL) from -e:1:in `new' from -e:1:in `
' http://doc.okkez.net/static/191/class/IO.html をみると new(fd, mode = "r") -> IO と書いてあるので、IO.new()のmode引数のデフォルトは"r"のはずで、これは変です。 そのカラクリですが、 rb_io_initialize で #if defined(HAVE_FCNTL) && defined(F_GETFL) oflags = fcntl(fd, F_GETFL); (snip) ofmode = rb_io_oflags_fmode(oflags); if (NIL_P(vmode)) { fmode = ofmode; } というコードがあるために、引数なしの時はfd引数のアクセス権を継承する仕様に変わってしまっています。 リファレンスが正しいなら、mode引数を省略したときも単に"r"と解釈してEINVALになるべきに見えます。 これは意図的でしょうか?意図したものである場合、ドキュメントの修正をご検討くださいませ ---------------------------------------- http://redmine.ruby-lang.org/issues/show/1582 ---------------------------------------- http://redmine.ruby-lang.org