mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enumerator.c (inspect_enumerator): show method arguments of
lazy enumerators correctly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35136 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ddc7bcd60e
commit
b9f67c4149
3 changed files with 87 additions and 38 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Mon Mar 26 11:46:23 2012 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
|
* enumerator.c (inspect_enumerator): show method arguments of
|
||||||
|
lazy enumerators correctly.
|
||||||
|
|
||||||
Mon Mar 26 13:51:23 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Mon Mar 26 13:51:23 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* win32/win32.c (check_if_dir, check_if_wdir): fix for Visual C++
|
* win32/win32.c (check_if_dir, check_if_wdir): fix for Visual C++
|
||||||
|
|
90
enumerator.c
90
enumerator.c
|
@ -858,7 +858,7 @@ inspect_enumerator(VALUE obj, VALUE dummy, int recur)
|
||||||
{
|
{
|
||||||
struct enumerator *e;
|
struct enumerator *e;
|
||||||
const char *cname;
|
const char *cname;
|
||||||
VALUE eobj, str, method;
|
VALUE eobj, eargs, str, method;
|
||||||
int tainted, untrusted;
|
int tainted, untrusted;
|
||||||
|
|
||||||
TypedData_Get_Struct(obj, struct enumerator, &enumerator_data_type, e);
|
TypedData_Get_Struct(obj, struct enumerator, &enumerator_data_type, e);
|
||||||
|
@ -891,16 +891,21 @@ inspect_enumerator(VALUE obj, VALUE dummy, int recur)
|
||||||
rb_str_buf_cat2(str, ":");
|
rb_str_buf_cat2(str, ":");
|
||||||
rb_str_buf_cat2(str, rb_id2name(e->meth));
|
rb_str_buf_cat2(str, rb_id2name(e->meth));
|
||||||
}
|
}
|
||||||
else if (RTEST(method)) {
|
else if (method != Qfalse) {
|
||||||
Check_Type(method, T_SYMBOL);
|
Check_Type(method, T_SYMBOL);
|
||||||
rb_str_buf_cat2(str, ":");
|
rb_str_buf_cat2(str, ":");
|
||||||
rb_str_buf_cat2(str, rb_id2name(SYM2ID(method)));
|
rb_str_buf_cat2(str, rb_id2name(SYM2ID(method)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e->args) {
|
eargs = rb_iv_get(obj, "arguments");
|
||||||
long argc = RARRAY_LEN(e->args);
|
if (NIL_P(eargs)) {
|
||||||
VALUE *argv = RARRAY_PTR(e->args);
|
eargs = e->args;
|
||||||
|
}
|
||||||
|
if (eargs != Qfalse) {
|
||||||
|
long argc = RARRAY_LEN(eargs);
|
||||||
|
VALUE *argv = RARRAY_PTR(eargs);
|
||||||
|
|
||||||
|
if (argc > 0) {
|
||||||
rb_str_buf_cat2(str, "(");
|
rb_str_buf_cat2(str, "(");
|
||||||
|
|
||||||
while (argc--) {
|
while (argc--) {
|
||||||
|
@ -913,6 +918,7 @@ inspect_enumerator(VALUE obj, VALUE dummy, int recur)
|
||||||
if (OBJ_UNTRUSTED(arg)) untrusted = TRUE;
|
if (OBJ_UNTRUSTED(arg)) untrusted = TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rb_str_buf_cat2(str, ">");
|
rb_str_buf_cat2(str, ">");
|
||||||
|
|
||||||
|
@ -1249,13 +1255,20 @@ lazy_initialize(int argc, VALUE *argv, VALUE self)
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* A macro to set the current method name to lazy and return lazy. */
|
static VALUE
|
||||||
#define RETURN_LAZY(lazy) do { \
|
lazy_set_method(VALUE lazy, VALUE args)
|
||||||
VALUE result = lazy; \
|
{
|
||||||
ID id = rb_frame_this_func(); \
|
ID id = rb_frame_this_func();
|
||||||
rb_iv_set(result, "method", ID2SYM(id)); \
|
rb_iv_set(lazy, "method", ID2SYM(id));
|
||||||
return result; \
|
if (NIL_P(args)) {
|
||||||
} while (0)
|
/* Qfalse indicates that the arguments are empty */
|
||||||
|
rb_iv_set(lazy, "arguments", Qfalse);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rb_iv_set(lazy, "arguments", args);
|
||||||
|
}
|
||||||
|
return lazy;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
|
@ -1315,7 +1328,9 @@ lazy_map(VALUE obj)
|
||||||
rb_raise(rb_eArgError, "tried to call lazy map without a block");
|
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
|
static VALUE
|
||||||
|
@ -1377,7 +1392,9 @@ lazy_flat_map(VALUE obj)
|
||||||
rb_raise(rb_eArgError, "tried to call lazy flat_map without a block");
|
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
|
static VALUE
|
||||||
|
@ -1398,7 +1415,9 @@ lazy_select(VALUE obj)
|
||||||
rb_raise(rb_eArgError, "tried to call lazy select without a block");
|
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
|
static VALUE
|
||||||
|
@ -1419,7 +1438,9 @@ lazy_reject(VALUE obj)
|
||||||
rb_raise(rb_eArgError, "tried to call lazy reject without a block");
|
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
|
static VALUE
|
||||||
|
@ -1449,9 +1470,11 @@ lazy_grep_iter(VALUE val, VALUE m, int argc, VALUE *argv)
|
||||||
static VALUE
|
static VALUE
|
||||||
lazy_grep(VALUE obj, VALUE pattern)
|
lazy_grep(VALUE obj, VALUE pattern)
|
||||||
{
|
{
|
||||||
RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj,
|
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
|
||||||
rb_block_given_p() ? lazy_grep_iter : lazy_grep_func,
|
rb_block_given_p() ?
|
||||||
pattern));
|
lazy_grep_iter : lazy_grep_func,
|
||||||
|
pattern),
|
||||||
|
rb_ary_new3(1, pattern));
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -1498,7 +1521,9 @@ lazy_zip(int argc, VALUE *argv, VALUE obj)
|
||||||
rb_ary_push(ary, rb_funcall(argv[i], id_lazy, 0));
|
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
|
static VALUE
|
||||||
|
@ -1533,8 +1558,9 @@ lazy_take(VALUE obj, VALUE n)
|
||||||
argc = 3;
|
argc = 3;
|
||||||
}
|
}
|
||||||
memo = NEW_MEMO(0, 0, len);
|
memo = NEW_MEMO(0, 0, len);
|
||||||
RETURN_LAZY(rb_block_call(rb_cLazy, id_new, argc, argv, lazy_take_func,
|
return lazy_set_method(rb_block_call(rb_cLazy, id_new, argc, argv,
|
||||||
(VALUE) memo));
|
lazy_take_func, (VALUE) memo),
|
||||||
|
rb_ary_new3(1, n));
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -1549,7 +1575,9 @@ lazy_take_while_func(VALUE val, VALUE args, int argc, VALUE *argv)
|
||||||
static VALUE
|
static VALUE
|
||||||
lazy_take_while(VALUE obj)
|
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
|
static VALUE
|
||||||
|
@ -1576,8 +1604,9 @@ lazy_drop(VALUE obj, VALUE n)
|
||||||
rb_raise(rb_eArgError, "attempt to drop negative size");
|
rb_raise(rb_eArgError, "attempt to drop negative size");
|
||||||
}
|
}
|
||||||
memo = NEW_MEMO(0, 0, len);
|
memo = NEW_MEMO(0, 0, len);
|
||||||
RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_drop_func,
|
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
|
||||||
(VALUE) memo));
|
lazy_drop_func, (VALUE) memo),
|
||||||
|
rb_ary_new3(1, n));
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -1600,8 +1629,9 @@ lazy_drop_while(VALUE obj)
|
||||||
NODE *memo;
|
NODE *memo;
|
||||||
|
|
||||||
memo = NEW_MEMO(0, 0, FALSE);
|
memo = NEW_MEMO(0, 0, FALSE);
|
||||||
RETURN_LAZY(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_drop_while_func,
|
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
|
||||||
(VALUE) memo));
|
lazy_drop_while_func, (VALUE) memo),
|
||||||
|
Qnil);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -1625,8 +1655,10 @@ lazy_cycle(int argc, VALUE *argv, VALUE obj)
|
||||||
if (argc > 0) {
|
if (argc > 0) {
|
||||||
rb_ary_cat(args, argv, argc);
|
rb_ary_cat(args, argv, argc);
|
||||||
}
|
}
|
||||||
RETURN_LAZY(rb_block_call(rb_cLazy, id_new, len, RARRAY_PTR(args),
|
return lazy_set_method(rb_block_call(rb_cLazy, id_new, len,
|
||||||
lazy_cycle_func, args /* prevent from GC */));
|
RARRAY_PTR(args), lazy_cycle_func,
|
||||||
|
args /* prevent from GC */),
|
||||||
|
rb_ary_new4(argc, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
|
|
@ -291,9 +291,21 @@ class TestLazyEnumerator < Test::Unit::TestCase
|
||||||
"foo".chars.lazy.inspect)
|
"foo".chars.lazy.inspect)
|
||||||
assert_equal("#<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:map>",
|
assert_equal("#<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:map>",
|
||||||
(1..10).lazy.map {}.inspect)
|
(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)
|
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
|
EOS
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue