[前][次][番号順一覧][スレッド一覧]

ruby-changes:31125

From: nobu <ko1@a...>
Date: Wed, 9 Oct 2013 00:56:10 +0900 (JST)
Subject: [ruby-changes:31125] nobu:r43204 (trunk): load.c: display backtrace to $stderr

nobu	2013-10-09 00:56:01 +0900 (Wed, 09 Oct 2013)

  New Revision: 43204

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43204

  Log:
    load.c: display backtrace to $stderr
    
    * load.c (load_lock): display backtrace to $stderr at circular
      require.
    * vm_backtrace.c (rb_backtrace_print_to): new function to print
      backtrace to the given output.

  Modified files:
    trunk/ChangeLog
    trunk/internal.h
    trunk/load.c
    trunk/test/ruby/test_require.rb
    trunk/vm_backtrace.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43203)
+++ ChangeLog	(revision 43204)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Oct  9 00:55:51 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* load.c (load_lock): display backtrace to $stderr at circular
+	  require.
+
+	* vm_backtrace.c (rb_backtrace_print_to): new function to print
+	  backtrace to the given output.
+
 Tue Oct  8 21:03:35 2013  Koichi Sasada  <ko1@a...>
 
 	* vm_backtrace.c, include/ruby/debug.h: add new APIs
Index: load.c
===================================================================
--- load.c	(revision 43203)
+++ load.c	(revision 43204)
@@ -706,8 +706,7 @@ load_lock(const char *ftptr) https://github.com/ruby/ruby/blob/trunk/load.c#L706
     }
     if (RTEST(ruby_verbose)) {
 	rb_warning("loading in progress, circular require considered harmful - %s", ftptr);
-	/* TODO: display to $stderr, not stderr in C */
-	rb_backtrace();
+	rb_backtrace_print_to(rb_stderr);
     }
     switch (rb_thread_shield_wait((VALUE)data)) {
       case Qfalse:
Index: vm_backtrace.c
===================================================================
--- vm_backtrace.c	(revision 43203)
+++ vm_backtrace.c	(revision 43204)
@@ -768,6 +768,35 @@ rb_backtrace(void) https://github.com/ruby/ruby/blob/trunk/vm_backtrace.c#L768
     vm_backtrace_print(stderr);
 }
 
+static void
+oldbt_print_to(void *data, VALUE file, int lineno, VALUE name)
+{
+    VALUE output = (VALUE)data;
+    VALUE str = rb_sprintf("\tfrom %"PRIsVALUE":%d:in ", file, lineno);
+
+    if (NIL_P(name)) {
+	rb_str_cat2(str, "unknown method\n");
+    }
+    else {
+	rb_str_catf(str, " `%"PRIsVALUE"'\n", name);
+    }
+    rb_io_write(output, str);
+}
+
+void
+rb_backtrace_print_to(VALUE output)
+{
+    struct oldbt_arg arg;
+
+    arg.func = oldbt_print_to;
+    arg.data = (void *)output;
+    backtrace_each(GET_THREAD(),
+		   oldbt_init,
+		   oldbt_iter_iseq,
+		   oldbt_iter_cfunc,
+		   &arg);
+}
+
 VALUE
 rb_make_backtrace(void)
 {
Index: internal.h
===================================================================
--- internal.h	(revision 43203)
+++ internal.h	(revision 43204)
@@ -744,6 +744,7 @@ VALUE rb_make_backtrace(void); https://github.com/ruby/ruby/blob/trunk/internal.h#L744
 void rb_backtrace_print_as_bugreport(void);
 int rb_backtrace_p(VALUE obj);
 VALUE rb_backtrace_to_str_ary(VALUE obj);
+void rb_backtrace_print_to(VALUE output);
 VALUE rb_vm_backtrace_object();
 
 RUBY_SYMBOL_EXPORT_BEGIN
Index: test/ruby/test_require.rb
===================================================================
--- test/ruby/test_require.rb	(revision 43203)
+++ test/ruby/test_require.rb	(revision 43204)
@@ -399,6 +399,7 @@ class TestRequire < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L399
   def test_race_exception
     bug5754 = '[ruby-core:41618]'
     path = nil
+    stderr = $stderr
     Tempfile.create(%w"bug5754 .rb") {|tmp|
       path = tmp.path
       tmp.print %{\
@@ -416,12 +417,11 @@ class TestRequire < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L417
       }
       tmp.close
 
-      # "circular require" warnings to $stderr, but backtraces to stderr
-      # in C-level.  And redirecting stderr to a pipe seems to change
-      # some blocking timings and causes a deadlock, so run in a
-      # separated process for the time being.
-      assert_separately(["-w", "-", path, bug5754], <<-'end;', ignore_stderr: true)
-      path, bug5754 = *ARGV
+      class << (output = "")
+        alias write concat
+      end
+      $stderr = output
+
       start = false
 
       scratch = []
@@ -454,9 +454,12 @@ class TestRequire < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L454
 
       assert_equal(true, (t1_res ^ t2_res), bug5754 + " t1:#{t1_res} t2:#{t2_res}")
       assert_equal([:pre, :post], scratch, bug5754)
-      end;
+
+      assert_match(/circular require/, output)
+      assert_match(/in #{__method__}'$/o, output)
     }
   ensure
+    $stderr = stderr
     $".delete(path)
   end
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]