Kaydet (Commit) ffcd8490 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Reduce the overhead in functools.total_ordering by localizing NotImplemented.

(Sugguested by Serhiy Storchaka)
üst 276e9c84
...@@ -94,80 +94,80 @@ def wraps(wrapped, ...@@ -94,80 +94,80 @@ def wraps(wrapped,
# infinite recursion that could occur when the operator dispatch logic # infinite recursion that could occur when the operator dispatch logic
# detects a NotImplemented result and then calls a reflected method. # detects a NotImplemented result and then calls a reflected method.
def _gt_from_lt(self, other): def _gt_from_lt(self, other, NotImplemented=NotImplemented):
'Return a > b. Computed by @total_ordering from (not a < b) and (a != b).' 'Return a > b. Computed by @total_ordering from (not a < b) and (a != b).'
op_result = self.__lt__(other) op_result = self.__lt__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result and self != other return not op_result and self != other
def _le_from_lt(self, other): def _le_from_lt(self, other, NotImplemented=NotImplemented):
'Return a <= b. Computed by @total_ordering from (a < b) or (a == b).' 'Return a <= b. Computed by @total_ordering from (a < b) or (a == b).'
op_result = self.__lt__(other) op_result = self.__lt__(other)
return op_result or self == other return op_result or self == other
def _ge_from_lt(self, other): def _ge_from_lt(self, other, NotImplemented=NotImplemented):
'Return a >= b. Computed by @total_ordering from (not a < b).' 'Return a >= b. Computed by @total_ordering from (not a < b).'
op_result = self.__lt__(other) op_result = self.__lt__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result return not op_result
def _ge_from_le(self, other): def _ge_from_le(self, other, NotImplemented=NotImplemented):
'Return a >= b. Computed by @total_ordering from (not a <= b) or (a == b).' 'Return a >= b. Computed by @total_ordering from (not a <= b) or (a == b).'
op_result = self.__le__(other) op_result = self.__le__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result or self == other return not op_result or self == other
def _lt_from_le(self, other): def _lt_from_le(self, other, NotImplemented=NotImplemented):
'Return a < b. Computed by @total_ordering from (a <= b) and (a != b).' 'Return a < b. Computed by @total_ordering from (a <= b) and (a != b).'
op_result = self.__le__(other) op_result = self.__le__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return op_result and self != other return op_result and self != other
def _gt_from_le(self, other): def _gt_from_le(self, other, NotImplemented=NotImplemented):
'Return a > b. Computed by @total_ordering from (not a <= b).' 'Return a > b. Computed by @total_ordering from (not a <= b).'
op_result = self.__le__(other) op_result = self.__le__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result return not op_result
def _lt_from_gt(self, other): def _lt_from_gt(self, other, NotImplemented=NotImplemented):
'Return a < b. Computed by @total_ordering from (not a > b) and (a != b).' 'Return a < b. Computed by @total_ordering from (not a > b) and (a != b).'
op_result = self.__gt__(other) op_result = self.__gt__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result and self != other return not op_result and self != other
def _ge_from_gt(self, other): def _ge_from_gt(self, other, NotImplemented=NotImplemented):
'Return a >= b. Computed by @total_ordering from (a > b) or (a == b).' 'Return a >= b. Computed by @total_ordering from (a > b) or (a == b).'
op_result = self.__gt__(other) op_result = self.__gt__(other)
return op_result or self == other return op_result or self == other
def _le_from_gt(self, other): def _le_from_gt(self, other, NotImplemented=NotImplemented):
'Return a <= b. Computed by @total_ordering from (not a > b).' 'Return a <= b. Computed by @total_ordering from (not a > b).'
op_result = self.__gt__(other) op_result = self.__gt__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result return not op_result
def _le_from_ge(self, other): def _le_from_ge(self, other, NotImplemented=NotImplemented):
'Return a <= b. Computed by @total_ordering from (not a >= b) or (a == b).' 'Return a <= b. Computed by @total_ordering from (not a >= b) or (a == b).'
op_result = self.__ge__(other) op_result = self.__ge__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result or self == other return not op_result or self == other
def _gt_from_ge(self, other): def _gt_from_ge(self, other, NotImplemented=NotImplemented):
'Return a > b. Computed by @total_ordering from (a >= b) and (a != b).' 'Return a > b. Computed by @total_ordering from (a >= b) and (a != b).'
op_result = self.__ge__(other) op_result = self.__ge__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return op_result and self != other return op_result and self != other
def _lt_from_ge(self, other): def _lt_from_ge(self, other, NotImplemented=NotImplemented):
'Return a < b. Computed by @total_ordering from (not a >= b).' 'Return a < b. Computed by @total_ordering from (not a >= b).'
op_result = self.__ge__(other) op_result = self.__ge__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment