Skip to content

Commit 9ba9ca1

Browse files
Merge pull request RustPython#183 from BojanKogoj/bojan/objobject-__and__-default
WIP: Added default __and__
2 parents c4cbeda + ccd1534 commit 9ba9ca1

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

vm/src/vm.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,50 @@ impl VirtualMachine {
531531
}
532532

533533
pub fn _and(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
534-
self.call_method(&a, "__and__", vec![b])
534+
// 1. Try __and__, next __rand__, next, give up
535+
if let Ok(method) = self.get_method(a.clone(), "__and__") {
536+
match self.invoke(
537+
method,
538+
PyFuncArgs {
539+
args: vec![b.clone()],
540+
kwargs: vec![],
541+
},
542+
) {
543+
Ok(value) => return Ok(value),
544+
Err(err) => {
545+
if !objtype::isinstance(&err, &self.ctx.exceptions.not_implemented_error) {
546+
return Err(err);
547+
}
548+
}
549+
}
550+
}
551+
552+
// 2. try __rand__
553+
if let Ok(method) = self.get_method(b.clone(), "__rand__") {
554+
match self.invoke(
555+
method,
556+
PyFuncArgs {
557+
args: vec![a.clone()],
558+
kwargs: vec![],
559+
},
560+
) {
561+
Ok(value) => return Ok(value),
562+
Err(err) => {
563+
if !objtype::isinstance(&err, &self.ctx.exceptions.not_implemented_error) {
564+
return Err(err);
565+
}
566+
}
567+
}
568+
}
569+
570+
// 3. It all failed :(
571+
// Cannot and a and b
572+
let a_type_name = objtype::get_type_name(&a.typ());
573+
let b_type_name = objtype::get_type_name(&b.typ());
574+
Err(self.new_type_error(format!(
575+
"Unsupported operand types for '&': '{}' and '{}'",
576+
a_type_name, b_type_name
577+
)))
535578
}
536579

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

0 commit comments

Comments
 (0)