[前][次][番号順一覧][スレッド一覧]

ruby-changes:45297

From: nobu <ko1@a...>
Date: Thu, 19 Jan 2017 15:25:12 +0900 (JST)
Subject: [ruby-changes:45297] nobu:r57370 (trunk): Change Kernel#warn to call Warning.warn

nobu	2017-01-19 15:25:06 +0900 (Thu, 19 Jan 2017)

  New Revision: 57370

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57370

  Log:
    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 are probably 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.
    
    [Feature #12944]
    Author:    Jeremy Evans <code@j...>

  Modified files:
    trunk/error.c
    trunk/io.c
Index: io.c
===================================================================
--- io.c	(revision 57369)
+++ io.c	(revision 57370)
@@ -7124,8 +7124,8 @@ rb_f_putc(VALUE recv, VALUE ch) https://github.com/ruby/ruby/blob/trunk/io.c#L7124
 }
 
 
-static int
-str_end_with_asciichar(VALUE str, int c)
+int
+rb_str_end_with_asciichar(VALUE str, int c)
 {
     long len = RSTRING_LEN(str);
     const char *ptr = RSTRING_PTR(str);
@@ -7202,7 +7202,7 @@ rb_io_puts(int argc, const VALUE *argv, https://github.com/ruby/ruby/blob/trunk/io.c#L7202
       string:
 	rb_io_write(out, line);
 	if (RSTRING_LEN(line) == 0 ||
-            !str_end_with_asciichar(line, '\n')) {
+            !rb_str_end_with_asciichar(line, '\n')) {
 	    rb_io_write(out, rb_default_rs);
 	}
     }
Index: error.c
===================================================================
--- error.c	(revision 57369)
+++ error.c	(revision 57370)
@@ -42,6 +42,7 @@ https://github.com/ruby/ruby/blob/trunk/error.c#L42
 
 VALUE rb_iseqw_local_variables(VALUE iseqval);
 VALUE rb_iseqw_new(const rb_iseq_t *);
+int rb_str_end_with_asciichar(VALUE str, int c);
 
 VALUE rb_eEAGAIN;
 VALUE rb_eEWOULDBLOCK;
@@ -280,9 +281,11 @@ rb_enc_warning(rb_encoding *enc, const c https://github.com/ruby/ruby/blob/trunk/error.c#L281
  * 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
- * <code>-W0</code> flag).
+ * If warnings have been disabled (for example with the
+ * <code>-W0</code> flag), does nothing.  Otherwise,
+ * converts each of the messages to strings, appends a newline
+ * character to the string if the string does not end in a newline,
+ * and calls <code>Warning.warn</code> with the string.
  *
  *    warn("warning 1", "warning 2")
  *
@@ -296,7 +299,15 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/error.c#L299
 rb_warn_m(int argc, VALUE *argv, VALUE exc)
 {
     if (!NIL_P(ruby_verbose) && argc > 0) {
-	rb_io_puts(argc, argv, rb_stderr);
+	int i;
+	VALUE str;
+	for (i = 0; i < argc; i++) {
+	    str = rb_obj_as_string(argv[i]);
+	    if (RSTRING_LEN(str) == 0 || !rb_str_end_with_asciichar(str, '\n')) {
+		str = rb_str_cat(rb_str_dup(str), "\n", 1);
+	    }
+	    rb_write_warning_str(str);
+	}
     }
     return Qnil;
 }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]