From f438d67d421ecb0785038dbb4f824744b657a151 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Fri, 15 Jul 2022 00:15:41 +0900 Subject: [PATCH] new_stop_iteration with manual __init__ --- vm/src/vm/vm_new.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/vm/src/vm/vm_new.rs b/vm/src/vm/vm_new.rs index 6537c7d0d..609fbfea7 100644 --- a/vm/src/vm/vm_new.rs +++ b/vm/src/vm/vm_new.rs @@ -265,14 +265,21 @@ impl VirtualMachine { } pub fn new_stop_iteration(&self, value: Option) -> PyBaseExceptionRef { + let dict = self.ctx.new_dict(); let args = if let Some(value) = value { + // manually set `value` attribute like StopIteration.__init__ + dict.set_item("value", value.clone(), self) + .expect("dict.__setitem__ never fails"); vec![value] } else { Vec::new() }; - let exc = self.new_exception(self.ctx.exceptions.stop_iteration.to_owned(), args.clone()); - // To initialize the `value` attribute to first argument - self.set_exception_attr(exc, "value", args.get(0).cloned()) + + PyRef::new_ref( + PyBaseException::new(args, self), + self.ctx.exceptions.stop_iteration.to_owned(), + Some(dict), + ) } fn new_downcast_error( @@ -314,19 +321,4 @@ impl VirtualMachine { obj.as_object(), ) } - - fn set_exception_attr( - &self, - exc: PyBaseExceptionRef, - attr_name: &str, - attr_value: Option, - ) -> PyBaseExceptionRef { - match exc - .as_object() - .set_attr(attr_name, self.unwrap_or_none(attr_value), self) - { - Ok(_) => exc, - Err(e) => e, - } - } }