Skip to content

Add set.{__iter__,__ior__,__iand__,__isub__,__ixor__} #524

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 2 commits into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
47 changes: 47 additions & 0 deletions tests/snippets/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,64 @@ def __hash__(self):
assert a == set([1,2,3,4,5])
assert_raises(TypeError, lambda: a.update(1))

a = set([1,2,3])
b = set()
for e in a:
assert e == 1 or e == 2 or e == 3
b.add(e)
assert a == b

a = set([1,2,3])
a |= set([3,4,5])
assert a == set([1,2,3,4,5])
try:
a |= 1
except TypeError:
pass
else:
assert False, "TypeError not raised"

a = set([1,2,3])
a.intersection_update([2,3,4,5])
assert a == set([2,3])
assert_raises(TypeError, lambda: a.intersection_update(1))

a = set([1,2,3])
a &= set([2,3,4,5])
assert a == set([2,3])
try:
a &= 1
except TypeError:
pass
else:
assert False, "TypeError not raised"
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use the assert_raises function for these checks? Just makes these a bit less verbose

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You cant do assignment inside lambda function so the best I can think about is something like this:

a = [set([1,2,3])]
def f():
    a[0] |= set([4,5])
assert_raises(TypeError, f)

I believe the current option is more clear. Any other suggestion?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I completely missed that it's an assignment! Hopefully in the future we'll have something like assertRaises from unittest:

with self.assertRaises(TypeError):
    a[0] |= set([4,5])

Copy link
Contributor

Choose a reason for hiding this comment

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

The with statement should work, so we could do the same now by implementing a contextmanager.


a = set([1,2,3])
a.difference_update([3,4,5])
assert a == set([1,2])
assert_raises(TypeError, lambda: a.difference_update(1))

a = set([1,2,3])
a -= set([3,4,5])
assert a == set([1,2])
try:
a -= 1
except TypeError:
pass
else:
assert False, "TypeError not raised"

a = set([1,2,3])
a.symmetric_difference_update([3,4,5])
assert a == set([1,2,4,5])
assert_raises(TypeError, lambda: a.difference_update(1))

a = set([1,2,3])
a ^= set([3,4,5])
assert a == set([1,2,4,5])
try:
a ^= 1
except TypeError:
pass
else:
assert False, "TypeError not raised"
55 changes: 48 additions & 7 deletions vm/src/obj/objset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ fn set_pop(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

fn set_update(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
set_ior(vm, args)?;
Ok(vm.get_none())
}

fn set_ior(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
Expand All @@ -447,17 +452,27 @@ fn set_update(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
while let Ok(v) = vm.call_method(&iterator, "__next__", vec![]) {
insert_into_set(vm, elements, &v)?;
}
Ok(vm.get_none())
}
_ => Err(vm.new_type_error("set.update is called with no other".to_string())),
_ => return Err(vm.new_type_error("set.update is called with no other".to_string())),
}
Ok(zelf.clone())
}

fn set_intersection_update(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
set_combine_update_inner(vm, args, SetCombineOperation::Intersection)?;
Ok(vm.get_none())
}

fn set_iand(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
set_combine_update_inner(vm, args, SetCombineOperation::Intersection)
}

fn set_difference_update(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
set_combine_update_inner(vm, args, SetCombineOperation::Difference)?;
Ok(vm.get_none())
}

fn set_isub(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
set_combine_update_inner(vm, args, SetCombineOperation::Difference)
}

Expand Down Expand Up @@ -486,13 +501,18 @@ fn set_combine_update_inner(
elements.remove(&element.0.clone());
}
}
Ok(vm.get_none())
}
_ => Err(vm.new_type_error("".to_string())),
_ => return Err(vm.new_type_error("".to_string())),
}
Ok(zelf.clone())
}

fn set_symmetric_difference_update(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
set_ixor(vm, args)?;
Ok(vm.get_none())
}

fn set_ixor(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
Expand All @@ -514,11 +534,27 @@ fn set_symmetric_difference_update(vm: &mut VirtualMachine, args: PyFuncArgs) ->
elements.remove(&element.0.clone());
}
}

Ok(vm.get_none())
}
_ => Err(vm.new_type_error("".to_string())),
_ => return Err(vm.new_type_error("".to_string())),
}

Ok(zelf.clone())
}

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

let items = get_elements(zelf).values().map(|x| x.clone()).collect();
let set_list = vm.ctx.new_list(items);
let iter_obj = PyObject::new(
PyObjectPayload::Iterator {
position: 0,
iterated_obj: set_list,
},
vm.ctx.iter_type(),
);

Ok(iter_obj)
}

fn frozenset_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Expand Down Expand Up @@ -593,21 +629,26 @@ pub fn init(context: &PyContext) {
context.set_attr(&set_type, "copy", context.new_rustfunc(set_copy));
context.set_attr(&set_type, "pop", context.new_rustfunc(set_pop));
context.set_attr(&set_type, "update", context.new_rustfunc(set_update));
context.set_attr(&set_type, "__ior__", context.new_rustfunc(set_ior));
context.set_attr(
&set_type,
"intersection_update",
context.new_rustfunc(set_intersection_update),
);
context.set_attr(&set_type, "__iand__", context.new_rustfunc(set_iand));
context.set_attr(
&set_type,
"difference_update",
context.new_rustfunc(set_difference_update),
);
context.set_attr(&set_type, "__isub__", context.new_rustfunc(set_isub));
context.set_attr(
&set_type,
"symmetric_difference_update",
context.new_rustfunc(set_symmetric_difference_update),
);
context.set_attr(&set_type, "__ixor__", context.new_rustfunc(set_ixor));
context.set_attr(&set_type, "__iter__", context.new_rustfunc(set_iter));

let frozenset_type = &context.frozenset_type;

Expand Down