Skip to content

Commit dfb24d7

Browse files
Merge pull request RustPython#657 from skinny121/attribute_error
Use new_attribute_error in more places
2 parents a48340d + 65c26a1 commit dfb24d7

File tree

4 files changed

+10
-26
lines changed

4 files changed

+10
-26
lines changed

vm/src/obj/objfunction.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,9 @@ fn classmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
129129
let py_method = vm.ctx.new_bound_method(function, py_obj);
130130
Ok(py_method)
131131
}
132-
None => {
133-
let attribute_error = vm.context().exceptions.attribute_error.clone();
134-
Err(vm.new_exception(
135-
attribute_error,
136-
String::from("Attribute Error: classmethod must have 'function' attribute"),
137-
))
138-
}
132+
None => Err(vm.new_attribute_error(
133+
"Attribute Error: classmethod must have 'function' attribute".to_string(),
134+
)),
139135
}
140136
}
141137

@@ -162,13 +158,9 @@ fn staticmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
162158
);
163159
match cls.get_attr("function") {
164160
Some(function) => Ok(function),
165-
None => {
166-
let attribute_error = vm.context().exceptions.attribute_error.clone();
167-
Err(vm.new_exception(
168-
attribute_error,
169-
String::from("Attribute Error: staticmethod must have 'function' attribute"),
170-
))
171-
}
161+
None => Err(vm.new_attribute_error(
162+
"Attribute Error: staticmethod must have 'function' attribute".to_string(),
163+
)),
172164
}
173165
}
174166

vm/src/obj/objobject.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,7 @@ fn object_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
256256
} else if let Some(getter) = cls.get_attr("__getattr__") {
257257
vm.invoke(getter, vec![cls, name_str.clone()])
258258
} else {
259-
let attribute_error = vm.context().exceptions.attribute_error.clone();
260-
Err(vm.new_exception(
261-
attribute_error,
262-
format!("{} has no attribute '{}'", obj, name),
263-
))
259+
Err(vm.new_attribute_error(format!("{} has no attribute '{}'", obj, name)))
264260
}
265261
}
266262

vm/src/obj/objtype.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,7 @@ pub fn type_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
225225
} else if let Some(getter) = cls.get_attr("__getattr__") {
226226
vm.invoke(getter, vec![mcl, name_str.clone()])
227227
} else {
228-
let attribute_error = vm.context().exceptions.attribute_error.clone();
229-
Err(vm.new_exception(
230-
attribute_error,
231-
format!("{} has no attribute '{}'", cls, name),
232-
))
228+
Err(vm.new_attribute_error(format!("{} has no attribute '{}'", cls, name)))
233229
}
234230
}
235231

vm/src/vm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ impl VirtualMachine {
137137
}
138138

139139
pub fn new_attribute_error(&mut self, msg: String) -> PyObjectRef {
140-
let type_error = self.ctx.exceptions.attribute_error.clone();
141-
self.new_exception(type_error, msg)
140+
let attribute_error = self.ctx.exceptions.attribute_error.clone();
141+
self.new_exception(attribute_error, msg)
142142
}
143143

144144
pub fn new_type_error(&mut self, msg: String) -> PyObjectRef {

0 commit comments

Comments
 (0)