8000 Implement __sub__/__rsub__. by cthulahoops · Pull Request #188 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Implement __sub__/__rsub__. #188

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 3 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions tests/snippets/subtraction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
assert 5 - 3 == 2

class Complex():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note complex builtin type is also present, no issue here though.

def __init__(self, real, imag):
self.real = real
self.imag = imag

def __repr__(self):
return "Com" + str((self.real, self.imag))

def __sub__(self, other):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good check! Here lies the proof of the pudding indeed!

return Complex(self.real - other, self.imag)

def __rsub__(self, other):
return Complex(other - self.real, -self.imag)

def __eq__(self, other):
return self.real == other.real and self.imag == other.imag

assert Complex(4, 5) - 3 == Complex(1, 5)
assert 7 - Complex(4, 5) == Complex(3, -5)
23 changes: 16 additions & 7 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,22 @@ impl VirtualMachine {

pub fn _sub(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
// Try __sub__, next __rsub__, next, give up
self.call_method(&a, "__sub__", vec![b])
/*
if a.has_attr("__sub__") {
self.call_method(&a, "__sub__", vec![b])
} else if b.has_attr("__rsub__") {
self.call_method(&b, "__rsub__", vec![a])
if let Ok(method) = self.get_method(a.clone(), "__sub__") {
self.invoke(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add a invoke_with_args method which is less vebose.

method,
PyFuncArgs {
args: vec![b],
kwargs: vec![],
},
)
} else if let Ok(method) = self.get_method(b.clone(), "__rsub__") {
self.invoke(
method,
PyFuncArgs {
args: vec![a],
kwargs: vec![],
},
)
} else {
// Cannot sub a and b
let a_type_name = objtype::get_type_name(&a.typ());
Expand All @@ -453,7 +463,6 @@ impl VirtualMachine {
a_type_name, b_type_name
)))
}
*/
}

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