Project

General

Profile

Feature #12299 ยป 0001-Add-Warning-module-for-customized-warning-handling.patch

jeremyevans0 (Jeremy Evans), 08/09/2016 03:36 PM

View differences:

error.c
42 42
VALUE rb_eEAGAIN;
43 43
VALUE rb_eEWOULDBLOCK;
44 44
VALUE rb_eEINPROGRESS;
45
VALUE rb_mWarning;
46

  
47
static ID id_warn;
45 48

  
46 49
extern const char ruby_description[];
47 50

  
......
147 150
}
148 151

  
149 152
static VALUE
153
rb_warning_s_warn(VALUE mod, VALUE str)
154
{
155
    rb_write_error_str(str);
156
    return Qnil;
157
}
158

  
159
static void
160
rb_write_warning_str(VALUE str)
161
{
162
    rb_funcall(rb_mWarning, id_warn, 1, str);
163
}
164

  
165
static VALUE
150 166
warn_vsprintf(rb_encoding *enc, const char *file, int line, const char *fmt, va_list args)
151 167
{
152 168
    VALUE str = rb_enc_str_new(0, 0, enc);
......
166 182
    va_start(args, fmt);
167 183
    str = warn_vsprintf(NULL, file, line, fmt, args);
168 184
    va_end(args);
169
    rb_write_error_str(str);
185
    rb_write_warning_str(str);
170 186
}
171 187

  
172 188
/* rb_compile_warning() reports only in verbose mode */
......
181 197
    va_start(args, fmt);
182 198
    str = warn_vsprintf(NULL, file, line, fmt, args);
183 199
    va_end(args);
184
    rb_write_error_str(str);
200
    rb_write_warning_str(str);
185 201
}
186 202

  
187 203
static VALUE
......
206 222
    va_start(args, fmt);
207 223
    mesg = warning_string(0, fmt, args);
208 224
    va_end(args);
209
    rb_write_error_str(mesg);
225
    rb_write_warning_str(mesg);
210 226
}
211 227

  
212 228
void
......
220 236
    va_start(args, fmt);
221 237
    mesg = warning_string(enc, fmt, args);
222 238
    va_end(args);
223
    rb_write_error_str(mesg);
239
    rb_write_warning_str(mesg);
224 240
}
225 241

  
226 242
/* rb_warning() reports only in verbose mode */
......
235 251
    va_start(args, fmt);
236 252
    mesg = warning_string(0, fmt, args);
237 253
    va_end(args);
238
    rb_write_error_str(mesg);
254
    rb_write_warning_str(mesg);
239 255
}
240 256

  
241 257
#if 0
......
250 266
    va_start(args, fmt);
251 267
    mesg = warning_string(enc, fmt, args);
252 268
    va_end(args);
253
    rb_write_error_str(mesg);
269
    rb_write_warning_str(mesg);
254 270
}
255 271
#endif
256 272

  
......
2052 2068

  
2053 2069
    rb_mErrno = rb_define_module("Errno");
2054 2070

  
2071
    rb_mWarning = rb_define_module("Warning");
2072
    rb_define_method(rb_mWarning, "warn", rb_warning_s_warn, 1);
2073
    rb_extend_object(rb_mWarning, rb_mWarning);
2074

  
2055 2075
    rb_define_global_function("warn", rb_warn_m, -1);
2056 2076

  
2057 2077
    id_new = rb_intern_const("new");
......
2066 2086
    id_Errno = rb_intern_const("Errno");
2067 2087
    id_errno = rb_intern_const("errno");
2068 2088
    id_i_path = rb_intern_const("@path");
2089
    id_warn = rb_intern_const("warn");
2069 2090
    id_iseq = rb_make_internal_id();
2070 2091
}
2071 2092

  
......
2289 2310
    va_end(args);
2290 2311
    rb_str_set_len(mesg, RSTRING_LEN(mesg)-1);
2291 2312
    rb_str_catf(mesg, ": %s\n", strerror(errno_save));
2292
    rb_write_error_str(mesg);
2313
    rb_write_warning_str(mesg);
2293 2314
    errno = errno_save;
2294 2315
}
2295 2316

  
......
2309 2330
    va_end(args);
2310 2331
    rb_str_set_len(mesg, RSTRING_LEN(mesg)-1);
2311 2332
    rb_str_catf(mesg, ": %s\n", strerror(errno_save));
2312
    rb_write_error_str(mesg);
2333
    rb_write_warning_str(mesg);
2313 2334
    errno = errno_save;
2314 2335
}
2315 2336

  
test/ruby/test_exception.rb
855 855
      end
856 856
    end
857 857
  end
858

  
859
  def test_warning_warn
860
    verbose = $VERBOSE
861
    warning = nil
862

  
863
    ::Warning.class_eval do
864
      alias_method :warn2, :warn
865
      remove_method :warn
866

  
867
      define_method(:warn) do |str|
868
        warning = str
869
      end
870
    end
871

  
872
    $VERBOSE = true
873
    a = @a
874

  
875
    assert_match(/instance variable @a not initialized/, warning)
876
  ensure
877
    $VERBOSE = verbose
878

  
879
    ::Warning.class_eval do
880
      remove_method :warn
881
      alias_method :warn, :warn2
882
      remove_method :warn2
883
    end
884
  end
858 885
end
859
-