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

ruby-changes:23086

From: shugo <ko1@a...>
Date: Mon, 26 Mar 2012 19:52:13 +0900 (JST)
Subject: [ruby-changes:23086] shugo:r35136 (trunk): * enumerator.c (inspect_enumerator): show method arguments of

shugo	2012-03-26 19:52:02 +0900 (Mon, 26 Mar 2012)

  New Revision: 35136

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

  Log:
    * enumerator.c (inspect_enumerator): show method arguments of
      lazy enumerators correctly.

  Modified files:
    trunk/ChangeLog
    trunk/enumerator.c
    trunk/test/ruby/test_lazy_enumerator.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 35135)
+++ ChangeLog	(revision 35136)
@@ -1,3 +1,8 @@
+Mon Mar 26 11:46:23 2012  Shugo Maeda  <shugo@r...>
+
+	* enumerator.c (inspect_enumerator): show method arguments of
+	  lazy enumerators correctly.
+
 Mon Mar 26 13:51:23 2012  Nobuyoshi Nakada  <nobu@r...>
 
 	* win32/win32.c (check_if_dir, check_if_wdir): fix for Visual C++
Index: enumerator.c
===================================================================
--- enumerator.c	(revision 35135)
+++ enumerator.c	(revision 35136)
@@ -858,7 +858,7 @@
 {
     struct enumerator *e;
     const char *cname;
-    VALUE eobj, str, method;
+    VALUE eobj, eargs, str, method;
     int tainted, untrusted;
 
     TypedData_Get_Struct(obj, struct enumerator, &enumerator_data_type, e);
@@ -891,26 +891,32 @@
 	rb_str_buf_cat2(str, ":");
 	rb_str_buf_cat2(str, rb_id2name(e->meth));
     }
-    else if (RTEST(method)) {
+    else if (method != Qfalse) {
 	Check_Type(method, T_SYMBOL);
 	rb_str_buf_cat2(str, ":");
 	rb_str_buf_cat2(str, rb_id2name(SYM2ID(method)));
     }
 
-    if (e->args) {
-	long   argc = RARRAY_LEN(e->args);
-	VALUE *argv = RARRAY_PTR(e->args);
+    eargs = rb_iv_get(obj, "arguments");
+    if (NIL_P(eargs)) {
+	eargs = e->args;
+    }
+    if (eargs != Qfalse) {
+	long   argc = RARRAY_LEN(eargs);
+	VALUE *argv = RARRAY_PTR(eargs);
 
-	rb_str_buf_cat2(str, "(");
+	if (argc > 0) {
+	    rb_str_buf_cat2(str, "(");
 
-	while (argc--) {
-	    VALUE arg = *argv++;
+	    while (argc--) {
+		VALUE arg = *argv++;
 
-	    rb_str_concat(str, rb_inspect(arg));
-	    rb_str_buf_cat2(str, argc > 0 ? ", " : ")");
+		rb_str_concat(str, rb_inspect(arg));
+		rb_str_buf_cat2(str, argc > 0 ? ", " : ")");
 
-	    if (OBJ_TAINTED(arg)) tainted = TRUE;
-	    if (OBJ_UNTRUSTED(arg)) untrusted = TRUE;
+		if (OBJ_TAINTED(arg)) tainted = TRUE;
+		if (OBJ_UNTRUSTED(arg)) untrusted = TRUE;
+	    }
 	}
     }
 
@@ -1249,13 +1255,20 @@
     return self;
 }
 
-/* A macro to set the current method name to lazy and return lazy. */
-#define RETURN_LAZY(lazy) do { \
-    VALUE result = lazy; \
-    ID id = rb_frame_this_func(); \
-    rb_iv_set(result, "method", ID2SYM(id)); \
-    return result; \
-} while (0)
+static VALUE
+lazy_set_method(VALUE lazy, VALUE args)
+{
+    ID id = rb_frame_this_func();
+    rb_iv_set(lazy, "method", ID2SYM(id));
+    if (NIL_P(args)) {
+	/* Qfalse indicates that the arguments are empty */
+	rb_iv_set(lazy, "arguments", Qfalse);
+    }
+    else {
+	rb_iv_set(lazy, "arguments", args);
+    }
+    return lazy;
+}
 
 /*
  * call-seq:
@@ -1315,7 +1328,9 @@
 	rb_raise(rb_eArgError, "tried to call lazy map without a block");
     }
 
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_map_func, 0));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 lazy_map_func, 0),
+			   Qnil);
 }
 
 static VALUE
@@ -1377,7 +1392,9 @@
 	rb_raise(rb_eArgError, "tried to call lazy flat_map without a block");
     }
 
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_flat_map_func, 0));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 lazy_flat_map_func, 0),
+			   Qnil);
 }
 
 static VALUE
@@ -1398,7 +1415,9 @@
 	rb_raise(rb_eArgError, "tried to call lazy select without a block");
     }
 
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_select_func, 0));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 lazy_select_func, 0),
+			   Qnil);
 }
 
 static VALUE
@@ -1419,7 +1438,9 @@
 	rb_raise(rb_eArgError, "tried to call lazy reject without a block");
     }
 
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_reject_func, 0));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 lazy_reject_func, 0),
+			   Qnil);
 }
 
 static VALUE
@@ -1449,9 +1470,11 @@
 static VALUE
 lazy_grep(VALUE obj, VALUE pattern)
 {
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj,
-			      rb_block_given_p() ? lazy_grep_iter : lazy_grep_func,
-			      pattern));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 rb_block_given_p() ?
+					 lazy_grep_iter : lazy_grep_func,
+					 pattern),
+			   rb_ary_new3(1, pattern));
 }
 
 static VALUE
@@ -1498,7 +1521,9 @@
 	rb_ary_push(ary, rb_funcall(argv[i], id_lazy, 0));
     }
 
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_zip_func, ary));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 lazy_zip_func, ary),
+			   rb_ary_new4(argc, argv));
 }
 
 static VALUE
@@ -1533,8 +1558,9 @@
 	argc = 3;
     }
     memo = NEW_MEMO(0, 0, len);
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, argc, argv, lazy_take_func,
-			      (VALUE) memo));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, argc, argv,
+					 lazy_take_func, (VALUE) memo),
+			   rb_ary_new3(1, n));
 }
 
 static VALUE
@@ -1549,7 +1575,9 @@
 static VALUE
 lazy_take_while(VALUE obj)
 {
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_take_while_func, 0));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 lazy_take_while_func, 0),
+			   Qnil);
 }
 
 static VALUE
@@ -1576,8 +1604,9 @@
 	rb_raise(rb_eArgError, "attempt to drop negative size");
     }
     memo = NEW_MEMO(0, 0, len);
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_drop_func,
-			      (VALUE) memo));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 lazy_drop_func, (VALUE) memo),
+			   rb_ary_new3(1, n));
 }
 
 static VALUE
@@ -1600,8 +1629,9 @@
     NODE *memo;
 
     memo = NEW_MEMO(0, 0, FALSE);
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_drop_while_func,
-			      (VALUE) memo));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
+					 lazy_drop_while_func, (VALUE) memo),
+			   Qnil);
 }
 
 static VALUE
@@ -1625,8 +1655,10 @@
     if (argc > 0) {
 	rb_ary_cat(args, argv, argc);
     }
-    RETURN_LAZY(rb_block_call(rb_cLazy, id_new, len, RARRAY_PTR(args),
-			      lazy_cycle_func, args /* prevent from GC */));
+    return lazy_set_method(rb_block_call(rb_cLazy, id_new, len,
+					 RARRAY_PTR(args), lazy_cycle_func,
+					 args /* prevent from GC */),
+			   rb_ary_new4(argc, argv));
 }
 
 static VALUE
Index: test/ruby/test_lazy_enumerator.rb
===================================================================
--- test/ruby/test_lazy_enumerator.rb	(revision 35135)
+++ test/ruby/test_lazy_enumerator.rb	(revision 35136)
@@ -291,9 +291,21 @@
                  "foo".chars.lazy.inspect)
     assert_equal("#<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:map>",
                  (1..10).lazy.map {}.inspect)
-    l = (1..10).lazy.map {}.collect {}.flat_map {}.collect_concat {}.select {}.find_all {}.reject {}.grep(1).zip(?a..?c).take(10).take_while {}.drop(3).drop_while {}.cycle
+    assert_equal("#<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:take(0)>",
+                 (1..10).lazy.take(0).inspect)
+    assert_equal("#<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:take(3)>",
+                 (1..10).lazy.take(3).inspect)
+    assert_equal('#<Enumerator::Lazy: #<Enumerator::Lazy: "a".."c">:grep(/b/)>',
+                 ("a".."c").lazy.grep(/b/).inspect)
+    assert_equal("#<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:cycle(3)>",
+                 (1..10).lazy.cycle(3).inspect)
+    assert_equal("#<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:cycle>",
+                 (1..10).lazy.cycle.inspect)
+    assert_equal("#<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:cycle(3)>",
+                 (1..10).lazy.cycle(3).inspect)
+    l = (1..10).lazy.map {}.collect {}.flat_map {}.collect_concat {}.select {}.find_all {}.reject {}.grep(1).zip(?a..?c).take(10).take_while {}.drop(3).drop_while {}.cycle(3)
     assert_equal(<<EOS.chomp, l.inspect)
-#<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:map>:collect>:flat_map>:collect_concat>:select>:find_all>:reject>:grep>:zip>:take>:take_while>:drop>:drop_while>:cycle>
+#<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:map>:collect>:flat_map>:collect_concat>:select>:find_all>:reject>:grep(1)>:zip("a".."c")>:take(10)>:take_while>:drop(3)>:drop_while>:cycle(3)>
 EOS
   end
 end

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

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