Skip to content
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
2 changes: 0 additions & 2 deletions Lib/test/test_deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ def test_extend(self):
d.extend(d)
self.assertEqual(list(d), list('abcdabcd'))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_add(self):
d = deque()
e = deque('abc')
Expand Down
26 changes: 26 additions & 0 deletions vm/src/stdlib/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,32 @@ mod _collections {
!self.borrow_deque().is_empty()
}

#[pymethod(magic)]
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
if let Some(o) = other.payload_if_subclass::<PyDeque>(vm) {
let mut deque = self.borrow_deque().clone();
Comment thread
DimitrisJim marked this conversation as resolved.
Resolved
let elements = o.borrow_deque().clone();
deque.extend(elements);

let skipped = self
.maxlen
.and_then(|maxlen| deque.len().checked_sub(maxlen))
.unwrap_or(0);
deque.drain(0..skipped);

Ok(PyDeque {
deque: PyRwLock::new(deque),
maxlen: self.maxlen,
state: AtomicCell::new(0),
})
} else {
Err(vm.new_type_error(format!(
"can only concatenate deque (not \"{}\") to deque",
other.class().name
)))
}
}

#[pymethod(magic)]
fn iadd(
zelf: PyRef<Self>,
Expand Down