Browse files

* 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



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37723 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
1 parent 92cef13 commit 58282ed667d720816366c9e45c3560904196d078 @kosaki kosaki committed Nov 19, 2012
Showing with 49 additions and 0 deletions.
  1. +7 −0 ChangeLog
  2. +3 −0 NEWS
  3. +21 −0 signal.c
  4. +18 −0 test/ruby/test_signal.rb
View
7 ChangeLog
@@ -1,3 +1,10 @@
+Tue Nov 20 08:36:15 2012 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
+
+ * 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@ruby-lang.org>
* struct.c (rb_struct_each_pair): yield associated pairs so that
View
3 NEWS
@@ -105,6 +105,9 @@ with all sufficient information, see the ChangeLog file.
* 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.
View
21 signal.c
@@ -207,6 +207,26 @@ signo2signm(int no)
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 @@ Init_signal(void)
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);
View
18 test/ruby/test_signal.rb
@@ -240,4 +240,22 @@ def test_reserved_signal
}
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

0 comments on commit 58282ed

Please sign in to comment.