Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Add support for the cmp builtin (#54) (#63) #80

Merged
merged 1 commit into from
Jan 12, 2017
Merged

Add support for the cmp builtin (#54) (#63) #80

merged 1 commit into from
Jan 12, 2017

Conversation

meadori
Copy link
Contributor

@meadori meadori commented Jan 9, 2017

Implement the cmp as defined here:

Implementing this builtin required implementing a fair
amount of supporting logic to do 3-way comparisons. The
3-way comparison implementation in CPython is somewhat
complex, thus there may be some cases that still need
support. This is a good start, though.

Copy link
Contributor

@trotterdylan trotterdylan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the existing LE, LT, Eq, etc. functions need to be rewritten to make use of this? I'm fine doing that in a subsequent PR, but I just want to make sure I understand exactly how this all fits together.

//
// It closely resembles the behavior of CPython's do_cmp in object.c.
func compare(f *Frame, v, w *Object) (*Object, *BaseException) {
cmp := v.typ.slots.Cmp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just move this logic directly into Compare()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

@@ -101,3 +101,133 @@ def __call__(self, *args, **kwargs):
assert callable(bar)
assert callable(bar())

# cmp(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough tests!

Implement the `cmp` as defined here:

* https://docs.python.org/2/library/functions.html#cmp

Implementing this builtin required implementing a fair
amount of supporting logic to do 3-way comparisons. The
3-way comparison implementation in CPython is somewhat
complex, thus there may be some cases that still need
support. This is a good start, though.
@meadori
Copy link
Contributor Author

meadori commented Jan 11, 2017

The rich compare implementation will need to be tweak to fall-back on 3-way compare if the appropriate rich comparison operation is not available. I will do that in a separate PR.

@meadori
Copy link
Contributor Author

meadori commented Jan 12, 2017

I made the one change you requested and will commit to adding the fallback to 3-way compare for rich comparisons in a follow-on PR. Everything else look okay?

// halfCompare tries a comparison with the __cmp__ slot, ensures the result
// is an integer, and returns it. It closely resembles the behavior of CPython's
// half_compare in typeobject.c.
func halfCompare(f *Frame, v, w *Object) (*Object, *BaseException) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a subsequent PR, would you mind changing the return value to (*Int, *BaseException)? You pass the result to intNeg() later on in the code and it would be easier to understand if it was an *Int.

@trotterdylan
Copy link
Contributor

Thanks again for all your work on this!

@trotterdylan trotterdylan merged commit 24d08dc into google:master Jan 12, 2017
@meadori meadori deleted the builtin-cmp branch January 12, 2017 16:16
@meadori meadori restored the builtin-cmp branch January 12, 2017 16:16
@meadori meadori deleted the builtin-cmp branch January 12, 2017 16:16
pombredanne pushed a commit to pombredanne/grumpy that referenced this pull request Mar 12, 2019
Fixes `from time import *; print sleep`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants