io.c: opening external command

* io.c (rb_io_open_generic): try to open the named file as usual,
  if klass is not IO nor File, so that Errno::ENOENT will be
  raised probably.  calling on File will be same in the future.

From: Nobuyoshi Nakada <nobu@ruby-lang.org>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61320 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-12-18 07:15:07 +00:00
parent 6b718de1d6
commit d2f685eed7
2 changed files with 8 additions and 13 deletions

16
io.c
View File

@ -7088,17 +7088,11 @@ rb_io_open_generic(VALUE klass, VALUE filename, int oflags, int fmode,
const convconfig_t *convconfig, mode_t perm) const convconfig_t *convconfig, mode_t perm)
{ {
VALUE cmd; VALUE cmd;
if (!NIL_P(cmd = check_pipe_command(filename))) { const int warn = klass == rb_cFile;
if (klass != rb_cIO) { if ((warn || klass == rb_cIO) && !NIL_P(cmd = check_pipe_command(filename))) {
ID func = rb_frame_this_func(); if (warn) {
VALUE fname = rb_id2str(func); rb_warn("IO.%"PRIsVALUE" called on File to invoke external command",
static const char MSG[] = "IO.%"PRIsVALUE" called on %"PRIsVALUE" to invoke external command"; rb_id2str(rb_frame_this_func()));
if (klass == rb_cFile) {
rb_warn(MSG, fname, klass);
}
else {
rb_raise(rb_eArgError, MSG, fname, klass);
}
} }
return pipe_open_s(cmd, rb_io_oflags_modestr(oflags), fmode, convconfig); return pipe_open_s(cmd, rb_io_oflags_modestr(oflags), fmode, convconfig);
} }

View File

@ -2185,16 +2185,17 @@ class TestIO < Test::Unit::TestCase
end end
def test_read_command def test_read_command
assert_equal("foo\n", IO.read("|echo foo"))
assert_warn(/invoke external command/) do assert_warn(/invoke external command/) do
File.read("|#{EnvUtil.rubybin} -e puts") File.read("|#{EnvUtil.rubybin} -e puts")
end end
assert_warn(/invoke external command/) do assert_warn(/invoke external command/) do
File.binread("|#{EnvUtil.rubybin} -e puts") File.binread("|#{EnvUtil.rubybin} -e puts")
end end
assert_raise_with_message(ArgumentError, /invoke external command/) do assert_raise(Errno::ENOENT) do
Class.new(IO).read("|#{EnvUtil.rubybin} -e puts") Class.new(IO).read("|#{EnvUtil.rubybin} -e puts")
end end
assert_raise_with_message(ArgumentError, /invoke external command/) do assert_raise(Errno::ENOENT) do
Class.new(IO).binread("|#{EnvUtil.rubybin} -e puts") Class.new(IO).binread("|#{EnvUtil.rubybin} -e puts")
end end
end end