diff --git a/vm/src/builtins/asyncgenerator.rs b/vm/src/builtins/asyncgenerator.rs index b25178092a..24fa94256d 100644 --- a/vm/src/builtins/asyncgenerator.rs +++ b/vm/src/builtins/asyncgenerator.rs @@ -155,10 +155,7 @@ impl PyAsyncGenWrappedValue { match_class!(match val { val @ Self => { ag.running_async.store(false); - Err(vm.new_exception( - vm.ctx.exceptions.stop_iteration.clone(), - vec![val.0.clone()], - )) + Err(vm.new_stop_iteration(Some(val.0.clone()))) } val => Ok(val), }) @@ -307,7 +304,7 @@ impl PyAsyncGenAThrow { } if self.ag.inner.closed() { self.state.store(AwaitableState::Closed); - return Err(vm.new_exception_empty(vm.ctx.exceptions.stop_iteration.clone())); + return Err(vm.new_stop_iteration(None)); } if !vm.is_none(&val) { return Err(vm.new_runtime_error( @@ -398,7 +395,7 @@ impl PyAsyncGenAThrow { && (exc.isinstance(&vm.ctx.exceptions.stop_async_iteration) || exc.isinstance(&vm.ctx.exceptions.generator_exit)) { - vm.new_exception_empty(vm.ctx.exceptions.stop_iteration.clone()) + vm.new_stop_iteration(None) } else { exc } diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 6a5cc5b0ec..9dabd2e172 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -166,10 +166,7 @@ impl IntoPyResult for PyIterReturn { fn into_pyresult(self, vm: &VirtualMachine) -> PyResult { match self { Self::Return(obj) => Ok(obj), - Self::StopIteration(v) => Err({ - let args = if let Some(v) = v { vec![v] } else { Vec::new() }; - vm.new_exception(vm.ctx.exceptions.stop_iteration.clone(), args) - }), + Self::StopIteration(v) => Err(vm.new_stop_iteration(v)), } } } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index f1b64ae3fb..fca9bd6731 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -800,9 +800,13 @@ impl VirtualMachine { self.new_exception_msg(memory_error_type, msg) } - pub fn new_stop_iteration(&self) -> PyBaseExceptionRef { - let stop_iteration_type = self.ctx.exceptions.stop_iteration.clone(); - self.new_exception_empty(stop_iteration_type) + pub fn new_stop_iteration(&self, value: Option) -> PyBaseExceptionRef { + let args = if let Some(value) = value { + vec![value] + } else { + Vec::new() + }; + self.new_exception(self.ctx.exceptions.stop_iteration.clone(), args) } #[track_caller]