Skip to content
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

slice().indices() returns incorrect values #86633

Closed
misianne mannequin opened this issue Nov 25, 2020 · 2 comments
Closed

slice().indices() returns incorrect values #86633

misianne mannequin opened this issue Nov 25, 2020 · 2 comments
Labels
3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@misianne
Copy link
Mannequin

misianne mannequin commented Nov 25, 2020

BPO 42467
Nosy @mdickinson
Superseder
  • bpo-11842: slice.indices with negative step and default stop
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-11-25.21:10:34.758>
    created_at = <Date 2020-11-25.20:54:21.047>
    labels = ['interpreter-core', '3.8']
    title = 'slice().indices() returns incorrect values'
    updated_at = <Date 2020-11-25.21:10:34.757>
    user = 'https://bugs.python.org/misianne'

    bugs.python.org fields:

    activity = <Date 2020-11-25.21:10:34.757>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-11-25.21:10:34.758>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2020-11-25.20:54:21.047>
    creator = 'misianne'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42467
    keywords = []
    message_count = 2.0
    messages = ['381862', '381864']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'misianne']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '11842'
    type = None
    url = 'https://bugs.python.org/issue42467'
    versions = ['Python 3.8']

    @misianne
    Copy link
    Mannequin Author

    misianne mannequin commented Nov 25, 2020

    I have a problem with slice().indices():

    x=[0,1,2,3,4,5,6,7,8,9]*10
    x[-91:-101:-3] # == [9, 6, 3, 0] OK!
    x[slice(*slice(-91,-101,-3).indices(len(x)))] # == [] WRONG!

    This is correct:

    x[-91:-100:-3] # == [9, 6, 3] OK!
    x[slice(*slice(-91,-100,-3).indices(len(x)))] # == [9, 6, 3] OK!

    This is not:

    x[-91:-101:-3] # == [9, 6, 3, 0] OK!
    x[slice(*slice(-91,-101,-3).indices(len(x)))] # == [] WRONG!

    The problem is that
    slice(-91,-101,-3).indices(len(x))
    returns:
    (9,-1,-3)
    which result in an empty list, while:
    (9,None,-3)
    gives the correct result.

    @misianne misianne mannequin added 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Nov 25, 2020
    @mdickinson
    Copy link
    Member

    This is essentially a duplicate of bpo-11842. In short, slice.indices is working as intended here. It's best to think of slice.indices as start, stop and step arguments that could be provided to the range built-in function to get the set of relevant indices.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant