Skip to content
Open
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
16 changes: 12 additions & 4 deletions crates/vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,9 @@ impl AsNumber for PySet {
}
}),
inplace_subtract: Some(|a, b, vm| {
if let Some(a) = a.downcast_ref::<PySet>() {
if let Some(a) = a.downcast_ref::<PySet>()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is there any case PySet but not PyAnySet?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the left operand needs to remain a PySet because these slots call mutating PySet::__i*__ methods. PyAnySet would also accept frozenset, which cannot be modified in place. Augmented assignment on a frozenset uses the regular binary operation and reassigns the result instead.

the right operand is only read, so accepting PyAnySet there is appropriate.

&& PyRef::<PyAnySet>::check(b, vm)
{
PySet::__isub__(
a.to_owned(),
PyRef::<PyAnySet>::try_from_object(vm, b.to_owned())?,
Expand All @@ -1163,7 +1165,9 @@ impl AsNumber for PySet {
}
}),
inplace_and: Some(|a, b, vm| {
if let Some(a) = a.downcast_ref::<PySet>() {
if let Some(a) = a.downcast_ref::<PySet>()
&& PyRef::<PyAnySet>::check(b, vm)
{
PySet::__iand__(
a.to_owned(),
PyRef::<PyAnySet>::try_from_object(vm, b.to_owned())?,
Expand All @@ -1175,7 +1179,9 @@ impl AsNumber for PySet {
}
}),
inplace_xor: Some(|a, b, vm| {
if let Some(a) = a.downcast_ref::<PySet>() {
if let Some(a) = a.downcast_ref::<PySet>()
&& PyRef::<PyAnySet>::check(b, vm)
{
PySet::__ixor__(
a.to_owned(),
PyRef::<PyAnySet>::try_from_object(vm, b.to_owned())?,
Expand All @@ -1187,7 +1193,9 @@ impl AsNumber for PySet {
}
}),
inplace_or: Some(|a, b, vm| {
if let Some(a) = a.downcast_ref::<PySet>() {
if let Some(a) = a.downcast_ref::<PySet>()
&& PyRef::<PyAnySet>::check(b, vm)
{
PySet::__ior__(
a.to_owned(),
PyRef::<PyAnySet>::try_from_object(vm, b.to_owned())?,
Expand Down
16 changes: 16 additions & 0 deletions extra_tests/snippets/builtin_set.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import operator

from testutils import assert_raises

assert set([1, 2]) == set([1, 2])
Expand Down Expand Up @@ -706,3 +708,17 @@ def __del__(self):
intersection_target.intersection_update({1})
assert not intersection_target
assert intersection_events == [True, True]


# Unsupported right operands must reach their reflected set operation.
keys = {2: None, 3: None}.keys()
for method, operation, expected in (
(set.__ior__, operator.ior, {1, 2, 3}),
(set.__iand__, operator.iand, {2}),
(set.__isub__, operator.isub, {1}),
(set.__ixor__, operator.ixor, {1, 3}),
):
source = {1, 2}
assert method(source, keys) is NotImplemented
assert operation(source, keys) == expected
assert source == {1, 2}
Comment thread
1ndahous3 marked this conversation as resolved.
Loading