Skip to content

deque: refactor tests and enhance docs #14121

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 3 commits into from
Mar 18, 2024
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
31 changes: 26 additions & 5 deletions docs/library/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,47 @@ Classes
appends and pops from either side of the deque. New deques are created
using the following arguments:

- *iterable* must be the empty tuple, and the new deque is created empty.
- *iterable* is an iterable used to populate the deque when it is
created. It can be an empty tuple or list to create a deque that
is initially empty.

- *maxlen* must be specified and the deque will be bounded to this
maximum length. Once the deque is full, any new items added will
discard items from the opposite end.

- The optional *flags* can be 1 to check for overflow when adding items.

As well as supporting `bool` and `len`, deque objects have the following
methods:
Deque objects support `bool`, `len`, iteration and subscript load and store.
They also have the following methods:

.. method:: deque.append(x)

Add *x* to the right side of the deque.
Raises IndexError if overflow checking is enabled and there is no more room left.
Raises ``IndexError`` if overflow checking is enabled and there is
no more room in the queue.

.. method:: deque.appendleft(x)

Add *x* to the left side of the deque.
Raises ``IndexError`` if overflow checking is enabled and there is
no more room in the queue.

.. method:: deque.pop()

Remove and return an item from the right side of the deque.
Raises ``IndexError`` if no items are present.

.. method:: deque.popleft()

Remove and return an item from the left side of the deque.
Raises IndexError if no items are present.
Raises ``IndexError`` if no items are present.

.. method:: deque.extend(iterable)

Extend the deque by appending all the items from *iterable* to
the right of the deque.
Raises ``IndexError`` if overflow checking is enabled and there is
no more room in the deque.

.. function:: namedtuple(name, fields)

Expand Down
68 changes: 0 additions & 68 deletions tests/basics/deque2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Tests for deques with "check overflow" flag and other extensions
# wrt to CPython.
try:
from collections import deque
except ImportError:
Expand Down Expand Up @@ -43,69 +41,3 @@
d[4] = 0
except IndexError:
print("IndexError")

# Removing elements with del is not supported, fall back on mp_obj_subscr() error message
try:
del d[0]
except TypeError:
print("TypeError")


# Only fixed-size deques are supported, so length arg is mandatory
try:
deque(())
except TypeError:
print("TypeError")

d = deque((), 2, True)

try:
d.popleft()
except IndexError:
print("IndexError")

try:
d.pop()
except IndexError:
print("IndexError")

print(d.append(1))
print(d.popleft())

d.append(2)
print(d.popleft())

d.append(3)
d.append(4)
print(d.popleft(), d.popleft())
try:
d.popleft()
except IndexError as e:
print(repr(e))

try:
d.pop()
except IndexError as e:
print(repr(e))

d.append(5)
d.append(6)
print(len(d))
try:
d.append(7)
except IndexError as e:
print(repr(e))

try:
d.appendleft(8)
except IndexError as e:
print(repr(e))

print(len(d))

print(d.popleft(), d.popleft())
print(len(d))
try:
d.popleft()
except IndexError as e:
print(repr(e))
75 changes: 75 additions & 0 deletions tests/basics/deque_micropython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Test MicroPython-specific features of collections.deque.

try:
from collections import deque
except ImportError:
print("SKIP")
raise SystemExit


# Only fixed-size deques are supported, so length arg is mandatory.
try:
deque(())
except TypeError:
print("TypeError")

# Test third argument: flags when True means check for under/overflow
d = deque((), 2, True)

try:
d.popleft()
except IndexError:
print("IndexError")

try:
d.pop()
except IndexError:
print("IndexError")

# Removing elements with del is not supported, fallback to
# mp_obj_subscr() error message.
try:
del d[0]
except TypeError:
print("TypeError")

print(d.append(1))
print(d.popleft())

d.append(2)
print(d.popleft())

d.append(3)
d.append(4)
print(d.popleft(), d.popleft())
try:
d.popleft()
except IndexError as e:
print(repr(e))

try:
d.pop()
except IndexError as e:
print(repr(e))

d.append(5)
d.append(6)
print(len(d))
try:
d.append(7)
except IndexError as e:
print(repr(e))

try:
d.appendleft(8)
except IndexError as e:
print(repr(e))

print(len(d))

print(d.popleft(), d.popleft())
print(len(d))
try:
d.popleft()
except IndexError as e:
print(repr(e))
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
1
2
3
[3, 4, 5]
[5, 6, 7]
0 1 3
5
IndexError
IndexError
TypeError
TypeError
IndexError
IndexError
TypeError
None
1
2
Expand Down
11 changes: 7 additions & 4 deletions tools/mpy-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1730,10 +1730,13 @@ def copy_section(file, offset, offset2):
bytecode.append(0b00000010) # prelude size (n_info=1, n_cell=0)
bytecode.extend(b"\x00") # simple_name: qstr index 0 (will use source filename)
for idx in range(len(compiled_modules)):
bytecode.append(0x32) # MP_BC_MAKE_FUNCTION
bytecode.append(idx) # index raw code
bytecode.extend(b"\x34\x00\x59") # MP_BC_CALL_FUNCTION, 0 args, MP_BC_POP_TOP
bytecode.extend(b"\x51\x63") # MP_BC_LOAD_NONE, MP_BC_RETURN_VALUE
bytecode.append(Opcode.MP_BC_MAKE_FUNCTION)
bytecode.extend(mp_encode_uint(idx)) # index of raw code
bytecode.append(Opcode.MP_BC_CALL_FUNCTION)
bytecode.append(0) # 0 arguments
bytecode.append(Opcode.MP_BC_POP_TOP)
bytecode.append(Opcode.MP_BC_LOAD_CONST_NONE)
bytecode.append(Opcode.MP_BC_RETURN_VALUE)

merged_mpy.extend(mp_encode_uint(len(bytecode) << 3 | 1 << 2)) # length, has_children
merged_mpy.extend(bytecode)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy