Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2aa7638

Browse files
committed
Try __sub__, next __rsub__, next give up
1 parentbddaf3e commit2aa7638

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
usesuper::obj::objobject;
1818
usesuper::obj::objtuple;
1919
usesuper::obj::objtype;
20-
usesuper::pyobject::{DictProtocol,PyContext,PyFuncArgs,PyObjectKind,PyObjectRef,PyResult};
20+
usesuper::pyobject::{
21+
AttributeProtocol,DictProtocol,PyContext,PyFuncArgs,PyObjectKind,PyObjectRef,PyResult,
22+
TypeProtocol,
23+
};
2124
usesuper::stdlib;
2225
usesuper::sysmodule;
2326

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

377380
pubfn_sub(&mutself,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+
}elseif 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
pubfn_add(&mutself,a:PyObjectRef,b:PyObjectRef) ->PyResult{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp