FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Drop what a deque with maxlen of zero is handed by luantaraschi · Pull Request #8567 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rs  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 10 additions & 4 deletions crates/vm/src/stdlib/_collections.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ mod _collections {
fn borrow_deque_mut(&self) -> PyRwLockWriteGuard<'_, VecDeque<PyObjectRef>> {
self.deque.write()
}

fn is_over_maxlen(&self, deque: &VecDeque<PyObjectRef>) -> bool {
self.maxlen.is_some_and(|maxlen| deque.len() > maxlen)
}
}

#[pyclass(
Expand All @@ -96,20 +100,22 @@ mod _collections {
fn append(&self, obj: PyObjectRef) {
self.state.fetch_add(1);
let mut deque = self.borrow_deque_mut();
if self.maxlen == Some(deque.len()) {
deque.push_back(obj);
// Trim after pushing, so that a `maxlen` of zero drops what just
// arrived instead of popping from an empty deque and keeping it.
if self.is_over_maxlen(&deque) {
deque.pop_front();
}
deque.push_back(obj);
}

#[pymethod]
fn appendleft(&self, obj: PyObjectRef) {
self.state.fetch_add(1);
let mut deque = self.borrow_deque_mut();
if self.maxlen == Some(deque.len()) {
deque.push_front(obj);
if self.is_over_maxlen(&deque) {
deque.pop_back();
}
deque.push_front(obj);
}

#[pymethod]
Expand Down
43 changes: 43 additions & 0 deletions extra_tests/snippets/stdlib_collections_deque.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,46 @@ class D(deque):
# until the allocator gives up, so it is left out of this check.
with assert_raises(MemoryError):
deque([0]) * sys.maxsize


# maxlen=0 keeps nothing, whichever end the item arrives at.
d = deque(maxlen=0)
d.append(1)
d.appendleft(2)
assert list(d) == []
assert len(d) == 0
assert d.maxlen == 0

d = deque(maxlen=0)
d.extend("abc")
d.extendleft("abc")
d += "abc"
assert list(d) == []

assert list(deque("abc", maxlen=0)) == []
assert list(deque("ab", maxlen=0) * 3) == []
assert list(deque("ab", maxlen=0) + deque("cd")) == []

d = deque("abc", maxlen=0)
d.rotate(1)
assert list(d) == []

assert_raises(IndexError, deque(maxlen=0).insert, 0, 1)


# A bounded deque still drops from the far end, and only once it is full.
d = deque(maxlen=1)
d.append(1)
assert list(d) == [1]
d.append(2)
assert list(d) == [2]
d.appendleft(3)
assert list(d) == [3]

d = deque("ab", maxlen=3)
d.append("c")
assert list(d) == ["a", "b", "c"]
d.append("d")
assert list(d) == ["b", "c", "d"]
d.appendleft("z")
assert list(d) == ["z", "b", "c"]
Loading

Back | FazBrowse Home | New Git URL