diff --git a/vm/src/obj/objfunction.rs b/vm/src/obj/objfunction.rs index 2f278ca076..8b9dd70d37 100644 --- a/vm/src/obj/objfunction.rs +++ b/vm/src/obj/objfunction.rs @@ -129,13 +129,9 @@ fn classmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let py_method = vm.ctx.new_bound_method(function, py_obj); Ok(py_method) } - None => { - let attribute_error = vm.context().exceptions.attribute_error.clone(); - Err(vm.new_exception( - attribute_error, - String::from("Attribute Error: classmethod must have 'function' attribute"), - )) - } + None => Err(vm.new_attribute_error( + "Attribute Error: classmethod must have 'function' attribute".to_string(), + )), } } @@ -162,13 +158,9 @@ fn staticmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { ); match cls.get_attr("function") { Some(function) => Ok(function), - None => { - let attribute_error = vm.context().exceptions.attribute_error.clone(); - Err(vm.new_exception( - attribute_error, - String::from("Attribute Error: staticmethod must have 'function' attribute"), - )) - } + None => Err(vm.new_attribute_error( + "Attribute Error: staticmethod must have 'function' attribute".to_string(), + )), } } diff --git a/vm/src/obj/objobject.rs b/vm/src/obj/objobject.rs index 0d9d21f1ea..eb03b33932 100644 --- a/vm/src/obj/objobject.rs +++ b/vm/src/obj/objobject.rs @@ -256,11 +256,7 @@ fn object_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } else if let Some(getter) = cls.get_attr("__getattr__") { vm.invoke(getter, vec![cls, name_str.clone()]) } else { - let attribute_error = vm.context().exceptions.attribute_error.clone(); - Err(vm.new_exception( - attribute_error, - format!("{} has no attribute '{}'", obj, name), - )) + Err(vm.new_attribute_error(format!("{} has no attribute '{}'", obj, name))) } } diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index be4591dfcd..5a975556fc 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -225,11 +225,7 @@ pub fn type_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult } else if let Some(getter) = cls.get_attr("__getattr__") { vm.invoke(getter, vec![mcl, name_str.clone()]) } else { - let attribute_error = vm.context().exceptions.attribute_error.clone(); - Err(vm.new_exception( - attribute_error, - format!("{} has no attribute '{}'", cls, name), - )) + Err(vm.new_attribute_error(format!("{} has no attribute '{}'", cls, name))) } } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 0f95bf66c6..361f776aa2 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -137,8 +137,8 @@ impl VirtualMachine { } pub fn new_attribute_error(&mut self, msg: String) -> PyObjectRef { - let type_error = self.ctx.exceptions.attribute_error.clone(); - self.new_exception(type_error, msg) + let attribute_error = self.ctx.exceptions.attribute_error.clone(); + self.new_exception(attribute_error, msg) } pub fn new_type_error(&mut self, msg: String) -> PyObjectRef {