8000 Support magic methods for in-place operations by OddCoincidence · Pull Request #492 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Support magic methods for in-place operations #492

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 6 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions tests/snippets/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@
assert not 1 in a

assert_raises(ValueError, lambda: a.remove(10), 'Remove not exist element')

foo = bar = [1]
foo += [2]
assert (foo, bar) == ([1, 2], [1, 2])
1 change: 1 addition & 0 deletions vm/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub enum Instruction {
},
BinaryOperation {
op: BinaryOperator,
inplace: bool,
},
LoadAttr {
name: String,
Expand Down
9 changes: 5 additions & 4 deletions vm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ impl Compiler {
self.compile_expression(value)?;

// Perform operation:
self.compile_op(op);
self.compile_op(op, true);
self.compile_store(target)?;
}
ast::Statement::Delete { targets } => {
Expand Down Expand Up @@ -757,7 +757,7 @@ impl Compiler {
Ok(())
}

fn compile_op(&mut self, op: &ast::Operator) {
fn compile_op(&mut self, op: &ast::Operator, inplace: bool) {
let i = match op {
ast::Operator::Add => bytecode::BinaryOperator::Add,
ast::Operator::Sub => bytecode::BinaryOperator::Subtract,
Expand All @@ -773,7 +773,7 @@ impl Compiler {
ast::Operator::BitXor => bytecode::BinaryOperator::Xor,
ast::Operator::BitAnd => bytecode::BinaryOperator::And,
};
self.emit(Instruction::BinaryOperation { op: i });
self.emit(Instruction::BinaryOperation { op: i, inplace });
}

fn compile_test(
Expand Down Expand Up @@ -852,13 +852,14 @@ impl Compiler {
self.compile_expression(b)?;

// Perform operation:
self.compile_op(op);
self.compile_op(op, false);
}
ast::Expression::Subscript { a, b } => {
self.compile_expression(a)?;
self.compile_expression(b)?;
self.emit(Instruction::BinaryOperation {
op: bytecode::BinaryOperator::Subscript,
inplace: false,
});
}
ast::Expression::Unop { op, a } => {
Expand Down
36 changes: 25 additions & 11 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ impl Frame {
vm.call_method(&dict_obj, "__setitem__", vec![key, value])?;
Ok(None)
}
bytecode::Instruction::BinaryOperation { ref op } => self.execute_binop(vm, op),
bytecode::Instruction::BinaryOperation { ref op, inplace } => {
self.execute_binop(vm, op, *inplace)
}
bytecode::Instruction::LoadAttr { ref name } => self.load_attr(vm, name),
bytecode::Instruction::StoreAttr { ref name } => self.store_attr(vm, name),
bytecode::Instruction::DeleteAttr { ref name } => self.delete_attr(vm, name),
Expand Down Expand Up @@ -893,27 +895,39 @@ impl Frame {
&mut self,
vm: &mut VirtualMachine,
op: &bytecode::BinaryOperator,
inplace: bool,
) -> FrameResult {
let b_ref = self.pop_value();
let a_ref = self.pop_value();
let value = match *op {
bytecode::BinaryOperator::Subtract if inplace => vm._isub(a_ref, b_ref),
bytecode::BinaryOperator::Subtract => vm._sub(a_ref, b_ref),
bytecode::BinaryOperator::Add if inplace => vm._iadd(a_ref, b_ref),
bytecode::BinaryOperator::Add => vm._add(a_ref, b_ref),
bytecode::BinaryOperator::Multiply if inplace => vm._imul(a_ref, b_ref),
bytecode::BinaryOperator::Multiply => vm._mul(a_ref, b_ref),
bytecode::BinaryOperator::MatrixMultiply => {
vm.call_method(&a_ref, "__matmul__", vec![b_ref])
}
bytecode::BinaryOperator::MatrixMultiply if inplace => vm._imatmul(a_ref, b_ref),
bytecode::BinaryOperator::MatrixMultiply => vm._matmul(a_ref, b_ref),
bytecode::BinaryOperator::Power if inplace => vm._ipow(a_ref, b_ref),
bytecode::BinaryOperator::Power => vm._pow(a_ref, b_ref),
bytecode::BinaryOperator::Divide => vm._div(a_ref, b_ref),
bytecode::BinaryOperator::FloorDivide => {
vm.call_method(&a_ref, "__floordiv__", vec![b_ref])
}
bytecode::BinaryOperator::Divide if inplace => vm._itruediv(a_ref, b_ref),
bytecode::BinaryOperator::Divide => vm._truediv(a_ref, b_ref),
bytecode::BinaryOperator::FloorDivide if inplace => vm._ifloordiv(a_ref, b_ref),
bytecode::BinaryOperator::FloorDivide => vm._floordiv(a_ref, b_ref),
// TODO: Subscript should probably have its own op
bytecode::BinaryOperator::Subscript if inplace => unreachable!(),
bytecode::BinaryOperator::Subscript => self.subscript(vm, a_ref, b_ref),
bytecode::BinaryOperator::Modulo => vm._modulo(a_ref, b_ref),
bytecode::BinaryOperator::Lshift => vm.call_method(&a_ref, "__lshift__", vec![b_ref]),
bytecode::BinaryOperator::Rshift => vm.call_method(&a_ref, "__rshift__", vec![b_ref]),
bytecode::BinaryOperator::Modulo if inplace => vm._imod(a_ref, b_ref),
bytecode::BinaryOperator::Modulo => vm._mod(a_ref, b_ref),
bytecode::BinaryOperator::Lshift if inplace => vm._ilshift(a_ref, b_ref),
bytecode::BinaryOperator::Lshift => vm._lshift(a_ref, b_ref),
bytecode::BinaryOperator::Rshift if inplace => vm._irshift(a_ref, b_ref),
bytecode::BinaryOperator::Rshift => vm._rshift(a_ref, b_ref),
bytecode::BinaryOperator::Xor if inplace => vm._ixor(a_ref, b_ref),
bytecode::BinaryOperator::Xor => vm._xor(a_ref, b_ref),
bytecode::BinaryOperator::Or if inplace => vm._ior(a_ref, b_ref),
bytecode::BinaryOperator::Or => vm._or(a_ref, b_ref),
bytecode::BinaryOperator::And if inplace => vm._iand(a_ref, b_ref),
bytecode::BinaryOperator::And => vm._and(a_ref, b_ref),
}?;

Expand Down
19 changes: 19 additions & 0 deletions vm/src/obj/objlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ fn list_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
}

fn list_iadd(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(zelf, Some(vm.ctx.list_type())), (other, None)]
);

if objtype::isinstance(other, &vm.ctx.list_type()) {
let mut elements = get_mut_elements(zelf);
for elem in get_elements(other).iter() {
elements.push(elem.clone());
}
Ok(zelf.clone())
} else {
Ok(vm.ctx.not_implemented())
}
}

fn list_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(o, Some(vm.ctx.list_type()))]);

Expand Down Expand Up @@ -443,6 +461,7 @@ pub fn init(context: &PyContext) {
The argument must be an iterable if specified.";

context.set_attr(&list_type, "__add__", context.new_rustfunc(list_add));
context.set_attr(&list_type, "__iadd__", context.new_rustfunc(list_iadd));
context.set_attr(
&list_type,
"__contains__",
Expand Down
Loading
0