Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 90843e8

Browse files
wmeehantekknolagi
authored andcommitted
Don't clear GeneratorExit in athrow().send()
Summary: This functionality was changed in python/cpython#7467. Based on Facebook D27940847
1 parent ab3fdd3 commit 90843e8

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

library/builtins_test.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,8 @@ async def f():
16151615

16161616
self._assertOpIterState(op_iter, "_STATE_CLOSED")
16171617

1618-
def test_send_state_init_arg_is_none_generator_raises_generator_exit_raises_stop_iteration_state_closed( # noqa: B950
1618+
@supports_38_feature
1619+
def test_send_state_init_arg_is_none_generator_propagates_generator_exit(
16191620
self,
16201621
):
16211622
async def f():
@@ -1624,11 +1625,8 @@ async def f():
16241625
# Make athrow op_iter
16251626
op_iter = f().athrow(GeneratorExit)
16261627

1627-
with self.assertRaises(StopIteration) as exc:
1628+
with self.assertRaises(GeneratorExit):
16281629
op_iter.send(None)
1629-
self.assertIsNone(exc.exception.value)
1630-
1631-
self._assertOpIterState(op_iter, "_STATE_CLOSED")
16321630

16331631
def test_send_state_init_arg_is_none_generator_yields_raises_stop_iteration_with_value_state_iter( # noqa: B950
16341632
self,

runtime/generator-builtins.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ static RawObject asyncGenAcloseSend(Thread* thread, RawObject raw_self_obj,
434434
RawObject send_value_raw) {
435435
HandleScope scope(thread);
436436
Object self_obj(&scope, raw_self_obj);
437+
Object send_value(&scope, send_value_raw);
437438
if (!self_obj.isAsyncGeneratorAclose()) {
438439
return thread->raiseWithFmt(LayoutId::kTypeError,
439440
"Must be called with an async_generator_aclose "
@@ -454,7 +455,6 @@ static RawObject asyncGenAcloseSend(Thread* thread, RawObject raw_self_obj,
454455
// to make progress through async-like yields.
455456
Object res(&scope, NoneType::object());
456457
AsyncGenerator generator(&scope, self.generator());
457-
Object send_value(&scope, send_value_raw);
458458
if (state == AsyncGeneratorOpIterBase::State::Init) {
459459
if (!send_value.isNoneType()) {
460460
return thread->raiseWithFmt(
@@ -586,6 +586,7 @@ static RawObject asyncGenAsendSend(Thread* thread, RawObject raw_self_obj,
586586
RawObject send_value_raw) {
587587
HandleScope scope(thread);
588588
Object self_obj(&scope, raw_self_obj);
589+
Object send_value(&scope, send_value_raw);
589590
if (!self_obj.isAsyncGeneratorAsend()) {
590591
return thread->raiseWithFmt(LayoutId::kTypeError,
591592
"Must be called with an async_generator_asend "
@@ -601,7 +602,6 @@ static RawObject asyncGenAsendSend(Thread* thread, RawObject raw_self_obj,
601602
}
602603
// Only use primed value for initial send, and only if no other specific value
603604
// is provided.
604-
Object send_value(&scope, send_value_raw);
605605
if (state == AsyncGeneratorOpIterBase::State::Init) {
606606
if (send_value.isNoneType()) {
607607
send_value = self.value();
@@ -710,6 +710,7 @@ static RawObject asyncGenAthrowSend(Thread* thread, RawObject raw_self_obj,
710710
RawObject send_value_raw) {
711711
HandleScope scope(thread);
712712
Object self_obj(&scope, raw_self_obj);
713+
Object send_value(&scope, send_value_raw);
713714
if (!self_obj.isAsyncGeneratorAthrow()) {
714715
return thread->raiseWithFmt(LayoutId::kTypeError,
715716
"Must be called with an async_generator_athrow "
@@ -730,7 +731,6 @@ static RawObject asyncGenAthrowSend(Thread* thread, RawObject raw_self_obj,
730731
// through async-like yields.
731732
Object res(&scope, NoneType::object());
732733
AsyncGenerator generator(&scope, self.generator());
733-
Object send_value(&scope, send_value_raw);
734734
if (state == AsyncGeneratorOpIterBase::State::Init) {
735735
if (!send_value.isNoneType()) {
736736
return thread->raiseWithFmt(
@@ -747,15 +747,11 @@ static RawObject asyncGenAthrowSend(Thread* thread, RawObject raw_self_obj,
747747
// Handle StopAsyncIteration and GeneratorExit exceptions raised here.
748748
// Other exceptions are handled further down.
749749
if (res.isErrorException()) {
750-
if (thread->pendingExceptionMatches(LayoutId::kStopAsyncIteration)) {
750+
if (thread->pendingExceptionMatches(LayoutId::kStopAsyncIteration) ||
751+
thread->pendingExceptionMatches(LayoutId::kGeneratorExit)) {
751752
self.setState(AsyncGeneratorOpIterBase::State::Closed);
752753
return *res;
753754
}
754-
if (thread->pendingExceptionMatches(LayoutId::kGeneratorExit)) {
755-
self.setState(AsyncGeneratorOpIterBase::State::Closed);
756-
thread->clearPendingException();
757-
return thread->raiseStopIteration();
758-
}
759755
}
760756
} else {
761757
DCHECK(state == AsyncGeneratorOpIterBase::State::Iter, "Unexpected state");

0 commit comments

Comments
 (0)