From fbacf0753c7168e76c079d10b8d0a10546ce56a8 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Tue, 13 Dec 2016 08:21:24 -0800 Subject: [PATCH] Change Kernel#warn to call Warning.warn This allows Warning.warn to filter/process warning messages generated by Kernel#warn. Currently, Warning.warn can only handle messages generated by the rb_warn/rb_warning C functions. The Kernel#warn API is different than the Warning.warn API, this tries to get similar behavior, but there may be corner cases where the behavior is different. This makes str_end_with_asciichar in io.c no longer static so it can be called from error.c. --- error.c | 19 ++++++++++++++----- io.c | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/error.c b/error.c index e03a4ee..2eaf341 100644 --- a/error.c +++ b/error.c @@ -38,6 +38,7 @@ VALUE rb_iseqw_local_variables(VALUE iseqval); VALUE rb_iseqw_new(const rb_iseq_t *); +int str_end_with_asciichar(VALUE str, int c); VALUE rb_eEAGAIN; VALUE rb_eEWOULDBLOCK; @@ -276,10 +277,12 @@ rb_enc_warning(rb_encoding *enc, const char *fmt, ...) * call-seq: * warn(msg, ...) -> nil * - * Displays each of the given messages followed by a record separator on - * STDERR unless warnings have been disabled (for example with the - * -W0 flag). - * + * If warnings have been disabled (for example with the + * -W0 flag), does nothing. Otherwise, + * joins all strings with a newline, makes sure + * the resulting string ends with a newline, and calls + * Warning.warn with the string. + * * warn("warning 1", "warning 2") * * produces: @@ -292,7 +295,13 @@ static VALUE rb_warn_m(int argc, VALUE *argv, VALUE exc) { if (!NIL_P(ruby_verbose) && argc > 0) { - rb_io_puts(argc, argv, rb_stderr); + VALUE str; + str = rb_ary_new_from_values(argc, argv); + str = rb_ary_join(str, rb_str_new("\n", 1)); + if (RSTRING_LEN(str) == 0 || !str_end_with_asciichar(str, '\n')) { + rb_str_cat(str, "\n", 1); + } + rb_write_warning_str(str); } return Qnil; } diff --git a/io.c b/io.c index 1f00f6c..865b3a6 100644 --- a/io.c +++ b/io.c @@ -7123,7 +7123,7 @@ rb_f_putc(VALUE recv, VALUE ch) } -static int +int str_end_with_asciichar(VALUE str, int c) { long len = RSTRING_LEN(str); -- 2.10.1