8000 gh-102840: Fix confused traceback when `floordiv` or `mod` operations happens between `Fraction` and `complex` objects by Eclips4 · Pull Request #102842 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-102840: Fix confused traceback when floordiv or mod operations happens between Fraction and complex objects #102842

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

Merged
merged 10 commits into from
Feb 10, 2024
42 changes: 23 additions & 19 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,29 +611,33 @@ class doesn't subclass a concrete type, there's no

"""
def forward(a, b):
if isinstance(b, Fraction):
return monomorphic_operator(a, b)
elif isinstance(b, int):
return monomorphic_operator(a, Fraction(b))
elif isinstance(b, float):
return fallback_operator(float(a), b)
elif isinstance(b, complex):
return fallback_operator(complex(a), b)
else:
return NotImplemented
try:
if isinstance(b, Fraction):
return monomorphic_operator(a, b)
elif isinstance(b, int):
return monomorphic_operator(a, Fraction(b))
elif isinstance(b, float):
return fallback_operator(float(a), b)
elif isinstance(b, complex):
return fallback_operator(complex(a), b)
except TypeError:
pass
return NotImplemented
forward.__name__ = '__' + fallback_operator.__name__ + '__'
forward.__doc__ = monomorphic_operator.__doc__

def reverse(b, a):
if isinstance(a, numbers.Rational):
# Includes ints.
return monomorphic_operator(Fraction(a), b)
elif isinstance(a, numbers.Real):
return fallback_operator(float(a), float(b))
elif isinstance(a, numbers.Complex):
return fallback_operator(complex(a), complex(b))
else:
return NotImplemented
try:
if isinstance(a, numbers.Rational):
# Includes ints.
return monomorphic_operator(Fraction(a), b)
elif isinstance(a, numbers.Real):
return fallback_operator(float(a), float(b))
elif isinstance(a, numbers.Complex):
return fallback_operator(complex(a), complex(b))
except TypeError:
pass
return NotImplemented
reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
reverse.__doc__ = monomorphic_operator.__doc__

Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,29 @@ def test_invalid_formats(self):
with self.assertRaises(ValueError):
format(fraction, spec)

def test_traceback_in_mod_and_floordiv_with_complex_objects(self):
# See issue-102840 for more details.

a = F(1, 2)
b = 1j
mod = lambda x, y: x % y
floordiv = lambda x, y: x // y
message = "unsupported operand type(s) for %s: %s and %s"
# test forward
self.assertRaisesMessage(TypeError,
message % ("%", "'Fraction'", "'complex'"),
mod, a, b)
self.assertRaisesMessage(TypeError,
message % ("//", "'Fraction'", "'complex'"),
floordiv, a, b)
# test reverse
self.assertRaisesMessage(TypeError,
message % ("%", "'complex'", "'Fraction'"),
mod, b, a)
self.assertRaisesMessage(TypeError,
message % ("//", "'complex'", "'Fraction'"),
floordiv, b, a)


if __name__ == '__main__':
unittest.main()
0