Project

General

Profile

Feature #12944 ยป 0001-Change-Kernel-warn-to-call-Warning.warn.patch

flatten array, append newlines to each line, then join - jeremyevans0 (Jeremy Evans), 12/14/2016 03:40 PM

View differences:

error.c
38 38

  
39 39
VALUE rb_iseqw_local_variables(VALUE iseqval);
40 40
VALUE rb_iseqw_new(const rb_iseq_t *);
41
int str_end_with_asciichar(VALUE str, int c);
41 42

  
42 43
VALUE rb_eEAGAIN;
43 44
VALUE rb_eEWOULDBLOCK;
44 45
VALUE rb_eEINPROGRESS;
45 46
VALUE rb_mWarning;
46 47

  
47
static ID id_warn;
48
static ID id_warn, id_flatten_b;
48 49

  
49 50
extern const char ruby_description[];
50 51

  
......
276 277
 * call-seq:
277 278
 *    warn(msg, ...)   -> nil
278 279
 *
279
 * Displays each of the given messages followed by a record separator on
280
 * STDERR unless warnings have been disabled (for example with the
281
 * <code>-W0</code> flag).
282
 *
280
 * If warnings have been disabled (for example with the
281
 * <code>-W0</code> flag), does nothing.  Otherwise, 
282
 * flattens the array of arguments and converts the
283
 * entries to strings, makes sure all strings end with
284
 * a newline, joins all strings into a single string,
285
 * and calls <code>Warning.warn</code> with the 
286
 * resulting string.
287
 * 
283 288
 *    warn("warning 1", "warning 2")
289
 *    warn(["warning 1\n", "warning 2"])
290
 *    warn("warning 1", "warning 2\n")
284 291
 *
285
 *  <em>produces:</em>
292
 *  <em>all call <code>Warning.warn</code> with:</em>
286 293
 *
287
 *    warning 1
288
 *    warning 2
294
 *    "warning 1\nwarning 2\n"
289 295
 */
290 296

  
291 297
static VALUE
292 298
rb_warn_m(int argc, VALUE *argv, VALUE exc)
293 299
{
294 300
    if (!NIL_P(ruby_verbose) && argc > 0) {
295
	rb_io_puts(argc, argv, rb_stderr);
301
	int i;
302
	VALUE str;
303
	VALUE ary = rb_ary_new_from_values(argc, argv);
304
	rb_funcall(ary, id_flatten_b, 0);
305
	Check_Type(ary, T_ARRAY);
306
	argc = RARRAY_LEN(ary);
307
	argv = RARRAY_PTR(ary);
308

  
309
	for(i = 0; i < argc; i++) {
310
	    str = rb_obj_as_string(argv[i]);
311
	    if (RSTRING_LEN(str) == 0 || !str_end_with_asciichar(str, '\n')) {
312
		str = rb_str_cat(rb_str_dup(str), "\n", 1);
313
	    }
314
	    rb_ary_store(ary, i, str);
315
	}
316

  
317
	rb_write_warning_str(rb_ary_join(ary, rb_str_new("", 0)));
296 318
    }
297 319
    return Qnil;
298 320
}
......
2108 2130
    id_errno = rb_intern_const("errno");
2109 2131
    id_i_path = rb_intern_const("@path");
2110 2132
    id_warn = rb_intern_const("warn");
2133
    id_flatten_b = rb_intern_const("flatten!");
2111 2134
    id_iseq = rb_make_internal_id();
2112 2135
}
2113 2136

  
io.c
7123 7123
}
7124 7124

  
7125 7125

  
7126
static int
7126
int
7127 7127
str_end_with_asciichar(VALUE str, int c)
7128 7128
{
7129 7129
    long len = RSTRING_LEN(str);
7130
-