Skip to content

Adds __rmul__ to collections.deque #2844

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 4 commits into from
Aug 11, 2021
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
8 changes: 2 additions & 6 deletions Lib/test/test_deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,6 @@ def test_insert_bug_26194(self):
else:
self.assertEqual(d[i-1], 'Z')

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_imul(self):
for n in (-10, -1, 0, 1, 2, 10, 1000):
d = deque()
Expand Down Expand Up @@ -388,8 +386,6 @@ def test_imul(self):
self.assertEqual(d, deque(('abcdef' * n)[-500:]))
self.assertEqual(d.maxlen, 500)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_mul(self):
d = deque('abc')
self.assertEqual(d * -5, deque())
Expand Down Expand Up @@ -1003,11 +999,11 @@ def test_subscript(self):
def test_free_after_iterating(self):
# For now, bypass tests that require slicing
self.skipTest("Exhausted deque iterator doesn't free a deque")

@unittest.skip("TODO: RUSTPYTHON TypeError: unexpected payload for __eq__")
def test_pickle(self):
pass

def test_iadd(self):
pass

Expand Down
12 changes: 7 additions & 5 deletions vm/src/stdlib/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,16 @@ mod _collections {
}

#[pymethod(magic)]
#[pymethod(name = "__rmul__")]
fn mul(&self, n: isize) -> Self {
let deque: SimpleSeqDeque = self.borrow_deque().into();
let mul = sequence::seq_mul(&deque, n);
let skipped = if let Some(maxlen) = self.maxlen.load() {
mul.len() - maxlen
} else {
0
};
let skipped = self
.maxlen
.load()
.and_then(|maxlen| mul.len().checked_sub(maxlen))
.unwrap_or(0);

let deque = mul.skip(skipped).cloned().collect();
PyDeque {
deque: PyRwLock::new(deque),
Expand Down