Skip to content

Commit 3379fda

Browse files
committed
Clean-up cases where vm.get_attribute is used for magic-method lookup.
1 parent 25cfb51 commit 3379fda

File tree

2 files changed

+11
-36
lines changed

2 files changed

+11
-36
lines changed

vm/src/builtins.rs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ fn dir_object(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyObjectRef {
5050

5151
fn builtin_abs(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5252
arg_check!(vm, args, required = [(x, None)]);
53-
match vm.get_attribute(x.clone(), &"__abs__") {
54-
Ok(attrib) => vm.invoke(attrib, PyFuncArgs::new(vec![], vec![])),
53+
match vm.call_method(x, "__abs__", vec![]) {
54+
Ok(result) => Ok(result),
5555
Err(..) => Err(vm.new_type_error("bad operand for abs".to_string())),
5656
}
5757
}
@@ -134,8 +134,8 @@ fn builtin_dir(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
134134

135135
fn builtin_divmod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
136136
arg_check!(vm, args, required = [(x, None), (y, None)]);
137-
match vm.get_attribute(x.clone(), &"__divmod__") {
138-
Ok(attrib) => vm.invoke(attrib, PyFuncArgs::new(vec![y.clone()], vec![])),
137+
match vm.call_method(&x.clone(), "__divmod__", vec![y.clone()]) {
138+
Ok(result) => Ok(result),
139139
Err(..) => Err(vm.new_type_error("unsupported operand type(s) for divmod".to_string())),
140140
}
141141
}
@@ -218,11 +218,7 @@ fn builtin_getattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
218218
args,
219219
required = [(obj, None), (attr, Some(vm.ctx.str_type()))]
220220
);
221-
if let PyObjectKind::String { ref value } = attr.borrow().kind {
222-
vm.get_attribute(obj.clone(), value)
223-
} else {
224-
panic!("argument checking failure: attr not string")
225-
}
221+
vm.call_method(&obj.clone(), "__getattribute__", vec![attr.clone()])
226222
}
227223

228224
// builtin_globals
@@ -233,15 +229,11 @@ fn builtin_hasattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
233229
args,
234230
required = [(obj, None), (attr, Some(vm.ctx.str_type()))]
235231
);
236-
if let PyObjectKind::String { ref value } = attr.borrow().kind {
237-
let has_attr = match vm.get_attribute(obj.clone(), value) {
238-
Ok(..) => true,
239-
Err(..) => false,
240-
};
241-
Ok(vm.context().new_bool(has_attr))
242-
} else {
243-
panic!("argument checking failure: attr not string")
244-
}
232+
let has_attr = match vm.call_method(&obj.clone(), "__getattribute__", vec![attr.clone()]) {
233+
Ok(..) => true,
234+
Err(..) => false,
235+
};
236+
Ok(vm.context().new_bool(has_attr))
245237
}
246238

247239
fn builtin_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -306,19 +298,7 @@ fn builtin_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
306298
PyObjectKind::Tuple { ref elements } => {
307299
Ok(vm.context().new_int(elements.len().to_bigint().unwrap()))
308300
}
309-
_ => {
310-
let len_method_name = "__len__".to_string();
311-
match vm.get_attribute(obj.clone(), &len_method_name) {
312-
Ok(value) => vm.invoke(value, PyFuncArgs::default()),
313-
Err(..) => Err(vm.context().new_str(
314-
format!(
315-
"TypeError: object of this {:?} type has no method {:?}",
316-
obj, len_method_name
317-
)
318-
.to_string(),
319-
)),
320-
}
321-
}
301+
_ => vm.call_method(&obj.clone(), "__len__", vec![]),
322302
}
323303
}
324304

vm/src/obj/objtype.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,6 @@ fn type_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
300300
Ok(vm.new_str(format!("<class '{}'>", type_name)))
301301
}
302302

303-
pub fn call(vm: &mut VirtualMachine, typ: PyObjectRef, args: PyFuncArgs) -> PyResult {
304-
let function = vm.get_attribute(typ, &String::from("__call__"))?;
305-
vm.invoke(function, args)
306-
}
307-
308303
#[cfg(test)]
309304
mod tests {
310305
use super::{linearise_mro, new};

0 commit comments

Comments
 (0)