Skip to content

[Bug]: Negative Indices normalized twice for deque subscript #923

@ztawfick-ux

Description

@ztawfick-ux

Describe the bug

Subscripting a deque with indices < -n does not raise an index error. Instead, the indices wrap around twice, and only start throwing index errors when idx < -2n.

This behavior is only present in the deque container from my testing.

Operating system

macOS

CPU architecture

x86_64

GraalPy version

25.0.2 Native Standalone and JVM standalone

JDK version

No response

Context configuration

No response

Steps to reproduce

Subscript a deque container with an index < -n where n is the size of the deque.

Expected behavior

An index error should be thrown if the idx < -n.

Stack trace

Additional context

example

"""Access a deque with gradually decreasing negative indices and report
whether each access raises an IndexError."""

from collections import deque


def main():
    d = deque([10, 20, 30, 40, 50])
    print(f"deque: {list(d)}")
    print(f"length: {len(d)}\n")

    # Start at -1 and gradually decrease (more negative) past the valid range.
    # Valid negative indices for a length-N deque are -1 .. -N; anything
    # beyond -N should raise IndexError.
    for index in range(-1, -12, -1):
        try:
            value = d[index]
            print(f"d[{index}] = {value}")
        except IndexError as exc:
            print(f"d[{index}] -> IndexError: {exc}")


if __name__ == "__main__":
    main()

This script produces these results:

ztawfick@MacBook-Q2KK9 graalpy % python3 deque_negative_index.py
deque: [10, 20, 30, 40, 50]
length: 5

d[-1] = 50
d[-2] = 40
d[-3] = 30
d[-4] = 20
d[-5] = 10
d[-6] -> IndexError: deque index out of range
d[-7] -> IndexError: deque index out of range
d[-8] -> IndexError: deque index out of range
d[-9] -> IndexError: deque index out of range
d[-10] -> IndexError: deque index out of range
d[-11] -> IndexError: deque index out of range
ztawfick@MacBook-Q2KK9 graalpy % graalpy deque_negative_index.py
deque: [10, 20, 30, 40, 50]
length: 5

d[-1] = 50
d[-2] = 40
d[-3] = 30
d[-4] = 20
d[-5] = 10
d[-6] = 50
d[-7] = 40
d[-8] = 30
d[-9] = 20
d[-10] = 10
d[-11] -> IndexError: deque index out of range

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions