1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

io.c: warn non-nil $,

* array.c (rb_ary_join_m): warn use of non-nil $,.

* io.c (rb_output_fs_setter): warn when set to non-nil value.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67606 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2019-04-18 21:56:55 +00:00
parent 4751caecc3
commit 3ee0648dc7
9 changed files with 41 additions and 19 deletions

View file

@ -2358,11 +2358,11 @@ rb_ary_join_m(int argc, VALUE *argv, VALUE ary)
{ {
VALUE sep; VALUE sep;
if (rb_check_arity(argc, 0, 1) == 0) { if (rb_check_arity(argc, 0, 1) == 0 || NIL_P(sep = argv[0])) {
sep = rb_output_fs;
}
else if (NIL_P(sep = argv[0])) {
sep = rb_output_fs; sep = rb_output_fs;
if (!NIL_P(sep)) {
rb_warn("$, is set to non-nil value");
}
} }
return rb_ary_join(ary, sep); return rb_ary_join(ary, sep);

12
io.c
View file

@ -7525,6 +7525,16 @@ rb_f_printf(int argc, VALUE *argv)
return Qnil; return Qnil;
} }
static void
rb_output_fs_setter(VALUE val, ID id, VALUE *var)
{
rb_str_setter(val, id, &val);
if (!NIL_P(val)) {
rb_warn("non-nil $, will be deprecated");
}
*var = val;
}
/* /*
* call-seq: * call-seq:
* ios.print -> nil * ios.print -> nil
@ -13172,7 +13182,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_str_setter); rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_output_fs_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);

View file

@ -38,11 +38,13 @@ describe "Array#join with $," do
end end
after :each do after :each do
$, = @before_separator suppress_warning {$, = @before_separator}
end end
it "separates elements with default separator when the passed separator is nil" do it "separates elements with default separator when the passed separator is nil" do
$, = "_" suppress_warning {
[1, 2, 3].join(nil).should == '1_2_3' $, = "_"
[1, 2, 3].join(nil).should == '1_2_3'
}
end end
end end

View file

@ -19,8 +19,10 @@ describe :array_join_with_default_separator, shared: true do
end end
it "returns a string formed by concatenating each String element separated by $," do it "returns a string formed by concatenating each String element separated by $," do
$, = " | " suppress_warning {
["1", "2", "3"].send(@method).should == "1 | 2 | 3" $, = " | "
["1", "2", "3"].send(@method).should == "1 | 2 | 3"
}
end end
it "attempts coercion via #to_str first" do it "attempts coercion via #to_str first" do

View file

@ -7,7 +7,9 @@ describe "Kernel#p" do
end end
after :each do after :each do
$/, $\, $, = @rs_f, @rs_b, @rs_c suppress_warning {
$/, $\, $, = @rs_f, @rs_b, @rs_c
}
end end
it "is a private method" do it "is a private method" do
@ -50,7 +52,9 @@ describe "Kernel#p" do
o = mock("Inspector Gadget") o = mock("Inspector Gadget")
o.should_receive(:inspect).any_number_of_times.and_return "Next time, Gadget, NEXT TIME!" o.should_receive(:inspect).any_number_of_times.and_return "Next time, Gadget, NEXT TIME!"
$, = " *helicopter sound*\n" suppress_warning {
$, = " *helicopter sound*\n"
}
lambda { p(o) }.should output_to_fd("Next time, Gadget, NEXT TIME!\n") lambda { p(o) }.should output_to_fd("Next time, Gadget, NEXT TIME!\n")
$\ = " *helicopter sound*\n" $\ = " *helicopter sound*\n"

View file

@ -41,18 +41,18 @@ describe "English" do
it "aliases $OFS to $," do it "aliases $OFS to $," do
original = $, original = $,
$, = "|" suppress_warning {$, = "|"}
$OFS.should_not be_nil $OFS.should_not be_nil
$OFS.should == $, $OFS.should == $,
$, = original suppress_warning {$, = original}
end end
it "aliases $OUTPUT_FIELD_SEPARATOR to $," do it "aliases $OUTPUT_FIELD_SEPARATOR to $," do
original = $, original = $,
$, = "|" suppress_warning {$, = "|"}
$OUTPUT_FIELD_SEPARATOR.should_not be_nil $OUTPUT_FIELD_SEPARATOR.should_not be_nil
$OUTPUT_FIELD_SEPARATOR.should == $, $OUTPUT_FIELD_SEPARATOR.should == $,
$, = original suppress_warning {$, = original}
end end
it "aliases $RS to $/" do it "aliases $RS to $/" do

View file

@ -159,7 +159,7 @@ describe "CApiGlobalSpecs" do
end end
after :each do after :each do
$, = @dollar_comma suppress_warning {$, = @dollar_comma}
end end
it "returns nil by default" do it "returns nil by default" do
@ -167,7 +167,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_fs.should == "foo" @f.rb_output_fs.should == "foo"
end end
end end

View file

@ -3,10 +3,14 @@ module DifferentOFS
module WithDifferentOFS module WithDifferentOFS
def setup def setup
super super
verbose, $VERBOSE = $VERBOSE, nil
@ofs, $, = $,, "-" @ofs, $, = $,, "-"
$VERBOSE = verbose
end end
def teardown def teardown
verbose, $VERBOSE = $VERBOSE, nil
$, = @ofs $, = @ofs
$VERBOSE = verbose
super super
end end
end end

View file

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