aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--error.c46
-rw-r--r--test/ruby/test_exception.rb6
2 files changed, 52 insertions, 0 deletions
diff --git a/error.c b/error.c
index b2f6565dfe..a61c023154 100644
--- a/error.c
+++ b/error.c
@@ -128,6 +128,50 @@ rb_syntax_error_append(VALUE exc, VALUE file, int line, int column,
return exc;
}
+static unsigned int warning_disabled_categories;
+#define RB_WARN_CATEGORY_DEPRECATED 1
+
+static unsigned int
+rb_warning_category_mask(VALUE category)
+{
+ unsigned int mask = 0;
+ Check_Type(category, T_SYMBOL);
+ if (category == ID2SYM(rb_intern("deprecated"))) {
+ mask = RB_WARN_CATEGORY_DEPRECATED;
+ }
+ else {
+ rb_raise(rb_eArgError, "unknown category: %"PRIsVALUE, category);
+ }
+ return mask;
+}
+
+static int
+rb_warning_category_enabled_p(VALUE category)
+{
+ return !(warning_disabled_categories & rb_warning_category_mask(category));
+}
+
+static VALUE
+rb_warning_s_aref(VALUE mod, VALUE category)
+{
+ if (rb_warning_category_enabled_p(category))
+ return Qtrue;
+ return Qfalse;
+}
+
+static VALUE
+rb_warning_s_aset(VALUE mod, VALUE category, VALUE flag)
+{
+ unsigned int mask = rb_warning_category_mask(category);
+ unsigned int disabled = warning_disabled_categories;
+ if (!RTEST(flag))
+ disabled |= mask;
+ else
+ disabled &= ~mask;
+ warning_disabled_categories = disabled;
+ return flag;
+}
+
/*
* call-seq:
* warn(msg) -> nil
@@ -2501,6 +2545,8 @@ Init_Exception(void)
rb_mErrno = rb_define_module("Errno");
rb_mWarning = rb_define_module("Warning");
+ rb_define_singleton_method(rb_mWarning, "[]", rb_warning_s_aref, 1);
+ rb_define_singleton_method(rb_mWarning, "[]=", rb_warning_s_aset, 2);
rb_define_method(rb_mWarning, "warn", rb_warning_s_warn, 1);
rb_extend_object(rb_mWarning, rb_mWarning);
diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb
index 05cc109b48..56cd19d0a2 100644
--- a/test/ruby/test_exception.rb
+++ b/test/ruby/test_exception.rb
@@ -1259,6 +1259,12 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status|
};
end
+ def test_warning_category
+ assert_raise(TypeError) {Warning[nil]}
+ assert_raise(ArgumentError) {Warning[:XXXX]}
+ assert_include([true, false], Warning[:deprecated])
+ end
+
def test_undefined_backtrace
assert_separately([], "#{<<-"begin;"}\n#{<<-"end;"}")
begin;