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

ruby-changes:26956

From: marcandre <ko1@a...>
Date: Sat, 2 Feb 2013 07:48:17 +0900 (JST)
Subject: [ruby-changes:26956] marcandRe: r39008 (trunk): * proc.c (proc_curry): Fix arity check [Bug #5747]

marcandre	2013-02-02 07:46:32 +0900 (Sat, 02 Feb 2013)

  New Revision: 39008

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

  Log:
    * proc.c (proc_curry): Fix arity check [Bug #5747]
    
    * test/ruby/test_proc.rb: Test for above

  Modified files:
    trunk/ChangeLog
    trunk/proc.c
    trunk/test/ruby/test_proc.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39007)
+++ ChangeLog	(revision 39008)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Feb  2 07:45:44 2013  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* proc.c (proc_curry): Fix arity check [Bug #5747]
+
+	* test/ruby/test_proc.rb: Test for above
+
 Sat Feb  2 07:44:15 2013  Marc-Andre Lafortune  <ruby-core@m...>
 
 	* proc.c: Add {*}_min_max_arity and refactor.
Index: proc.c
===================================================================
--- proc.c	(revision 39007)
+++ proc.c	(revision 39008)
@@ -2163,22 +2163,17 @@ curry(VALUE dummy, VALUE args, int argc, https://github.com/ruby/ruby/blob/trunk/proc.c#L2163
 static VALUE
 proc_curry(int argc, VALUE *argv, VALUE self)
 {
-    int sarity, marity = rb_proc_arity(self);
-    VALUE arity, opt = Qfalse;
-
-    if (marity < 0) {
-	marity = -marity - 1;
-	opt = Qtrue;
-    }
+    int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity);
+    VALUE arity;
 
     rb_scan_args(argc, argv, "01", &arity);
     if (NIL_P(arity)) {
-	arity = INT2FIX(marity);
+	arity = INT2FIX(min_arity);
     }
     else {
 	sarity = FIX2INT(arity);
-	if (rb_proc_lambda_p(self) && (sarity < marity || (sarity > marity && !opt))) {
-	    rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", sarity, marity);
+	if (rb_proc_lambda_p(self)) {
+	    rb_check_arity(sarity, min_arity, max_arity);
 	}
     }
 
Index: test/ruby/test_proc.rb
===================================================================
--- test/ruby/test_proc.rb	(revision 39007)
+++ test/ruby/test_proc.rb	(revision 39008)
@@ -270,6 +270,13 @@ class TestProc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_proc.rb#L270
     assert_equal(self, result[1])
   end
 
+  def test_curry_optional_params
+    obj = Object.new
+    def obj.foo(a, b=42); end
+    assert_raise(ArgumentError) { obj.method(:foo).to_proc.curry(3) }
+    assert_raise(ArgumentError) { ->(a, b=42){}.curry(3) }
+  end
+
   def test_dup_clone
     b = proc {|x| x + "bar" }
     class << b; attr_accessor :foo; end

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

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