Fix stack trace for rescued StopIteration

This commit is contained in:
Marcelo Pereira 2022-07-29 15:09:54 +02:00 committed by Nobuyoshi Nakada
parent 82cd70ef93
commit f15123c34c
Notes: git 2023-07-15 06:25:02 +00:00
2 changed files with 33 additions and 2 deletions

View File

@ -165,7 +165,10 @@ static VALUE sym_each, sym_cycle, sym_yield;
static VALUE lazy_use_super_method;
extern ID ruby_static_id_cause;
#define id_call idCall
#define id_cause ruby_static_id_cause
#define id_each idEach
#define id_eqq idEqq
#define id_initialize idInitialize
@ -787,8 +790,16 @@ get_next_values(VALUE obj, struct enumerator *e)
{
VALUE curr, vs;
if (e->stop_exc)
rb_exc_raise(e->stop_exc);
if (e->stop_exc) {
VALUE exc = e->stop_exc;
VALUE result = rb_attr_get(exc, id_result);
VALUE mesg = rb_attr_get(exc, idMesg);
if (!NIL_P(mesg)) mesg = rb_str_dup(mesg);
VALUE stop_exc = rb_exc_new_str(rb_eStopIteration, mesg);
rb_ivar_set(stop_exc, id_cause, exc);
rb_ivar_set(stop_exc, id_result, result);
rb_exc_raise(stop_exc);
}
curr = rb_fiber_current();

View File

@ -244,6 +244,26 @@ class TestEnumerator < Test::Unit::TestCase
assert_equal(res, exc.result)
end
def test_stopiteration_rescue
e = [1].each
res = e.each {}
e.next
exc0 = assert_raise(StopIteration) { e.peek }
assert_include(exc0.backtrace.first, "test_enumerator.rb:#{__LINE__-1}:")
assert_nil(exc0.cause)
assert_equal(res, exc0.result)
exc1 = assert_raise(StopIteration) { e.next }
assert_include(exc1.backtrace.first, "test_enumerator.rb:#{__LINE__-1}:")
assert_same(exc0, exc1.cause)
assert_equal(res, exc1.result)
exc2 = assert_raise(StopIteration) { e.next }
assert_include(exc2.backtrace.first, "test_enumerator.rb:#{__LINE__-1}:")
assert_same(exc0, exc2.cause)
assert_equal(res, exc2.result)
end
def test_next_values
o = Object.new
def o.each