Skip to content

Normalize PDeque slice bounds without wrapping - #335

Open
ac1982 wants to merge 1 commit into
tobgu:masterfrom
ac1982:fix/deque-slice-bounds
Open

ac1982 wants to merge 1 commit into
tobgu:masterfrom
ac1982:fix/deque-slice-bounds

Conversation

@ac1982

@ac1982 ac1982 commented Oct 8, 2026 •

Copy link
Copy Markdown

PDeque's unit-step slices currently wrap bounds modulo the deque length instead of clipping them. For example, pdeque([0, 1, 2])[:3] returns an empty deque and [3:] returns the whole deque; explicit bounds on an empty deque raise ZeroDivisionError.

Use slice.indices() to normalize bounds, then trim the left and right sides with the existing persistent operations. This preserves structural sharing and maxlen, handles empty/reversed ranges, and follows Python's integer-index protocol.

Regression tests compare the results with list slicing across empty, singleton and multi-element inputs, bounded/unbounded deques, negative/out-of-range bounds, and custom/invalid index types. Before the change: 40 failures in the deque suite. After:

  • Full suite with the C extension: 701 passed, 1 skipped.
  • Pure-Python suite: 600 passed, 102 skipped.
  • All 80 module doctests passed.
  • Wheel and sdist builds passed on Linux / Python 3.12.

The separate maxlen fixes proposed in #331 concern remove() and reverse() and are unchanged here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant