From 6274c3fe8bb6498a1ecf654360c0ea1495b45a82 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 8 Feb 2019 17:18:14 +0200 Subject: [PATCH 1/8] Add set.__ge__ --- tests/snippets/set.py | 2 ++ vm/src/obj/objset.rs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/snippets/set.py diff --git a/tests/snippets/set.py b/tests/snippets/set.py new file mode 100644 index 000000000..eda3e7702 --- /dev/null +++ b/tests/snippets/set.py @@ -0,0 +1,2 @@ +assert set([1,2,3]) >= set([1,2]) +assert not set([1,3]) >= set([1,2]) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 5ac7507d6..85a01e294 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -126,6 +126,28 @@ pub fn set_contains(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.new_bool(false)) } +fn set_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!( + vm, + args, + required = [ + (zelf, Some(vm.ctx.set_type())), + (other, Some(vm.ctx.set_type())) + ] + ); + for element in get_elements(other).iter() { + match vm.call_method(zelf, "__contains__", vec![element.1.clone()]) { + Ok(value) => { + if !objbool::get_value(&value) { + return Ok(vm.new_bool(false)); + } + } + Err(_) => return Err(vm.new_type_error("".to_string())), + } + } + Ok(vm.new_bool(true)) +} + fn frozenset_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(o, Some(vm.ctx.frozenset_type()))]); @@ -159,6 +181,7 @@ pub fn init(context: &PyContext) { context.set_attr(&set_type, "__len__", context.new_rustfunc(set_len)); context.set_attr(&set_type, "__new__", context.new_rustfunc(set_new)); context.set_attr(&set_type, "__repr__", context.new_rustfunc(set_repr)); + context.set_attr(&set_type, "__ge__", context.new_rustfunc(set_ge)); context.set_attr(&set_type, "__doc__", context.new_str(set_doc.to_string())); context.set_attr(&set_type, "add", context.new_rustfunc(set_add)); From 0c737ae8d2ec5d6b496fd29a4e607fd7f8b15772 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 8 Feb 2019 17:31:45 +0200 Subject: [PATCH 2/8] Add set.__eq__ --- tests/snippets/set.py | 3 +++ vm/src/obj/objset.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/tests/snippets/set.py b/tests/snippets/set.py index eda3e7702..22f1262b5 100644 --- a/tests/snippets/set.py +++ b/tests/snippets/set.py @@ -1,2 +1,5 @@ +assert set([1,2]) == set([1,2]) +assert not set([1,2,3]) == set([1,2]) + assert set([1,2,3]) >= set([1,2]) assert not set([1,3]) >= set([1,2]) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 85a01e294..8c7b66266 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -148,6 +148,33 @@ fn set_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.new_bool(true)) } +fn set_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!( + vm, + args, + required = [ + (zelf, Some(vm.ctx.set_type())), + (other, Some(vm.ctx.set_type())) + ] + ); + let zelf_elements = get_elements(zelf); + let other_elements = get_elements(other); + if zelf_elements.len() != other_elements.len() { + return Ok(vm.new_bool(false)); + } + for element in zelf_elements.iter() { + match vm.call_method(other, "__contains__", vec![element.1.clone()]) { + Ok(value) => { + if !objbool::get_value(&value) { + return Ok(vm.new_bool(false)); + } + } + Err(_) => return Err(vm.new_type_error("".to_string())), + } + } + Ok(vm.new_bool(true)) +} + fn frozenset_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(o, Some(vm.ctx.frozenset_type()))]); @@ -181,6 +208,7 @@ pub fn init(context: &PyContext) { context.set_attr(&set_type, "__len__", context.new_rustfunc(set_len)); context.set_attr(&set_type, "__new__", context.new_rustfunc(set_new)); context.set_attr(&set_type, "__repr__", context.new_rustfunc(set_repr)); + context.set_attr(&set_type, "__eq__", context.new_rustfunc(set_eq)); context.set_attr(&set_type, "__ge__", context.new_rustfunc(set_ge)); context.set_attr(&set_type, "__doc__", context.new_str(set_doc.to_string())); context.set_attr(&set_type, "add", context.new_rustfunc(set_add)); From 358aa6b2c03b3390cd3d53cc7a244ff4028abf96 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 8 Feb 2019 17:50:36 +0200 Subject: [PATCH 3/8] Add set.__gt__ --- tests/snippets/set.py | 5 +++++ vm/src/obj/objset.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/tests/snippets/set.py b/tests/snippets/set.py index 22f1262b5..27225a6e9 100644 --- a/tests/snippets/set.py +++ b/tests/snippets/set.py @@ -2,4 +2,9 @@ assert set([1,2]) == set([1,2]) assert not set([1,2,3]) == set([1,2]) assert set([1,2,3]) >= set([1,2]) +assert set([1,2]) >= set([1,2]) assert not set([1,3]) >= set([1,2]) + +assert set([1,2,3]) > set([1,2]) +assert not set([1,2]) > set([1,2]) +assert not set([1,3]) > set([1,2]) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 8c7b66266..40f131ab7 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -175,6 +175,34 @@ fn set_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.new_bool(true)) } +fn set_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!( + vm, + args, + required = [ + (zelf, Some(vm.ctx.set_type())), + (other, Some(vm.ctx.set_type())) + ] + ); + + let zelf_elements = get_elements(zelf); + let other_elements = get_elements(other); + if zelf_elements.len() <= other_elements.len() { + return Ok(vm.new_bool(false)); + } + for element in other_elements.iter() { + match vm.call_method(zelf, "__contains__", vec![element.1.clone()]) { + Ok(value) => { + if !objbool::get_value(&value) { + return Ok(vm.new_bool(false)); + } + } + Err(_) => return Err(vm.new_type_error("".to_string())), + } + } + Ok(vm.new_bool(true)) +} + fn frozenset_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(o, Some(vm.ctx.frozenset_type()))]); @@ -210,6 +238,7 @@ pub fn init(context: &PyContext) { context.set_attr(&set_type, "__repr__", context.new_rustfunc(set_repr)); context.set_attr(&set_type, "__eq__", context.new_rustfunc(set_eq)); context.set_attr(&set_type, "__ge__", context.new_rustfunc(set_ge)); + context.set_attr(&set_type, "__gt__", context.new_rustfunc(set_gt)); context.set_attr(&set_type, "__doc__", context.new_str(set_doc.to_string())); context.set_attr(&set_type, "add", context.new_rustfunc(set_add)); From 1ee583ef617b0157d99adc96ea060cf147e205b4 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 8 Feb 2019 18:06:22 +0200 Subject: [PATCH 4/8] Use set_compare_inner --- vm/src/obj/objset.rs | 51 ++++++-------------------------------------- 1 file changed, 7 insertions(+), 44 deletions(-) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 40f131ab7..216d90db4 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -127,55 +127,18 @@ pub fn set_contains(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } fn set_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!( - vm, - args, - required = [ - (zelf, Some(vm.ctx.set_type())), - (other, Some(vm.ctx.set_type())) - ] - ); - for element in get_elements(other).iter() { - match vm.call_method(zelf, "__contains__", vec![element.1.clone()]) { - Ok(value) => { - if !objbool::get_value(&value) { - return Ok(vm.new_bool(false)); - } - } - Err(_) => return Err(vm.new_type_error("".to_string())), - } - } - Ok(vm.new_bool(true)) + return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf < other}) } fn set_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!( - vm, - args, - required = [ - (zelf, Some(vm.ctx.set_type())), - (other, Some(vm.ctx.set_type())) - ] - ); - let zelf_elements = get_elements(zelf); - let other_elements = get_elements(other); - if zelf_elements.len() != other_elements.len() { - return Ok(vm.new_bool(false)); - } - for element in zelf_elements.iter() { - match vm.call_method(other, "__contains__", vec![element.1.clone()]) { - Ok(value) => { - if !objbool::get_value(&value) { - return Ok(vm.new_bool(false)); - } - } - Err(_) => return Err(vm.new_type_error("".to_string())), - } - } - Ok(vm.new_bool(true)) + return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf != other}) } fn set_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf <= other}) +} + +fn set_compare_inner(vm: &mut VirtualMachine, args: PyFuncArgs, size_func: &Fn(usize, usize) -> bool) -> PyResult { arg_check!( vm, args, @@ -187,7 +150,7 @@ fn set_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let zelf_elements = get_elements(zelf); let other_elements = get_elements(other); - if zelf_elements.len() <= other_elements.len() { + if size_func(zelf_elements.len(), other_elements.len()) { return Ok(vm.new_bool(false)); } for element in other_elements.iter() { From adee66168b96d1cf6a427219acb676a6b3f0d62c Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 8 Feb 2019 18:25:33 +0200 Subject: [PATCH 5/8] Add set.__le__ --- tests/snippets/set.py | 4 ++++ vm/src/obj/objset.rs | 26 +++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/tests/snippets/set.py b/tests/snippets/set.py index 27225a6e9..b1c938df7 100644 --- a/tests/snippets/set.py +++ b/tests/snippets/set.py @@ -8,3 +8,7 @@ assert not set([1,3]) >= set([1,2]) assert set([1,2,3]) > set([1,2]) assert not set([1,2]) > set([1,2]) assert not set([1,3]) > set([1,2]) + +assert set([1,2]) <= set([1,2,3]) +assert set([1,2]) <= set([1,2]) +assert not set([1,3]) <= set([1,2]) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 216d90db4..774ecb70a 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -126,19 +126,23 @@ pub fn set_contains(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.new_bool(false)) } -fn set_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf < other}) +fn set_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf != other}, false) } -fn set_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf != other}) +fn set_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf < other}, false) } fn set_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf <= other}) + return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf <= other}, false) } -fn set_compare_inner(vm: &mut VirtualMachine, args: PyFuncArgs, size_func: &Fn(usize, usize) -> bool) -> PyResult { +fn set_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf < other}, true) +} + +fn set_compare_inner(vm: &mut VirtualMachine, args: PyFuncArgs, size_func: &Fn(usize, usize) -> bool, swap: bool) -> PyResult { arg_check!( vm, args, @@ -148,13 +152,16 @@ fn set_compare_inner(vm: &mut VirtualMachine, args: PyFuncArgs, size_func: &Fn(u ] ); - let zelf_elements = get_elements(zelf); - let other_elements = get_elements(other); + let get_zelf = |swap: bool| -> &PyObjectRef {if swap {other} else {zelf}}; + let get_other = |swap: bool| -> &PyObjectRef {if swap {zelf} else {other}}; + + let zelf_elements = get_elements(get_zelf(swap)); + let other_elements = get_elements(get_other(swap)); if size_func(zelf_elements.len(), other_elements.len()) { return Ok(vm.new_bool(false)); } for element in other_elements.iter() { - match vm.call_method(zelf, "__contains__", vec![element.1.clone()]) { + match vm.call_method(get_zelf(swap), "__contains__", vec![element.1.clone()]) { Ok(value) => { if !objbool::get_value(&value) { return Ok(vm.new_bool(false)); @@ -202,6 +209,7 @@ pub fn init(context: &PyContext) { context.set_attr(&set_type, "__eq__", context.new_rustfunc(set_eq)); context.set_attr(&set_type, "__ge__", context.new_rustfunc(set_ge)); context.set_attr(&set_type, "__gt__", context.new_rustfunc(set_gt)); + context.set_attr(&set_type, "__le__", context.new_rustfunc(set_le)); context.set_attr(&set_type, "__doc__", context.new_str(set_doc.to_string())); context.set_attr(&set_type, "add", context.new_rustfunc(set_add)); From 2bc946b748ec7395bd645b1eda82909a203e8cc5 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 8 Feb 2019 18:27:37 +0200 Subject: [PATCH 6/8] Add set.__lt__ --- tests/snippets/set.py | 4 ++++ vm/src/obj/objset.rs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/tests/snippets/set.py b/tests/snippets/set.py index b1c938df7..c078908f7 100644 --- a/tests/snippets/set.py +++ b/tests/snippets/set.py @@ -12,3 +12,7 @@ assert not set([1,3]) > set([1,2]) assert set([1,2]) <= set([1,2,3]) assert set([1,2]) <= set([1,2]) assert not set([1,3]) <= set([1,2]) + +assert set([1,2]) < set([1,2,3]) +assert not set([1,2]) < set([1,2]) +assert not set([1,3]) < set([1,2]) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 774ecb70a..e2399776e 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -142,6 +142,10 @@ fn set_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf < other}, true) } +fn set_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf <= other}, true) +} + fn set_compare_inner(vm: &mut VirtualMachine, args: PyFuncArgs, size_func: &Fn(usize, usize) -> bool, swap: bool) -> PyResult { arg_check!( vm, @@ -210,6 +214,7 @@ pub fn init(context: &PyContext) { context.set_attr(&set_type, "__ge__", context.new_rustfunc(set_ge)); context.set_attr(&set_type, "__gt__", context.new_rustfunc(set_gt)); context.set_attr(&set_type, "__le__", context.new_rustfunc(set_le)); + context.set_attr(&set_type, "__lt__", context.new_rustfunc(set_lt)); context.set_attr(&set_type, "__doc__", context.new_str(set_doc.to_string())); context.set_attr(&set_type, "add", context.new_rustfunc(set_add)); From 9ec2eef57993226e206c15d00285aceacee7b197 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 8 Feb 2019 18:33:04 +0200 Subject: [PATCH 7/8] Add set.{issubset,issuperset} --- tests/snippets/set.py | 8 ++++++++ vm/src/obj/objset.rs | 2 ++ 2 files changed, 10 insertions(+) diff --git a/tests/snippets/set.py b/tests/snippets/set.py index c078908f7..8b31c7c23 100644 --- a/tests/snippets/set.py +++ b/tests/snippets/set.py @@ -5,6 +5,10 @@ assert set([1,2,3]) >= set([1,2]) assert set([1,2]) >= set([1,2]) assert not set([1,3]) >= set([1,2]) +assert set([1,2,3]).issuperset(set([1,2])) +assert set([1,2]).issuperset(set([1,2])) +assert not set([1,3]).issuperset(set([1,2])) + assert set([1,2,3]) > set([1,2]) assert not set([1,2]) > set([1,2]) assert not set([1,3]) > set([1,2]) @@ -13,6 +17,10 @@ assert set([1,2]) <= set([1,2,3]) assert set([1,2]) <= set([1,2]) assert not set([1,3]) <= set([1,2]) +assert set([1,2]).issubset(set([1,2,3])) +assert set([1,2]).issubset(set([1,2])) +assert not set([1,3]).issubset(set([1,2])) + assert set([1,2]) < set([1,2,3]) assert not set([1,2]) < set([1,2]) assert not set([1,3]) < set([1,2]) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index e2399776e..dfb46b4c2 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -215,6 +215,8 @@ pub fn init(context: &PyContext) { context.set_attr(&set_type, "__gt__", context.new_rustfunc(set_gt)); context.set_attr(&set_type, "__le__", context.new_rustfunc(set_le)); context.set_attr(&set_type, "__lt__", context.new_rustfunc(set_lt)); + context.set_attr(&set_type, "issubset", context.new_rustfunc(set_le)); + context.set_attr(&set_type, "issuperset", context.new_rustfunc(set_ge)); context.set_attr(&set_type, "__doc__", context.new_str(set_doc.to_string())); context.set_attr(&set_type, "add", context.new_rustfunc(set_add)); From 2df9d799f5ca64a4fd5228a3df938266b3fbbe33 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 9 Feb 2019 10:18:05 +0200 Subject: [PATCH 8/8] Fix formatting errors --- vm/src/obj/objset.rs | 60 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index dfb46b4c2..2648fb4f3 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -127,26 +127,56 @@ pub fn set_contains(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } fn set_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf != other}, false) + return set_compare_inner( + vm, + args, + &|zelf: usize, other: usize| -> bool { zelf != other }, + false, + ); } fn set_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf < other}, false) + return set_compare_inner( + vm, + args, + &|zelf: usize, other: usize| -> bool { zelf < other }, + false, + ); } fn set_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf <= other}, false) + return set_compare_inner( + vm, + args, + &|zelf: usize, other: usize| -> bool { zelf <= other }, + false, + ); } fn set_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf < other}, true) + return set_compare_inner( + vm, + args, + &|zelf: usize, other: usize| -> bool { zelf < other }, + true, + ); } fn set_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner(vm, args, &|zelf: usize, other: usize| -> bool {zelf <= other}, true) + return set_compare_inner( + vm, + args, + &|zelf: usize, other: usize| -> bool { zelf <= other }, + true, + ); } -fn set_compare_inner(vm: &mut VirtualMachine, args: PyFuncArgs, size_func: &Fn(usize, usize) -> bool, swap: bool) -> PyResult { +fn set_compare_inner( + vm: &mut VirtualMachine, + args: PyFuncArgs, + size_func: &Fn(usize, usize) -> bool, + swap: bool, +) -> PyResult { arg_check!( vm, args, @@ -156,13 +186,25 @@ fn set_compare_inner(vm: &mut VirtualMachine, args: PyFuncArgs, size_func: &Fn(u ] ); - let get_zelf = |swap: bool| -> &PyObjectRef {if swap {other} else {zelf}}; - let get_other = |swap: bool| -> &PyObjectRef {if swap {zelf} else {other}}; + let get_zelf = |swap: bool| -> &PyObjectRef { + if swap { + other + } else { + zelf + } + }; + let get_other = |swap: bool| -> &PyObjectRef { + if swap { + zelf + } else { + other + } + }; let zelf_elements = get_elements(get_zelf(swap)); let other_elements = get_elements(get_other(swap)); if size_func(zelf_elements.len(), other_elements.len()) { - return Ok(vm.new_bool(false)); + return Ok(vm.new_bool(false)); } for element in other_elements.iter() { match vm.call_method(get_zelf(swap), "__contains__", vec![element.1.clone()]) {