-
Notifications
You must be signed in to change notification settings - Fork 64
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
fix Python 3 regressions #71
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,7 +180,9 @@ def __ne__(self, other): | |
def __cmp__(self, other): | ||
if not isinstance(other, TVal): | ||
raise TypeError("Cannot compare to non-TVal") | ||
return cmp(self.to_nsec(), other.to_nsec()) | ||
a = self.to_nsec() | ||
b = other.to_nsec() | ||
return (a > b) - (a < b) | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, TVal): | ||
|
@@ -226,6 +228,9 @@ def to_time(self): | |
""" | ||
return self.to_sec() | ||
|
||
def __hash__(self): | ||
return super(Time, self).__hash__() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these needed? Doesn't the base class provide them> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, clearly the base class does provide them, because we're calling What I meant to say, is why do we need to overload them? This works just fine for me in python 2.7 and 3.5 class Base(object):
def __hash__(self):
return 4
class Derived(Base): pass
class DerivedWithOverride(Base):
def __hash__(self):
return super(DerivedWithOverride, self).__hash__()
assert hash(Base()) == hash(DerivedWithOverride()) == hash(Derived()) == 4 So what exactly is adding these lines trying to fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't work for me in the case of the Duration and Time classes. You can try to remove them from this patch and it should fail with:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you give me the full traceback? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I see it now. Something weird seems to be happening, so in the absence of knowing the reason, reintroducing these lines makes sense! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for drawing my attention to this, and sorry for the bad patch before. In case you're interested, the minimal case is http://stackoverflow.com/q/40186509/102441 |
||
|
||
def __repr__(self): | ||
return "genpy.Time[%d]"%self.to_nsec() | ||
|
||
|
@@ -261,7 +266,9 @@ def __cmp__(self, other): | |
""" | ||
if not isinstance(other, Time): | ||
raise TypeError("cannot compare to non-Time") | ||
return cmp(self.to_nsec(), other.to_nsec()) | ||
a = self.to_nsec() | ||
b = other.to_nsec() | ||
return (a > b) - (a < b) | ||
|
||
def __eq__(self, other): | ||
""" | ||
|
@@ -303,6 +310,9 @@ def __setstate__(self, state): | |
""" | ||
self.secs, self.nsecs = state | ||
|
||
def __hash__(self): | ||
return super(Duration, self).__hash__() | ||
|
||
def __repr__(self): | ||
return "genpy.Duration[%d]"%self.to_nsec() | ||
|
||
|
@@ -423,7 +433,9 @@ def __divmod__(self, val): | |
def __cmp__(self, other): | ||
if not isinstance(other, Duration): | ||
raise TypeError("Cannot compare to non-Duration") | ||
return cmp(self.to_nsec(), other.to_nsec()) | ||
a = self.to_nsec() | ||
b = other.to_nsec() | ||
return (a > b) - (a < b) | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, Duration): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arguably the real issue here is implementing
__cmp__
in python 3 at all. Ifcmp
doesn't exist, then__cmp__
will never be called. So I don't understand what this patch is fixing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see what the issue is - the implementation of
__lt__
delegates to__cmp__