ruby-changes:46357
From: nobu <ko1@a...>
Date: Tue, 25 Apr 2017 17:17:29 +0900 (JST)
Subject: [ruby-changes:46357] nobu:r58471 (trunk): load.c: backtrace of circular require
nobu 2017-04-25 17:17:24 +0900 (Tue, 25 Apr 2017) New Revision: 58471 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58471 Log: load.c: backtrace of circular require * load.c (load_lock): print backtrace of circular require via `Warning.warn` [ruby-core:80850] [Bug #13505] Modified files: trunk/error.c trunk/internal.h trunk/load.c trunk/test/ruby/test_exception.rb trunk/vm_backtrace.c Index: error.c =================================================================== --- error.c (revision 58470) +++ error.c (revision 58471) @@ -145,10 +145,16 @@ rb_warning_s_warn(VALUE mod, VALUE str) https://github.com/ruby/ruby/blob/trunk/error.c#L145 return Qnil; } +VALUE +rb_warning_warn(VALUE mod, VALUE str) +{ + return rb_funcallv(mod, id_warn, 1, &str); +} + static void rb_write_warning_str(VALUE str) { - rb_funcall(rb_mWarning, id_warn, 1, str); + rb_warning_warn(rb_mWarning, str); } static VALUE Index: load.c =================================================================== --- load.c (revision 58470) +++ load.c (revision 58471) @@ -719,6 +719,8 @@ rb_f_load(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/load.c#L719 return Qtrue; } +extern VALUE rb_mWarning; + static char * load_lock(const char *ftptr) { @@ -742,7 +744,7 @@ load_lock(const char *ftptr) https://github.com/ruby/ruby/blob/trunk/load.c#L744 } if (RTEST(ruby_verbose)) { rb_warning("loading in progress, circular require considered harmful - %s", ftptr); - rb_backtrace_print_to(rb_stderr); + rb_backtrace_each(rb_warning_warn, rb_mWarning); } switch (rb_thread_shield_wait((VALUE)data)) { case Qfalse: Index: test/ruby/test_exception.rb =================================================================== --- test/ruby/test_exception.rb (revision 58470) +++ test/ruby/test_exception.rb (revision 58471) @@ -943,23 +943,23 @@ $stderr = $stdout; raise "\x82\xa0"') do https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L943 end end - def test_warning_warn + def capture_warning_warn verbose = $VERBOSE - warning = nil + warning = [] ::Warning.class_eval do alias_method :warn2, :warn remove_method :warn define_method(:warn) do |str| - warning = str + warning << str end end $VERBOSE = true - a = @a + yield - assert_match(/instance variable @a not initialized/, warning) + return warning ensure $VERBOSE = verbose @@ -970,6 +970,11 @@ $stderr = $stdout; raise "\x82\xa0"') do https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L970 end end + def test_warning_warn + warning = capture_warning_warn {@a} + assert_match(/instance variable @a not initialized/, warning[0]) + end + def test_warning_warn_invalid_argument assert_raise(TypeError) do ::Warning.warn nil @@ -982,6 +987,25 @@ $stderr = $stdout; raise "\x82\xa0"') do https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L987 end end + def test_warning_warn_circular_require_backtrace + warning = nil + path = nil + Tempfile.create(%w[circular .rb]) do |t| + path = t.path + basename = File.basename(path) + t.puts "require '#{basename}'" + t.close + $LOAD_PATH.push(File.dirname(t)) + warning = capture_warning_warn {require basename} + ensure + $LOAD_PATH.pop + $LOADED_FEATURES.delete(t) + end + assert_match(/circular require/, warning.first) + warning.pop while %r[lib/rubygems/core_ext/kernel_require.rb:] =~ warning.last + assert_operator(warning.last, :start_with?, "\tfrom #{path}:1:") + end + def test_undefined_backtrace assert_separately([], "#{<<-"begin;"}\n#{<<-"end;"}") begin; Index: vm_backtrace.c =================================================================== --- vm_backtrace.c (revision 58470) +++ vm_backtrace.c (revision 58471) @@ -772,10 +772,15 @@ rb_backtrace(void) https://github.com/ruby/ruby/blob/trunk/vm_backtrace.c#L772 vm_backtrace_print(stderr); } +struct print_to_arg { + VALUE (*iter)(VALUE recv, VALUE str); + VALUE output; +}; + static void oldbt_print_to(void *data, VALUE file, int lineno, VALUE name) { - VALUE output = (VALUE)data; + const struct print_to_arg *arg = data; VALUE str = rb_sprintf("\tfrom %"PRIsVALUE":%d:in ", file, lineno); if (NIL_P(name)) { @@ -784,16 +789,19 @@ oldbt_print_to(void *data, VALUE file, i https://github.com/ruby/ruby/blob/trunk/vm_backtrace.c#L789 else { rb_str_catf(str, " `%"PRIsVALUE"'\n", name); } - rb_io_write(output, str); + (*arg->iter)(arg->output, str); } void -rb_backtrace_print_to(VALUE output) +rb_backtrace_each(VALUE (*iter)(VALUE recv, VALUE str), VALUE output) { struct oldbt_arg arg; + struct print_to_arg parg; + parg.iter = iter; + parg.output = output; arg.func = oldbt_print_to; - arg.data = (void *)output; + arg.data = &parg; backtrace_each(GET_THREAD(), oldbt_init, oldbt_iter_iseq, Index: internal.h =================================================================== --- internal.h (revision 58470) +++ internal.h (revision 58471) @@ -1139,6 +1139,7 @@ VALUE rb_name_err_new(VALUE mesg, VALUE https://github.com/ruby/ruby/blob/trunk/internal.h#L1139 NORETURN(void ruby_deprecated_internal_feature(const char *)); #define DEPRECATED_INTERNAL_FEATURE(func) \ (ruby_deprecated_internal_feature(func), UNREACHABLE) +VALUE rb_warning_warn(VALUE mod, VALUE str); /* eval.c */ VALUE rb_refinement_module_get_refined_class(VALUE module); @@ -1749,7 +1750,7 @@ void rb_backtrace_print_as_bugreport(voi https://github.com/ruby/ruby/blob/trunk/internal.h#L1750 int rb_backtrace_p(VALUE obj); VALUE rb_backtrace_to_str_ary(VALUE obj); VALUE rb_backtrace_to_location_ary(VALUE obj); -void rb_backtrace_print_to(VALUE output); +void rb_backtrace_each(VALUE (*iter)(VALUE recv, VALUE str), VALUE output); RUBY_SYMBOL_EXPORT_BEGIN const char *rb_objspace_data_type_name(VALUE obj); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/