Skip to content

Commit 996cae6

Browse files
nobuhsbt
authored andcommitted
Depricate IO operation with |
1 parent f4e0178 commit 996cae6

File tree

3 files changed

+11
-98
lines changed

3 files changed

+11
-98
lines changed

io.c

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8223,21 +8223,6 @@ rb_io_s_sysopen(int argc, VALUE *argv, VALUE _)
82238223
return INT2NUM(fd);
82248224
}
82258225

8226-
static VALUE
8227-
check_pipe_command(VALUE filename_or_command)
8228-
{
8229-
char *s = RSTRING_PTR(filename_or_command);
8230-
long l = RSTRING_LEN(filename_or_command);
8231-
char *e = s + l;
8232-
int chlen;
8233-
8234-
if (rb_enc_ascget(s, e, &chlen, rb_enc_get(filename_or_command)) == '|') {
8235-
VALUE cmd = rb_str_new(s+chlen, l-chlen);
8236-
return cmd;
8237-
}
8238-
return Qnil;
8239-
}
8240-
82418226
/*
82428227
* call-seq:
82438228
* open(path, mode = 'r', perm = 0666, **opts) -> io or nil
@@ -8283,13 +8268,7 @@ rb_f_open(int argc, VALUE *argv, VALUE _)
82838268
redirect = TRUE;
82848269
}
82858270
else {
8286-
VALUE cmd = check_pipe_command(tmp);
8287-
if (!NIL_P(cmd)) {
8288-
// TODO: when removed in 4.0, update command_injection.rdoc
8289-
rb_warn_deprecated_to_remove_at(4.0, "Calling Kernel#open with a leading '|'", "IO.popen");
8290-
argv[0] = cmd;
8291-
return rb_io_s_popen(argc, argv, rb_cIO);
8292-
}
8271+
argv[0] = tmp;
82938272
}
82948273
}
82958274
}
@@ -8308,16 +8287,8 @@ static VALUE
83088287
rb_io_open_generic(VALUE klass, VALUE filename, int oflags, enum rb_io_mode fmode,
83098288
const struct rb_io_encoding *convconfig, mode_t perm)
83108289
{
8311-
VALUE cmd;
8312-
if (klass == rb_cIO && !NIL_P(cmd = check_pipe_command(filename))) {
8313-
// TODO: when removed in 4.0, update command_injection.rdoc
8314-
rb_warn_deprecated_to_remove_at(4.0, "IO process creation with a leading '|'", "IO.popen");
8315-
return pipe_open_s(cmd, rb_io_oflags_modestr(oflags), fmode, convconfig);
8316-
}
8317-
else {
8318-
return rb_file_open_generic(io_alloc(klass), filename,
8319-
oflags, fmode, convconfig, perm);
8320-
}
8290+
return rb_file_open_generic(io_alloc(klass), filename,
8291+
oflags, fmode, convconfig, perm);
83218292
}
83228293

83238294
static VALUE

test/ruby/test_io.rb

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,36 +2619,15 @@ def o.to_open(kw); kw; end
26192619
assert_equal({:a=>1}, open(o, {a: 1}))
26202620
end
26212621

2622-
def test_open_pipe
2623-
assert_deprecated_warning(/Kernel#open with a leading '\|'/) do # https://bugs.ruby-lang.org/issues/19630
2624-
open("|" + EnvUtil.rubybin, "r+") do |f|
2625-
f.puts "puts 'foo'"
2626-
f.close_write
2627-
assert_equal("foo\n", f.read)
2628-
end
2629-
end
2630-
end
2622+
def test_path_with_pipe
2623+
mkcdtmpdir do
2624+
cmd = "|echo foo"
2625+
assert_file.not_exist?(cmd)
26312626

2632-
def test_read_command
2633-
assert_deprecated_warning(/IO process creation with a leading '\|'/) do # https://bugs.ruby-lang.org/issues/19630
2634-
assert_equal("foo\n", IO.read("|echo foo"))
2635-
end
2636-
assert_raise(Errno::ENOENT, Errno::EINVAL) do
2637-
File.read("|#{EnvUtil.rubybin} -e puts")
2638-
end
2639-
assert_raise(Errno::ENOENT, Errno::EINVAL) do
2640-
File.binread("|#{EnvUtil.rubybin} -e puts")
2641-
end
2642-
assert_raise(Errno::ENOENT, Errno::EINVAL) do
2643-
Class.new(IO).read("|#{EnvUtil.rubybin} -e puts")
2644-
end
2645-
assert_raise(Errno::ENOENT, Errno::EINVAL) do
2646-
Class.new(IO).binread("|#{EnvUtil.rubybin} -e puts")
2647-
end
2648-
assert_raise(Errno::ESPIPE) do
2649-
assert_deprecated_warning(/IO process creation with a leading '\|'/) do # https://bugs.ruby-lang.org/issues/19630
2650-
IO.read("|#{EnvUtil.rubybin} -e 'puts :foo'", 1, 1)
2651-
end
2627+
pipe_errors = [Errno::ENOENT, Errno::EINVAL, Errno::EACCES, Errno::EPERM]
2628+
assert_raise(*pipe_errors) { open(cmd, "r+") }
2629+
assert_raise(*pipe_errors) { IO.read(cmd) }
2630+
assert_raise(*pipe_errors) { IO.foreach(cmd) {|x| assert false } }
26522631
end
26532632
end
26542633

@@ -2853,19 +2832,6 @@ def test_reopen_ivar
28532832
end
28542833

28552834
def test_foreach
2856-
a = []
2857-
2858-
assert_deprecated_warning(/IO process creation with a leading '\|'/) do # https://bugs.ruby-lang.org/issues/19630
2859-
IO.foreach("|" + EnvUtil.rubybin + " -e 'puts :foo; puts :bar; puts :baz'") {|x| a << x }
2860-
end
2861-
assert_equal(["foo\n", "bar\n", "baz\n"], a)
2862-
2863-
a = []
2864-
assert_deprecated_warning(/IO process creation with a leading '\|'/) do # https://bugs.ruby-lang.org/issues/19630
2865-
IO.foreach("|" + EnvUtil.rubybin + " -e 'puts :zot'", :open_args => ["r"]) {|x| a << x }
2866-
end
2867-
assert_equal(["zot\n"], a)
2868-
28692835
make_tempfile {|t|
28702836
a = []
28712837
IO.foreach(t.path) {|x| a << x }

test/ruby/test_io_m17n.rb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,30 +1395,6 @@ def test_popenv_r_enc_enc_in_opt2
13951395
}
13961396
end
13971397

1398-
def test_open_pipe_r_enc
1399-
EnvUtil.suppress_warning do # https://bugs.ruby-lang.org/issues/19630
1400-
open("|#{EnvUtil.rubybin} -e 'putc 255'", "r:ascii-8bit") {|f|
1401-
assert_equal(Encoding::ASCII_8BIT, f.external_encoding)
1402-
assert_equal(nil, f.internal_encoding)
1403-
s = f.read
1404-
assert_equal(Encoding::ASCII_8BIT, s.encoding)
1405-
assert_equal("\xff".force_encoding("ascii-8bit"), s)
1406-
}
1407-
end
1408-
end
1409-
1410-
def test_open_pipe_r_enc2
1411-
EnvUtil.suppress_warning do # https://bugs.ruby-lang.org/issues/19630
1412-
open("|#{EnvUtil.rubybin} -e 'putc \"\\u3042\"'", "r:UTF-8") {|f|
1413-
assert_equal(Encoding::UTF_8, f.external_encoding)
1414-
assert_equal(nil, f.internal_encoding)
1415-
s = f.read
1416-
assert_equal(Encoding::UTF_8, s.encoding)
1417-
assert_equal("\u3042", s)
1418-
}
1419-
end
1420-
end
1421-
14221398
def test_s_foreach_enc
14231399
with_tmpdir {
14241400
generate_file("t", "\xff")

0 commit comments

Comments
 (0)