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

ruby-changes:25666

From: kosaki <ko1@a...>
Date: Mon, 19 Nov 2012 18:44:04 +0900 (JST)
Subject: [ruby-changes:25666] kosaki:r37723 (trunk): * signal.c (sig_signame): implements Signal.signame method

kosaki	2012-11-19 18:43:53 +0900 (Mon, 19 Nov 2012)

  New Revision: 37723

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

  Log:
    * signal.c (sig_signame): implements Signal.signame method
    [Feature #5613]
    * test/ruby/test_signal.rb (test_signame): adds test for above
    * NEWS: add an item about above

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/signal.c
    trunk/test/ruby/test_signal.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37722)
+++ ChangeLog	(revision 37723)
@@ -1,3 +1,10 @@
+Tue Nov 20 08:36:15 2012  KOSAKI Motohiro  <kosaki.motohiro@g...>
+
+	* signal.c (sig_signame): implements Signal.signame method
+	[Feature #5613]
+	* test/ruby/test_signal.rb (test_signame): adds test for above
+	* NEWS: add an item about above
+
 Mon Nov 19 16:30:59 2012  Nobuyoshi Nakada  <nobu@r...>
 
 	* struct.c (rb_struct_each_pair): yield associated pairs so that
Index: NEWS
===================================================================
--- NEWS	(revision 37722)
+++ NEWS	(revision 37723)
@@ -105,6 +105,9 @@
       * added Range#bsearch for binary search.
 
   * Signal
+    * added method:
+      * added Signal.signame which returns signal name
+
     * incompatible changes:
       * Signal.trap raises ArgumentError when :SEGV, :BUS, :ILL, :FPE, :VTALRM
         are specified.
Index: test/ruby/test_signal.rb
===================================================================
--- test/ruby/test_signal.rb	(revision 37722)
+++ test/ruby/test_signal.rb	(revision 37723)
@@ -240,4 +240,22 @@
     }
   end
 
+  def test_signame
+    return unless Process.respond_to?(:kill)
+
+    begin
+      10.times do
+        caught = 0
+        signame = "wrong"
+
+        Signal.trap("INT") { |signo| signame = Signal.signame(signo); caught = 1;  }
+        Process.kill("INT", 0)
+
+        sleep 0.01 while caught==0
+        assert_equal(signame, "INT")
+      end
+    ensure
+      Signal.trap("INT", "DEFAULT")
+    end
+  end
 end
Index: signal.c
===================================================================
--- signal.c	(revision 37722)
+++ signal.c	(revision 37723)
@@ -207,6 +207,26 @@
     return 0;
 }
 
+/*
+ * call-seq:
+ *     Signal.signame(signo)  ->  string
+ *
+ *  convert signal number to signal name
+ *
+ *     Signal.trap("INT") { |signo| puts Signal.signame(signo) }
+ *     Process.kill("INT", 0)
+ *
+ *  <em>produces:</em>
+ *
+ *     INT
+ */
+static VALUE
+sig_signame(VALUE recv, VALUE signo)
+{
+    const char *signame = signo2signm(NUM2INT(signo));
+    return rb_str_new_cstr(signame);
+}
+
 const char *
 ruby_signal_name(int no)
 {
@@ -1086,6 +1106,7 @@
     rb_define_global_function("trap", sig_trap, -1);
     rb_define_module_function(mSignal, "trap", sig_trap, -1);
     rb_define_module_function(mSignal, "list", sig_list, 0);
+    rb_define_module_function(mSignal, "signame", sig_signame, 1);
 
     rb_define_method(rb_eSignal, "initialize", esignal_init, -1);
     rb_define_method(rb_eSignal, "signo", esignal_signo, 0);

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

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