mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Merge pull request #2949 from stromberger/main
added __add__ method to collections.deque
This commit is contained in:
@@ -204,8 +204,6 @@ class TestBasic(unittest.TestCase):
|
||||
d.extend(d)
|
||||
self.assertEqual(list(d), list('abcdabcd'))
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_add(self):
|
||||
d = deque()
|
||||
e = deque('abc')
|
||||
|
||||
@@ -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();
|
||||
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>,
|
||||
|
||||
Reference in New Issue
Block a user