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

ruby-changes:34375

From: nobu <ko1@a...>
Date: Tue, 17 Jun 2014 12:37:54 +0900 (JST)
Subject: [ruby-changes:34375] nobu:r46456 (trunk): eval.c: pass unknown options

nobu	2014-06-17 12:37:47 +0900 (Tue, 17 Jun 2014)

  New Revision: 46456

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

  Log:
    eval.c: pass unknown options
    
    * eval.c (extract_raise_opts): pass unknown options to the
      exception, so that exception class can receive a hash argument.
      [ruby-core:63203] [Feature #8257]

  Modified files:
    trunk/ChangeLog
    trunk/eval.c
    trunk/test/ruby/test_exception.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46455)
+++ ChangeLog	(revision 46456)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jun 17 12:35:24 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* eval.c (extract_raise_opts): pass unknown options to the
+	  exception, so that exception class can receive a hash argument.
+	  [ruby-core:63203] [Feature #8257]
+
 Tue Jun 17 12:24:57 2014  Koichi Sasada  <ko1@a...>
 
 	* gc.c (obj_memsize_of): memsize_of(T_ZOMBIE) returns 0, not a rb_bug.
Index: eval.c
===================================================================
--- eval.c	(revision 46455)
+++ eval.c	(revision 46456)
@@ -606,12 +606,11 @@ extract_raise_opts(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/eval.c#L606
     if (argc > 0) {
 	VALUE opt = argv[argc-1];
 	if (RB_TYPE_P(opt, T_HASH)) {
-	    VALUE kw = rb_extract_keywords(&opt);
-	    if (!opt) --argc;
-	    if (kw) {
+	    if (!RHASH_EMPTY_P(opt)) {
 		ID keywords[1];
 		CONST_ID(keywords[0], "cause");
-		rb_get_kwargs(kw, keywords, 0, 1, opts);
+		rb_get_kwargs(opt, keywords, 0, -1-raise_max_opt, opts);
+		if (RHASH_EMPTY_P(opt)) --argc;
 		return argc;
 	    }
 	}
Index: test/ruby/test_exception.rb
===================================================================
--- test/ruby/test_exception.rb	(revision 46455)
+++ test/ruby/test_exception.rb	(revision 46456)
@@ -596,4 +596,25 @@ end.join https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L596
       raise cause: cause
     end
   end
+
+  def test_unknown_option
+    bug = '[ruby-core:63203] [Feature #8257] should pass unknown options'
+
+    exc = Class.new(RuntimeError) do
+      attr_reader :arg
+      def initialize(msg = nil)
+        @arg = msg
+        super(msg)
+      end
+    end
+
+    e = assert_raise(exc, bug) {raise exc, "foo" => "bar", foo: "bar"}
+    assert_equal({"foo" => "bar", foo: "bar"}, e.arg, bug)
+
+    e = assert_raise(exc, bug) {raise exc, "foo" => "bar", foo: "bar", cause: "zzz"}
+    assert_equal({"foo" => "bar", foo: "bar"}, e.arg, bug)
+
+    e = assert_raise(exc, bug) {raise exc, {}}
+    assert_equal({}, e.arg, bug)
+  end
 end

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

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