Skip to content

WIP: Added default __and__ #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,50 @@ impl VirtualMachine {
}

pub fn _and(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(&a, "__and__", vec![b])
// 1. Try __and__, next __rand__, next, give up
if let Ok(method) = self.get_method(a.clone(), "__and__") {
match self.invoke(
method,
PyFuncArgs {
args: vec![b.clone()],
kwargs: vec![],
},
) {
Ok(value) => return Ok(value),
Err(err) => {
if !objtype::isinstance(&err, &self.ctx.exceptions.not_implemented_error) {
return Err(err);
}
}
}
}

// 2. try __rand__
if let Ok(method) = self.get_method(b.clone(), "__rand__") {
match self.invoke(
method,
PyFuncArgs {
args: vec![a.clone()],
kwargs: vec![],
},
) {
Ok(value) => return Ok(value),
Err(err) => {
if !objtype::isinstance(&err, &self.ctx.exceptions.not_implemented_error) {
return Err(err);
}
}
}
}

// 3. It all failed :(
// Cannot and a and b
let a_type_name = objtype::get_type_name(&a.typ());
let b_type_name = objtype::get_type_name(&b.typ());
Err(self.new_type_error(format!(
"Unsupported operand types for '&': '{}' and '{}'",
a_type_name, b_type_name
)))
}

pub fn _eq(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
Expand Down