ruby-changes:62811
From: eileencodes <ko1@a...>
Date: Thu, 3 Sep 2020 04:44:48 +0900 (JST)
Subject: [ruby-changes:62811] eada635033 (master): Add category to `rb_warn_deprecated`
https://git.ruby-lang.org/ruby.git/commit/?id=eada635033 From eada6350332155972f19bad52bd8621f607520a2 Mon Sep 17 00:00:00 2001 From: eileencodes <eileencodes@g...> Date: Wed, 2 Sep 2020 08:56:13 -0400 Subject: Add category to `rb_warn_deprecated` PR https://github.com/ruby/ruby/pull/3418 added a category to `rb_warn_deprecated_to_remove` but not to `rb_warn_deprecated`. This adds the same code to `rb_warn_deprecated` so that those warnings also get a category. This change also adds tests for `rb_warn_deprecated` and updates the tests for `rb_warn_deprecated_to_remove` to have clearer names. I've fixed the call to `rb_method_entry` as we need to be using the instance method, not singleton. Feature: https://bugs.ruby-lang.org/issues/17122 diff --git a/error.c b/error.c index 841ab6c..a9adf57 100644 --- a/error.c +++ b/error.c @@ -392,7 +392,22 @@ rb_warn_deprecated(const char *fmt, const char *suggest, ...) https://github.com/ruby/ruby/blob/trunk/error.c#L392 rb_str_cat_cstr(mesg, " is deprecated"); if (suggest) rb_str_catf(mesg, "; use %s instead", suggest); rb_str_cat_cstr(mesg, "\n"); - rb_write_warning_str(mesg); + + VALUE warn_args[2]; + warn_args[0] = mesg; + + const rb_method_entry_t * me; + me = rb_method_entry(rb_singleton_class(rb_mWarning), id_warn); + + if (rb_method_entry_arity(me) != 1) { + VALUE kwargs = rb_hash_new(); + rb_hash_aset(kwargs, ID2SYM(rb_intern("category")), ID2SYM(rb_intern("deprecated"))); + warn_args[1] = kwargs; + + rb_funcallv_kw(rb_mWarning, id_warn, 2, warn_args, RB_PASS_KEYWORDS); + } else { + rb_funcall(rb_mWarning, id_warn, 1, mesg); + } } void @@ -411,7 +426,7 @@ rb_warn_deprecated_to_remove(const char *fmt, const char *removal, ...) https://github.com/ruby/ruby/blob/trunk/error.c#L426 warn_args[0] = mesg; const rb_method_entry_t * me; - me = rb_method_entry(rb_mWarning, id_warn); + me = rb_method_entry(rb_singleton_class(rb_mWarning), id_warn); if (rb_method_entry_arity(me) != 1) { VALUE kwargs = rb_hash_new(); diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index a89aed8..120b041 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -960,13 +960,25 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L960 assert_equal(["\n"], capture_warning_warn {warn ""}) end - def test_warn_backwards_compatibility + def test_warn_deprecated_backwards_compatibility_category + warning = capture_warning_warn { Dir.exists?("non-existent") } + + assert_match(/deprecated/, warning[0]) + end + + def test_warn_deprecated_category + warning = capture_warning_warn(category: true) { Dir.exists?("non-existent") } + + assert_equal :deprecated, warning[0][1] + end + + def test_warn_deprecated_to_remove_backwards_compatibility_category warning = capture_warning_warn { Object.new.tainted? } assert_match(/deprecated/, warning[0]) end - def test_warn_category + def test_warn_deprecated_to_remove_category warning = capture_warning_warn(category: true) { Object.new.tainted? } assert_equal :deprecated, warning[0][1] -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/