Warn non-nil $\ [Feature #14240]

This commit is contained in:
Nobuyoshi Nakada 2020-01-20 17:53:46 +09:00
parent 588a86e32c
commit 6298ec2875
No known key found for this signature in database
GPG Key ID: 4BC7D6DF58D8DF60
Notes: git 2020-02-23 16:49:04 +09:00
8 changed files with 37 additions and 26 deletions

8
io.c
View File

@ -7570,11 +7570,11 @@ rb_f_printf(int argc, VALUE *argv, VALUE _)
} }
static void static void
rb_output_fs_setter(VALUE val, ID id, VALUE *var) deprecated_str_setter(VALUE val, ID id, VALUE *var)
{ {
rb_str_setter(val, id, &val); rb_str_setter(val, id, &val);
if (!NIL_P(val)) { if (!NIL_P(val)) {
rb_warn_deprecated("`$,'", NULL); rb_warn_deprecated("`%s'", NULL, rb_id2name(id));
} }
*var = val; *var = val;
} }
@ -13282,7 +13282,7 @@ Init_IO(void)
rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1); rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
rb_output_fs = Qnil; rb_output_fs = Qnil;
rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_output_fs_setter); rb_define_hooked_variable("$,", &rb_output_fs, 0, deprecated_str_setter);
rb_default_rs = rb_fstring_lit("\n"); /* avoid modifying RS_default */ rb_default_rs = rb_fstring_lit("\n"); /* avoid modifying RS_default */
rb_gc_register_mark_object(rb_default_rs); rb_gc_register_mark_object(rb_default_rs);
@ -13290,7 +13290,7 @@ Init_IO(void)
rb_output_rs = Qnil; rb_output_rs = Qnil;
rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter); rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter);
rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter); rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter);
rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter); rb_define_hooked_variable("$\\", &rb_output_rs, 0, deprecated_str_setter);
rb_define_virtual_variable("$_", get_LAST_READ_LINE, set_LAST_READ_LINE); rb_define_virtual_variable("$_", get_LAST_READ_LINE, set_LAST_READ_LINE);

View File

@ -4,12 +4,12 @@ require_relative 'fixtures/classes'
describe IO, "#print" do describe IO, "#print" do
before :each do before :each do
@old_separator = $\ @old_separator = $\
$\ = '->' suppress_warning {$\ = '->'}
@name = tmp("io_print") @name = tmp("io_print")
end end
after :each do after :each do
$\ = @old_separator suppress_warning {$\ = @old_separator}
rm_r @name rm_r @name
end end

View File

@ -57,10 +57,14 @@ describe "Kernel#p" do
} }
-> { p(o) }.should output_to_fd("Next time, Gadget, NEXT TIME!\n") -> { p(o) }.should output_to_fd("Next time, Gadget, NEXT TIME!\n")
$\ = " *helicopter sound*\n" suppress_warning {
$\ = " *helicopter sound*\n"
}
-> { p(o) }.should output_to_fd("Next time, Gadget, NEXT TIME!\n") -> { p(o) }.should output_to_fd("Next time, Gadget, NEXT TIME!\n")
$/ = " *helicopter sound*\n" suppress_warning {
$/ = " *helicopter sound*\n"
}
-> { p(o) }.should output_to_fd("Next time, Gadget, NEXT TIME!\n") -> { p(o) }.should output_to_fd("Next time, Gadget, NEXT TIME!\n")
end end

View File

@ -67,18 +67,18 @@ describe "English" do
it "aliases $ORS to $\\" do it "aliases $ORS to $\\" do
original = $\ original = $\
$\ = "\t" suppress_warning {$\ = "\t"}
$ORS.should_not be_nil $ORS.should_not be_nil
$ORS.should == $\ $ORS.should == $\
$\ = original suppress_warning {$\ = original}
end end
it "aliases $OUTPUT_RECORD_SEPARATOR to $\\" do it "aliases $OUTPUT_RECORD_SEPARATOR to $\\" do
original = $\ original = $\
$\ = "\t" suppress_warning {$\ = "\t"}
$OUTPUT_RECORD_SEPARATOR.should_not be_nil $OUTPUT_RECORD_SEPARATOR.should_not be_nil
$OUTPUT_RECORD_SEPARATOR.should == $\ $OUTPUT_RECORD_SEPARATOR.should == $\
$\ = original suppress_warning {$\ = original}
end end
it "aliases $INPUT_LINE_NUMBER to $." do it "aliases $INPUT_LINE_NUMBER to $." do

View File

@ -39,13 +39,14 @@ describe "StringIO#print" do
end end
it "honors the output record separator global" do it "honors the output record separator global" do
old_rs, $\ = $\, 'x' old_rs = $\
suppress_warning {$\ = 'x'}
begin begin
@io.print(5, 6, 7, 8) @io.print(5, 6, 7, 8)
@io.string.should == '5678xle' @io.string.should == '5678xle'
ensure ensure
$\ = old_rs suppress_warning {$\ = old_rs}
end end
end end
@ -58,13 +59,14 @@ describe "StringIO#print" do
end end
it "correctly updates the current position when honoring the output record separator global" do it "correctly updates the current position when honoring the output record separator global" do
old_rs, $\ = $\, 'x' old_rs = $\
suppress_warning {$\ = 'x'}
begin begin
@io.print(5, 6, 7, 8) @io.print(5, 6, 7, 8)
@io.pos.should eql(5) @io.pos.should eql(5)
ensure ensure
$\ = old_rs suppress_warning {$\ = old_rs}
end end
end end
end end

View File

@ -30,11 +30,12 @@ describe "StringIO#puts when passed an Array" do
it "does not honor the global output record separator $\\" do it "does not honor the global output record separator $\\" do
begin begin
old_rs, $\ = $\, "test" old_rs = $\
suppress_warning {$\ = "test"}
@io.puts([1, 2, 3, 4]) @io.puts([1, 2, 3, 4])
@io.string.should == "1\n2\n3\n4\n" @io.string.should == "1\n2\n3\n4\n"
ensure ensure
$\ = old_rs suppress_warning {$\ = old_rs}
end end
end end
@ -68,11 +69,12 @@ describe "StringIO#puts when passed 1 or more objects" do
it "does not honor the global output record separator $\\" do it "does not honor the global output record separator $\\" do
begin begin
old_rs, $\ = $\, "test" old_rs = $\
suppress_warning {$\ = "test"}
@io.puts(1, 2, 3, 4) @io.puts(1, 2, 3, 4)
@io.string.should == "1\n2\n3\n4\n" @io.string.should == "1\n2\n3\n4\n"
ensure ensure
$\ = old_rs suppress_warning {$\ = old_rs}
end end
end end
@ -117,11 +119,12 @@ describe "StringIO#puts when passed no arguments" do
it "does not honor the global output record separator $\\" do it "does not honor the global output record separator $\\" do
begin begin
old_rs, $\ = $\, "test" old_rs = $\
suppress_warning {$\ = "test"}
@io.puts @io.puts
@io.string.should == "\n" @io.string.should == "\n"
ensure ensure
$\ = old_rs suppress_warning {$\ = old_rs}
end end
end end
end end

View File

@ -140,7 +140,7 @@ describe "CApiGlobalSpecs" do
end end
after :each do after :each do
$\ = @dollar_backslash suppress_warning {$\ = @dollar_backslash}
end end
it "returns nil by default" do it "returns nil by default" do
@ -148,7 +148,7 @@ describe "CApiGlobalSpecs" do
end end
it "returns the value of $\\" do it "returns the value of $\\" do
$\ = "foo" suppress_warning {$\ = "foo"}
@f.rb_output_rs.should == "foo" @f.rb_output_rs.should == "foo"
end end
end end

View File

@ -2568,8 +2568,10 @@ class TestIO < Test::Unit::TestCase
end end
def test_print_separators def test_print_separators
EnvUtil.suppress_warning {$, = ':'} EnvUtil.suppress_warning {
$\ = "\n" $, = ':'
$\ = "\n"
}
pipe(proc do |w| pipe(proc do |w|
w.print('a') w.print('a')
EnvUtil.suppress_warning {w.print('a','b','c')} EnvUtil.suppress_warning {w.print('a','b','c')}