Skip to content

&= and -= operator between Set types do not work correctly. #3992

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

Open
tgsong827 opened this issue Aug 2, 2022 · 2 comments
Open

&= and -= operator between Set types do not work correctly. #3992

tgsong827 opened this issue Aug 2, 2022 · 2 comments
Labels
z-ca-2022 Tag to track contrubution-academy 2022

Comments

@tgsong827
Copy link
Contributor

Feature

I'm trying to fix @unittest.expectedFailure test_inplace_on_self in file test_set.py.

def test_inplace_on_self(self):
    t = self.s.copy()
    t |= t
    self.assertEqual(t, self.s)
    t &= t
    self.assertEqual(t, self.s).      -> FAIL
    t -= t
    self.assertEqual(t, self.thetype()).     -> FAIL
    t = self.s.copy()
    t ^= t
    self.assertEqual(t, self.thetype())

I found the two operator fail.
&= and -= is not work correctly.

[ &= ]

  • RustPython
>>>>> s = set({'a', 'b', 'c'})
>>>>> s &= s
>>>>> s
set()
  • CPython
>>> s = set({'a', 'b', 'c'})
>>> s &= s
>>> s
{'b', 'c', 'a'}

[ -= ]

  • RustPython
>>>>> s = set({'a', 'b', 'c'})
>>>>> s -= s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: set changed size during iteration

Maybe the points to start

  • &=
#[pymethod(magic)]
fn iand(zelf: PyRef<Self>, set: AnySet, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
    zelf.inner
        .intersection_update(std::iter::once(set.into_iterable(vm)?), vm)?;
    Ok(zelf)
}
  • -=
#[pymethod(magic)]
fn isub(zelf: PyRef<Self>, set: AnySet, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
    zelf.inner
        .difference_update(set.into_iterable_iter(vm)?, vm)?;
    Ok(zelf)
}

Python Documentation

CPython Method

@tgsong827
Copy link
Contributor Author

This may be related with #3881 #3912.
Can I try it after #3912 is merged?

@arihant2math
Copy link
Collaborator

&= now works, -= still fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
z-ca-2022 Tag to track contrubution-academy 2022
Projects
None yet
Development

No branches or pull requests

3 participants