Skip to content

Commit 2aa7638

Browse files
committed
Try __sub__, next __rsub__, next give up
1 parent bddaf3e commit 2aa7638

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

vm/src/vm.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use super::obj::objlist;
1717
use super::obj::objobject;
1818
use super::obj::objtuple;
1919
use super::obj::objtype;
20-
use super::pyobject::{DictProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult};
20+
use super::pyobject::{
21+
AttributeProtocol, DictProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult,
22+
TypeProtocol,
23+
};
2124
use super::stdlib;
2225
use super::sysmodule;
2326

@@ -375,7 +378,20 @@ impl VirtualMachine {
375378
}
376379

377380
pub fn _sub(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
378-
self.call_method(&a, "__sub__", vec![b])
381+
// Try __sub__, next __rsub__, next, give up
382+
if a.has_attr("__sub__") {
383+
self.call_method(&a, "__sub__", vec![b])
384+
} else if b.has_attr("__rsub__") {
385+
self.call_method(&b, "__rsub__", vec![a])
386+
} else {
387+
// Cannot sub a and b
388+
let a_type_name = objtype::get_type_name(&a.typ());
389+
let b_type_name = objtype::get_type_name(&b.typ());
390+
Err(self.new_type_error(format!(
391+
"Unsupported operand types for '-': '{}' and '{}'",
392+
a_type_name, b_type_name
393+
)))
394+
}
379395
}
380396

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

0 commit comments

Comments
 (0)