mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
Allow reflected fallback for in-place set operators (#9182)
Assisted-by: Codex:gpt-6
This commit is contained in:
1 parent
e411f91721
commit
e7e3dfd6ce
2 files changed
+28
-4
No files matched your search
@@ -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>()
|
||||
&& PyRef::<PyAnySet>::check(b, vm)
|
||||
{
|
||||
PySet::__isub__(
|
||||
a.to_owned(),
|
||||
PyRef::<PyAnySet>::try_from_object(vm, b.to_owned())?,
|
||||
@@ -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())?,
|
||||
@@ -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())?,
|
||||
@@ -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())?,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import operator
|
||||
|
||||
from testutils import assert_raises
|
||||
|
||||
assert set([1, 2]) == set([1, 2])
|
||||
@@ -706,3 +708,17 @@ for use_operator in (False, True):
|
||||
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}
|
||||
Reference in new issue
Block a user