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

ruby-changes:26170

From: ko1 <ko1@a...>
Date: Thu, 6 Dec 2012 12:15:53 +0900 (JST)
Subject: [ruby-changes:26170] ko1:r38227 (trunk): * vm_trace.c: TracePoint#enable should not cause an error

ko1	2012-12-06 12:13:50 +0900 (Thu, 06 Dec 2012)

  New Revision: 38227

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

  Log:
    * vm_trace.c: TracePoint#enable should not cause an error
      when it is already enabled. TracePoint#disable is too.
      [ruby-core:50561] [ruby-trunk - Bug #7513]
    * test/ruby/test_settracefunc.rb: add tests.

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_settracefunc.rb
    trunk/vm_trace.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38226)
+++ ChangeLog	(revision 38227)
@@ -1,3 +1,11 @@
+Thu Dec 06 12:07:11 2012  Koichi Sasada  <ko1@a...>
+
+	* vm_trace.c: TracePoint#enable should not cause an error
+	  when it is already enabled. TracePoint#disable is too.
+	  [ruby-core:50561] [ruby-trunk - Bug #7513]
+
+	* test/ruby/test_settracefunc.rb: add tests.
+
 Thu Dec  6 07:19:58 2012  Eric Hodel  <drbrain@s...>
 
 	* lib/rdoc*:  Improved display of ChangeLog files as HTML.
Index: vm_trace.c
===================================================================
--- vm_trace.c	(revision 38226)
+++ vm_trace.c	(revision 38227)
@@ -952,20 +952,23 @@
 
 /*
  * call-seq:
- *	trace.enable		-> trace
+ *	trace.enable		-> true or false
  *	trace.enable { block }	-> obj
  *
  * Activates the trace
  *
- * Will raise a RuntimeError if the trace is already activated
+ * Return true if trace was enabled.
+ * Return false if trace was disabled.
  *
  *	trace.enabled?  #=> false
- *	trace.enable    #=> #<TracePoint:0x007fa3fad4aaa8>
+ *	trace.enable    #=> false (previous state)
+ *                      #   trace is enabled
  *	trace.enabled?  #=> true
- *	trace.enable    #=> RuntimeError
+ *	trace.enable    #=> true (previous state)
+ *                      #   trace is still enabled
  *
  * If a block is given, the trace will only be enabled within the scope of the
- * block. Note: You cannot access event hooks within the block.
+ * block.
  *
  *	trace.enabled?
  *	#=> false
@@ -978,6 +981,8 @@
  *	trace.enabled?
  *	#=> false
  *
+ * Note: You cannot access event hooks within the block.
+ *
  *	trace.enable { p tp.lineno }
  *	#=> RuntimeError: access from outside
  *
@@ -986,36 +991,36 @@
 tracepoint_enable_m(VALUE tpval)
 {
     rb_tp_t *tp = tpptr(tpval);
+    int previous_tracing = tp->tracing;
+    rb_tracepoint_enable(tpval);
 
-    if (tp->tracing) {
-	rb_raise(rb_eRuntimeError, "trace is already enable");
-    }
-
-    rb_tracepoint_enable(tpval);
     if (rb_block_given_p()) {
-	return rb_ensure(rb_yield, Qnil, rb_tracepoint_disable, tpval);
+	return rb_ensure(rb_yield, Qnil,
+			 previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
+			 tpval);
     }
     else {
-	return tpval;
+	return previous_tracing ? Qtrue : Qfalse;
     }
 }
 
 /*
  * call-seq:
- *	trace.disable		-> trace
+ *	trace.disable		-> tru eo rfalse
  *	trace.disable { block } -> obj
  *
  * Deactivates the trace
  *
- * Will raise a RuntimeError if the trace is already deactivated
+ * Return true if trace was enabled.
+ * Return false if trace was disabled.
  *
  *	trace.enabled?	#=> true
- *	trace.disable	#=> #<TracePoint:0x007fa3fad4aaa8>
+ *	trace.disable	#=> false (previous status)
  *	trace.enabled?	#=> false
- *	trace.disable	#=> RuntimeError
+ *	trace.disable	#=> false
  *
  * If a block is given, the trace will only be disable within the scope of the
- * block. Note: You cannot access event hooks within the block.
+ * block.
  *
  *	trace.enabled?
  *	#=> true
@@ -1028,25 +1033,25 @@
  *	trace.enabled?
  *	#=> true
  *
- *	trace.enable { p trace.lineno }
+ * Note: You cannot access event hooks within the block.
+ *
+ *	trace.disable { p tp.lineno }
  *	#=> RuntimeError: access from outside
- *
  */
 static VALUE
 tracepoint_disable_m(VALUE tpval)
 {
     rb_tp_t *tp = tpptr(tpval);
+    int previous_tracing = tp->tracing;
+    rb_tracepoint_disable(tpval);
 
-    if (!tp->tracing) {
-	rb_raise(rb_eRuntimeError, "trace is not enable");
-    }
-
-    rb_tracepoint_disable(tpval);
     if (rb_block_given_p()) {
-	return rb_ensure(rb_yield, Qnil, rb_tracepoint_enable, tpval);
+	return rb_ensure(rb_yield, Qnil,
+			 previous_tracing ? rb_tracepoint_enable : rb_tracepoint_disable,
+			 tpval);
     }
     else {
-	return tpval;
+	return previous_tracing ? Qtrue : Qfalse;
     }
 }
 
Index: test/ruby/test_settracefunc.rb
===================================================================
--- test/ruby/test_settracefunc.rb	(revision 38226)
+++ test/ruby/test_settracefunc.rb	(revision 38227)
@@ -623,6 +623,16 @@
     }
     foo
     assert_equal([:foo], ary)
+
+    trace = TracePoint.new{}
+    begin
+      assert_equal(false, trace.enable)
+      assert_equal(true, trace.enable)
+      trace.enable{}
+      assert_equal(true, trace.enable)
+    ensure
+      trace.disable
+    end
   end
 
   def test_tracepoint_disable
@@ -637,6 +647,14 @@
     foo
     trace.disable
     assert_equal([:foo, :foo], ary)
+
+    trace = TracePoint.new{}
+    trace.enable{
+      assert_equal(true, trace.disable)
+      assert_equal(false, trace.disable)
+      trace.disable{}
+      assert_equal(false, trace.disable)
+    }
   end
 
   def test_tracepoint_enabled

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

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