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

Implement comparison operators for range objects #57410

Closed
smarnach mannequin opened this issue Oct 17, 2011 · 14 comments
Closed

Implement comparison operators for range objects #57410

smarnach mannequin opened this issue Oct 17, 2011 · 14 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@smarnach
Copy link
Mannequin

smarnach mannequin commented Oct 17, 2011

BPO 13201
Nosy @mdickinson, @vstinner, @ezio-melotti, @smarnach
Files
  • range-compare.patch
  • range-compare-v2.patch
  • range-compare-v3.patch
  • range-compare-v4.patch
  • 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/mdickinson'
    closed_at = <Date 2011-10-23.18:54:23.971>
    created_at = <Date 2011-10-17.14:52:53.746>
    labels = ['interpreter-core', 'type-feature']
    title = 'Implement comparison operators for range objects'
    updated_at = <Date 2011-10-23.18:54:23.970>
    user = 'https://github.com/smarnach'

    bugs.python.org fields:

    activity = <Date 2011-10-23.18:54:23.970>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2011-10-23.18:54:23.971>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2011-10-17.14:52:53.746>
    creator = 'smarnach'
    dependencies = []
    files = ['23426', '23429', '23441', '23494']
    hgrepos = []
    issue_num = 13201
    keywords = ['patch', 'needs review']
    message_count = 14.0
    messages = ['145705', '145714', '145727', '145746', '145796', '146015', '146016', '146172', '146173', '146174', '146198', '146199', '146244', '146245']
    nosy_count = 5.0
    nosy_names = ['mark.dickinson', 'vstinner', 'ezio.melotti', 'python-dev', 'smarnach']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue13201'
    versions = ['Python 3.3']

    @smarnach
    Copy link
    Mannequin Author

    smarnach mannequin commented Oct 17, 2011

    It seems some sort of consensus on how to compare range objects has emerged from the python-ideas discussion on comparison of range objects 1.

    The attached patch defines '==' and '!=' for range object equality based on the sequence of values they represent. The other comparison operators are left NotImplemented.

    @mdickinson mdickinson added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Oct 17, 2011
    @mdickinson
    Copy link
    Member

    Nice patch! I put some comments on Rietveld.

    @mdickinson mdickinson self-assigned this Oct 17, 2011
    @smarnach
    Copy link
    Mannequin Author

    smarnach mannequin commented Oct 17, 2011

    Mark, thanks for your comments. Here's a new version of the patch, I answer on Rietveld.

    @mdickinson
    Copy link
    Member

    The new patch looks fine; I'd still like to have the more explicit reference counting in range_hash (see replies on Rietveld).

    A few more things:

    • The patch needs a Misc/NEWS entry before committing; it probably deserves a line in Doc/whatsnew/3.3.rst, too.

    • The doc update should have a ".. versionchanged:: 3.3" note.

    • Maybe the range_compare function could be renamed to something that makes it clearer it's just doing an equality comparison rather than a generalized comparison. 'range_equality_check'? Or just 'range_equal' or 'range_equality'?

    @smarnach
    Copy link
    Mannequin Author

    smarnach mannequin commented Oct 18, 2011

    Mark, thanks again for your comments. (I never looked at the Python source code before, so tey are highly appreciated.) I uploaded a new version of the patch hopefully.

    @mdickinson
    Copy link
    Member

    I get a test failure in test_hash (which is checking exactly that the hash(range) uses the default object hash, so that test is clearly out of date now). Apart from that, the latest patch looks good to me.

    I'm going to give this a couple of days in case anyone else has any comments, then I'll aim to commit it (with the extra test_hash fix) this weekend.

    Thanks for all the work on this!

    @vstinner
    Copy link
    Member

    + one = PyLong_FromLong(1);
    + if (!one)
    + return -1;
    + cmp_result = PyObject_RichCompareBool(r0->length, one, Py_EQ);
    + Py_DECREF(one);

    If would be nice to have a PyLong_CompareLong() function.

    @mdickinson
    Copy link
    Member

    I've taken the liberty of updating the patch, with a few minor changes:

    range_equality -> range_equals (like range_contains)
    move identity check into range_equals
    move comments before the code they describe (PEP-7)
    add whatsnew entry
    remove check that range.__hash__ matches object.__hash__ in test_hash
    change assertEqual into assertIs where appropriate (as suggested by Ezio)
    additional comments and tests in Lib/test/test_range (ditto)

    Sven, Ezio: okay to apply this?

    @mdickinson
    Copy link
    Member

    Hmm. Why does my patch not get a 'review' button?

    @mdickinson
    Copy link
    Member

    Ah, there it is. Never mind. :-)

    @smarnach
    Copy link
    Mannequin Author

    smarnach mannequin commented Oct 22, 2011

    Thanks for the updates, Mark. I was just about to look into this again. The changes are fine with me.

    @smarnach
    Copy link
    Mannequin Author

    smarnach mannequin commented Oct 22, 2011

    Victor Stinner wrote:

    If would be nice to have a PyLong_CompareLong() function.

    In most cases, global variables Py_Zero and Py_One would be enough to simplify this kind of code.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 23, 2011

    New changeset 479a7dd1ea6a by Mark Dickinson in branch 'default':
    Issue bpo-13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
    http://hg.python.org/cpython/rev/479a7dd1ea6a

    @mdickinson
    Copy link
    Member

    In most cases, global variables Py_Zero and Py_One would be enough to
    simplify this kind of code.

    Agreed.

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants