ruby-changes:46376
From: nobu <ko1@a...>
Date: Thu, 27 Apr 2017 05:13:11 +0900 (JST)
Subject: [ruby-changes:46376] nobu:r58490 (trunk): error.c: send as a single string
nobu 2017-04-27 05:13:07 +0900 (Thu, 27 Apr 2017) New Revision: 58490 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58490 Log: error.c: send as a single string * error.c (rb_warn_m): send the arguments as a single string concatenated with a newline, so it can be filtered easily. [ruby-core:80875] [Feature #12944] Modified files: trunk/error.c trunk/test/ruby/test_exception.rb Index: test/ruby/test_exception.rb =================================================================== --- test/ruby/test_exception.rb (revision 58489) +++ test/ruby/test_exception.rb (revision 58490) @@ -973,6 +973,10 @@ $stderr = $stdout; raise "\x82\xa0"') do https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L973 def test_warning_warn warning = capture_warning_warn {@a} assert_match(/instance variable @a not initialized/, warning[0]) + + assert_equal(["a\nz\n"], capture_warning_warn {warn "a\n", "z"}) + assert_equal([], capture_warning_warn {warn}) + assert_equal(["\n"], capture_warning_warn {warn ""}) end def test_warning_warn_invalid_argument Index: error.c =================================================================== --- error.c (revision 58489) +++ error.c (revision 58490) @@ -48,6 +48,7 @@ VALUE rb_eEAGAIN; https://github.com/ruby/ruby/blob/trunk/error.c#L48 VALUE rb_eEWOULDBLOCK; VALUE rb_eEINPROGRESS; VALUE rb_mWarning; +VALUE rb_cWarningBuffer; static ID id_warn; @@ -279,6 +280,13 @@ rb_enc_warning(rb_encoding *enc, const c https://github.com/ruby/ruby/blob/trunk/error.c#L280 } #endif +static inline int +end_with_asciichar(VALUE str, int c) +{ + return RB_TYPE_P(str, T_STRING) && + rb_str_end_with_asciichar(str, c); +} + /* * call-seq: * warn(msg, ...) -> nil @@ -301,7 +309,14 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/error.c#L309 rb_warn_m(int argc, VALUE *argv, VALUE exc) { if (!NIL_P(ruby_verbose) && argc > 0) { - rb_io_puts(argc, argv, rb_mWarning); + VALUE str = argv[0]; + if (argc > 1 || !end_with_asciichar(str, '\n')) { + str = rb_str_tmp_new(0); + RBASIC_SET_CLASS(str, rb_cWarningBuffer); + rb_io_puts(argc, argv, str); + RBASIC_SET_CLASS(str, rb_cString); + } + rb_write_warning_str(str); } return Qnil; } @@ -2175,9 +2190,11 @@ Init_Exception(void) https://github.com/ruby/ruby/blob/trunk/error.c#L2190 rb_mWarning = rb_define_module("Warning"); rb_define_method(rb_mWarning, "warn", rb_warning_s_warn, 1); - rb_define_method(rb_mWarning, "write", rb_warning_s_warn, 1); rb_extend_object(rb_mWarning, rb_mWarning); + rb_cWarningBuffer = rb_define_class_under(rb_mWarning, "buffer", rb_cString); + rb_define_method(rb_cWarningBuffer, "write", rb_str_append, 1); + rb_define_global_function("warn", rb_warn_m, -1); id_new = rb_intern_const("new"); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/