Index: Lib/fractions.py =================================================================== --- Lib/fractions.py (revision 61097) +++ Lib/fractions.py (working copy) @@ -325,30 +325,68 @@ b.numerator * a.denominator, a.denominator * b.denominator) - __add__, __radd__ = _operator_fallbacks(_add, operator.add) + __xadd__, __radd__ = _operator_fallbacks(_add, operator.add) + def __add__(self, other): + if type(other) is Fraction: + na = self._numerator + da = self._denominator + nb = other._numerator + db = other._denominator + return Fraction(na * db + nb * da, da * db) + return self.__xadd__(other) + def _sub(a, b): """a - b""" return Fraction(a.numerator * b.denominator - b.numerator * a.denominator, a.denominator * b.denominator) - __sub__, __rsub__ = _operator_fallbacks(_sub, operator.sub) + __xsub__, __rsub__ = _operator_fallbacks(_sub, operator.sub) + def __sub__(self, other): + if type(other) is Fraction: + na = self._numerator + da = self._denominator + nb = other._numerator + db = other._denominator + return Fraction(na * db - nb * da, da * db) + return self.__xsub__(other) def _mul(a, b): """a * b""" - return Fraction(a.numerator * b.numerator, a.denominator * b.denominator) + return Fraction(a.numerator * b.numerator, + a.denominator * b.denominator) - __mul__, __rmul__ = _operator_fallbacks(_mul, operator.mul) + __xmul__, __rmul__ = _operator_fallbacks(_mul, operator.mul) + def __mul__(self, other): + if type(other) is Fraction: + return Fraction(self._numerator * other._numerator, + self._denominator * other._denominator) + return self.__xmul__(other) + + def _div(a, b): """a / b""" return Fraction(a.numerator * b.denominator, a.denominator * b.numerator) - __truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv) - __div__, __rdiv__ = _operator_fallbacks(_div, operator.div) + __xtruediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv) + __xdiv__, __rdiv__ = _operator_fallbacks(_div, operator.div) + def __truediv__(self, other): + if type(other) is Fraction: + return Fraction(self._numerator * other._denominator, + self._denominator * other._numerator) + return self.__xtruediv__(other) + + def __div__(self, other): + if type(other) is Fraction: + return Fraction(self._numerator * other._denominator, + self._denominator * other._numerator) + return self.__xdiv__(other) + + def __floordiv__(a, b): """a // b""" # Will be math.floor(a / b) in 3.0.