mirror of
https://github.com/python/cpython.git
synced 2025-10-14 10:53:40 +00:00
gh-98727: Remove old style classes from test_cmath
(GH-98728)
This commit is contained in:
parent
f32369480d
commit
6777e09166
1 changed files with 5 additions and 33 deletions
|
@ -192,14 +192,7 @@ class CMathTests(unittest.TestCase):
|
||||||
# end up being passed to the cmath functions
|
# end up being passed to the cmath functions
|
||||||
|
|
||||||
# usual case: new-style class implementing __complex__
|
# usual case: new-style class implementing __complex__
|
||||||
class MyComplex(object):
|
class MyComplex:
|
||||||
def __init__(self, value):
|
|
||||||
self.value = value
|
|
||||||
def __complex__(self):
|
|
||||||
return self.value
|
|
||||||
|
|
||||||
# old-style class implementing __complex__
|
|
||||||
class MyComplexOS:
|
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.value = value
|
self.value = value
|
||||||
def __complex__(self):
|
def __complex__(self):
|
||||||
|
@ -208,18 +201,13 @@ class CMathTests(unittest.TestCase):
|
||||||
# classes for which __complex__ raises an exception
|
# classes for which __complex__ raises an exception
|
||||||
class SomeException(Exception):
|
class SomeException(Exception):
|
||||||
pass
|
pass
|
||||||
class MyComplexException(object):
|
class MyComplexException:
|
||||||
def __complex__(self):
|
|
||||||
raise SomeException
|
|
||||||
class MyComplexExceptionOS:
|
|
||||||
def __complex__(self):
|
def __complex__(self):
|
||||||
raise SomeException
|
raise SomeException
|
||||||
|
|
||||||
# some classes not providing __float__ or __complex__
|
# some classes not providing __float__ or __complex__
|
||||||
class NeitherComplexNorFloat(object):
|
class NeitherComplexNorFloat(object):
|
||||||
pass
|
pass
|
||||||
class NeitherComplexNorFloatOS:
|
|
||||||
pass
|
|
||||||
class Index:
|
class Index:
|
||||||
def __int__(self): return 2
|
def __int__(self): return 2
|
||||||
def __index__(self): return 2
|
def __index__(self): return 2
|
||||||
|
@ -228,48 +216,32 @@ class CMathTests(unittest.TestCase):
|
||||||
|
|
||||||
# other possible combinations of __float__ and __complex__
|
# other possible combinations of __float__ and __complex__
|
||||||
# that should work
|
# that should work
|
||||||
class FloatAndComplex(object):
|
class FloatAndComplex:
|
||||||
def __float__(self):
|
def __float__(self):
|
||||||
return flt_arg
|
return flt_arg
|
||||||
def __complex__(self):
|
def __complex__(self):
|
||||||
return cx_arg
|
return cx_arg
|
||||||
class FloatAndComplexOS:
|
class JustFloat:
|
||||||
def __float__(self):
|
|
||||||
return flt_arg
|
|
||||||
def __complex__(self):
|
|
||||||
return cx_arg
|
|
||||||
class JustFloat(object):
|
|
||||||
def __float__(self):
|
|
||||||
return flt_arg
|
|
||||||
class JustFloatOS:
|
|
||||||
def __float__(self):
|
def __float__(self):
|
||||||
return flt_arg
|
return flt_arg
|
||||||
|
|
||||||
for f in self.test_functions:
|
for f in self.test_functions:
|
||||||
# usual usage
|
# usual usage
|
||||||
self.assertEqual(f(MyComplex(cx_arg)), f(cx_arg))
|
self.assertEqual(f(MyComplex(cx_arg)), f(cx_arg))
|
||||||
self.assertEqual(f(MyComplexOS(cx_arg)), f(cx_arg))
|
|
||||||
# other combinations of __float__ and __complex__
|
# other combinations of __float__ and __complex__
|
||||||
self.assertEqual(f(FloatAndComplex()), f(cx_arg))
|
self.assertEqual(f(FloatAndComplex()), f(cx_arg))
|
||||||
self.assertEqual(f(FloatAndComplexOS()), f(cx_arg))
|
|
||||||
self.assertEqual(f(JustFloat()), f(flt_arg))
|
self.assertEqual(f(JustFloat()), f(flt_arg))
|
||||||
self.assertEqual(f(JustFloatOS()), f(flt_arg))
|
|
||||||
self.assertEqual(f(Index()), f(int(Index())))
|
self.assertEqual(f(Index()), f(int(Index())))
|
||||||
# TypeError should be raised for classes not providing
|
# TypeError should be raised for classes not providing
|
||||||
# either __complex__ or __float__, even if they provide
|
# either __complex__ or __float__, even if they provide
|
||||||
# __int__ or __index__. An old-style class
|
# __int__ or __index__:
|
||||||
# currently raises AttributeError instead of a TypeError;
|
|
||||||
# this could be considered a bug.
|
|
||||||
self.assertRaises(TypeError, f, NeitherComplexNorFloat())
|
self.assertRaises(TypeError, f, NeitherComplexNorFloat())
|
||||||
self.assertRaises(TypeError, f, MyInt())
|
self.assertRaises(TypeError, f, MyInt())
|
||||||
self.assertRaises(Exception, f, NeitherComplexNorFloatOS())
|
|
||||||
# non-complex return value from __complex__ -> TypeError
|
# non-complex return value from __complex__ -> TypeError
|
||||||
for bad_complex in non_complexes:
|
for bad_complex in non_complexes:
|
||||||
self.assertRaises(TypeError, f, MyComplex(bad_complex))
|
self.assertRaises(TypeError, f, MyComplex(bad_complex))
|
||||||
self.assertRaises(TypeError, f, MyComplexOS(bad_complex))
|
|
||||||
# exceptions in __complex__ should be propagated correctly
|
# exceptions in __complex__ should be propagated correctly
|
||||||
self.assertRaises(SomeException, f, MyComplexException())
|
self.assertRaises(SomeException, f, MyComplexException())
|
||||||
self.assertRaises(SomeException, f, MyComplexExceptionOS())
|
|
||||||
|
|
||||||
def test_input_type(self):
|
def test_input_type(self):
|
||||||
# ints should be acceptable inputs to all cmath
|
# ints should be acceptable inputs to all cmath
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue