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

inconsistent behavior of __getslice__ #36642

Closed
douady mannequin opened this issue May 24, 2002 · 6 comments
Closed

inconsistent behavior of __getslice__ #36642

douady mannequin opened this issue May 24, 2002 · 6 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@douady
Copy link
Mannequin

douady mannequin commented May 24, 2002

BPO 560064
Nosy @gvanrossum, @brettcannon, @rhettinger
Files
  • str.diff: Patch to stringobject.c
  • 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 = 'https://github.com/brettcannon'
    closed_at = <Date 2003-05-20.02:43:14.000>
    created_at = <Date 2002-05-24.11:59:51.000>
    labels = ['interpreter-core']
    title = 'inconsistent behavior of __getslice__'
    updated_at = <Date 2003-05-20.02:43:14.000>
    user = 'https://bugs.python.org/douady'

    bugs.python.org fields:

    activity = <Date 2003-05-20.02:43:14.000>
    actor = 'brett.cannon'
    assignee = 'brett.cannon'
    closed = True
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2002-05-24.11:59:51.000>
    creator = 'douady'
    dependencies = []
    files = ['492']
    hgrepos = []
    issue_num = 560064
    keywords = []
    message_count = 6.0
    messages = ['10917', '10918', '10919', '10920', '10921', '10922']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'brett.cannon', 'rhettinger', 'douady']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue560064'
    versions = ['Python 2.3']

    @douady
    Copy link
    Mannequin Author

    douady mannequin commented May 24, 2002

    The following trace shows what I mean:
    >>> help(str.__getslice__)
    Help on wrapper_descriptor:
    __getslice__(...)
        x.__getslice__(i, j) <==> x[i:j]
    >>> 'ab'[-1]
    'b'
    >>> 'ab'.__getitem__(-1)
    'b'
    >>> 'ab'[-2:-1]
    'a'
    >>> 'ab'.__getslice__(-2,-1)
    ''

    that is, __getslice__ does not add the length of the
    sequence to its arguments when they are negative as
    indexing with [:] does.
    This contradicts the docstring which says __getslice__
    is equivalent to indexing.
    The same behavior applies to lists.

    Note that __getitem__ does it right.

    This bug may be linked to bug 473985 ('str, __getitem__
    and slices') but should be much easier to fix.

    @douady douady mannequin closed this as completed May 24, 2002
    @douady douady mannequin assigned brettcannon May 24, 2002
    @douady douady mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label May 24, 2002
    @rhettinger
    Copy link
    Contributor

    Logged In: YES
    user_id=80475

    Tim, I attached a tiny patch which fixes the behavior for
    strings. I've also tried out the same change in listobject.c
    and unicodeobject.c. Is this the right this to do or are
    there more subtleties to this than meet the eye?

    @gvanrossum
    Copy link
    Member

    Logged In: YES
    user_id=6380

    This should NOT be fixed this way, Raymond!

    The docs are (subtly) wrong. For historical reasons,
    __getslice__ expects the Python VM to do the adjustment of
    negative indices. If you did another adjustment inside getslice,
    certain indices would start behaving differently (e.g. s[:-5]
    where len(s)==3).

    Maybe the docstring should be fixed.

    @brettcannon
    Copy link
    Member

    Logged In: YES
    user_id=357491

    If the docstring fix is still the best way to fix this I am willing to make the
    change.

    @gvanrossum
    Copy link
    Member

    Logged In: YES
    user_id=6380

    Yes, please fix the docstring.

    @brettcannon
    Copy link
    Member

    Logged In: YES
    user_id=357491

    Fixed as revision 2.232 on HEAD (after getting it right; bah) and as
    2.126.4.35 on release22-maint.

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

    No branches or pull requests

    3 participants