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

Commit9ba9ca1

Browse files
Merge pull requestRustPython#183 from BojanKogoj/bojan/objobject-__and__-default
WIP: Added default __and__
2 parentsc4cbeda +ccd1534 commit9ba9ca1

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

‎vm/src/vm.rs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,50 @@ impl VirtualMachine {
531531
}
532532

533533
pubfn_and(&mutself,a:PyObjectRef,b:PyObjectRef) ->PyResult{
534-
self.call_method(&a,"__and__",vec![b])
534+
// 1. Try __and__, next __rand__, next, give up
535+
ifletOk(method) =self.get_method(a.clone(),"__and__"){
536+
matchself.invoke(
537+
method,
538+
PyFuncArgs{
539+
args:vec![b.clone()],
540+
kwargs:vec![],
541+
},
542+
){
543+
Ok(value) =>returnOk(value),
544+
Err(err) =>{
545+
if !objtype::isinstance(&err,&self.ctx.exceptions.not_implemented_error){
546+
returnErr(err);
547+
}
548+
}
549+
}
550+
}
551+
552+
// 2. try __rand__
553+
ifletOk(method) =self.get_method(b.clone(),"__rand__"){
554+
matchself.invoke(
555+
method,
556+
PyFuncArgs{
557+
args:vec![a.clone()],
558+
kwargs:vec![],
559+
},
560+
){
561+
Ok(value) =>returnOk(value),
562+
Err(err) =>{
563+
if !objtype::isinstance(&err,&self.ctx.exceptions.not_implemented_error){
564+
returnErr(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
pubfn_eq(&mutself,a:&PyObjectRef,b:PyObjectRef) ->PyResult{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp