From ddc670bd7ea9825492e456a18e98b8fd041ae8b5 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 21 Jun 2021 14:53:21 +0300 Subject: [PATCH 01/11] Refactor test_float/complex.py Taken from #26827 Co-authored-by: Sergey B Kirpichev --- Lib/test/test_complex.py | 88 +++++++++++++++++++++++----------------- Lib/test/test_float.py | 58 +++++++++++++------------- 2 files changed, 81 insertions(+), 65 deletions(-) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 9180cca62b28b8..74349c187d8ba0 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -10,6 +10,42 @@ INF = float("inf") NAN = float("nan") + +class ComplexSubclass(complex): + pass + +class OtherComplexSubclass(complex): + pass + +class MyIndex: + def __init__(self, value): + self.value = value + + def __index__(self): + return self.value + +class MyInt: + def __init__(self, value): + self.value = value + + def __int__(self): + return self.value + +class FloatLike: + def __init__(self, value): + self.value = value + + def __float__(self): + return self.value + +class ComplexLike: + def __init__(self, value): + self.value = value + + def __complex__(self): + return self.value + + # These tests ensure that complex math does the right thing ZERO_DIVISION = ( @@ -306,14 +342,11 @@ def test_conjugate(self): self.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j) def test_constructor(self): - class NS: - def __init__(self, value): self.value = value - def __complex__(self): return self.value - self.assertEqual(complex(NS(1+10j)), 1+10j) - self.assertRaises(TypeError, complex, NS(None)) + self.assertEqual(complex(ComplexLike(1+10j)), 1+10j) + self.assertRaises(TypeError, complex, ComplexLike(None)) self.assertRaises(TypeError, complex, {}) - self.assertRaises(TypeError, complex, NS(1.5)) - self.assertRaises(TypeError, complex, NS(1)) + self.assertRaises(TypeError, complex, ComplexLike(1.5)) + self.assertRaises(TypeError, complex, ComplexLike(1)) self.assertAlmostEqual(complex("1+10j"), 1+10j) self.assertAlmostEqual(complex(10), 10+0j) @@ -360,8 +393,7 @@ def __complex__(self): return self.value self.assertAlmostEqual(complex('-1e-500j'), 0.0 - 0.0j) self.assertAlmostEqual(complex('-1e-500+1e-500j'), -0.0 + 0.0j) - class complex2(complex): pass - self.assertAlmostEqual(complex(complex2(1+1j)), 1+1j) + self.assertAlmostEqual(complex(ComplexSubclass(1+1j)), 1+1j) self.assertAlmostEqual(complex(real=17, imag=23), 17+23j) self.assertAlmostEqual(complex(real=17+23j), 17+23j) self.assertAlmostEqual(complex(real=17+23j, imag=23), 17+46j) @@ -443,33 +475,17 @@ def __complex__(self): self.assertRaises(EvilExc, complex, evilcomplex()) - class float2: - def __init__(self, value): - self.value = value - def __float__(self): - return self.value - - self.assertAlmostEqual(complex(float2(42.)), 42) - self.assertAlmostEqual(complex(real=float2(17.), imag=float2(23.)), 17+23j) - self.assertRaises(TypeError, complex, float2(None)) - - class MyIndex: - def __init__(self, value): - self.value = value - def __index__(self): - return self.value + self.assertAlmostEqual(complex(FloatLike(42.)), 42) + self.assertAlmostEqual(complex(real=FloatLike(17.), imag=FloatLike(23.)), 17+23j) + self.assertRaises(TypeError, complex, FloatLike(None)) self.assertAlmostEqual(complex(MyIndex(42)), 42.0+0.0j) self.assertAlmostEqual(complex(123, MyIndex(42)), 123.0+42.0j) self.assertRaises(OverflowError, complex, MyIndex(2**2000)) self.assertRaises(OverflowError, complex, 123, MyIndex(2**2000)) - class MyInt: - def __int__(self): - return 42 - - self.assertRaises(TypeError, complex, MyInt()) - self.assertRaises(TypeError, complex, 123, MyInt()) + self.assertRaises(TypeError, complex, MyInt(42)) + self.assertRaises(TypeError, complex, 123, MyInt(42)) class complex0(complex): """Test usage of __complex__() when inheriting from 'complex'""" @@ -508,24 +524,22 @@ class complex_subclass(complex): @support.requires_IEEE_754 def test_constructor_special_numbers(self): - class complex2(complex): - pass for x in 0.0, -0.0, INF, -INF, NAN: for y in 0.0, -0.0, INF, -INF, NAN: with self.subTest(x=x, y=y): z = complex(x, y) self.assertFloatsAreIdentical(z.real, x) self.assertFloatsAreIdentical(z.imag, y) - z = complex2(x, y) - self.assertIs(type(z), complex2) + z = ComplexSubclass(x, y) + self.assertIs(type(z), ComplexSubclass) self.assertFloatsAreIdentical(z.real, x) self.assertFloatsAreIdentical(z.imag, y) - z = complex(complex2(x, y)) + z = complex(ComplexSubclass(x, y)) self.assertIs(type(z), complex) self.assertFloatsAreIdentical(z.real, x) self.assertFloatsAreIdentical(z.imag, y) - z = complex2(complex(x, y)) - self.assertIs(type(z), complex2) + z = ComplexSubclass(complex(x, y)) + self.assertIs(type(z), ComplexSubclass) self.assertFloatsAreIdentical(z.real, x) self.assertFloatsAreIdentical(z.imag, y) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index b6daae7e9280ff..8999e7a8ea4534 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -33,6 +33,28 @@ class FloatSubclass(float): class OtherFloatSubclass(float): pass +class MyIndex: + def __init__(self, value): + self.value = value + + def __index__(self): + return self.value + +class MyInt: + def __init__(self, value): + self.value = value + + def __int__(self): + return self.value + +class FloatLike: + def __init__(self, value): + self.value = value + + def __float__(self): + return self.value + + class GeneralFloatCases(unittest.TestCase): def test_float(self): @@ -182,10 +204,6 @@ def test_float_with_comma(self): def test_floatconversion(self): # Make sure that calls to __float__() work properly - class Foo1(object): - def __float__(self): - return 42. - class Foo2(float): def __float__(self): return 42. @@ -207,45 +225,29 @@ class FooStr(str): def __float__(self): return float(str(self)) + 1 - self.assertEqual(float(Foo1()), 42.) + self.assertEqual(float(FloatLike(42.)), 42.) self.assertEqual(float(Foo2()), 42.) with self.assertWarns(DeprecationWarning): self.assertEqual(float(Foo3(21)), 42.) self.assertRaises(TypeError, float, Foo4(42)) self.assertEqual(float(FooStr('8')), 9.) - class Foo5: - def __float__(self): - return "" - self.assertRaises(TypeError, time.sleep, Foo5()) + self.assertRaises(TypeError, time.sleep, FloatLike("")) # Issue #24731 - class F: - def __float__(self): - return OtherFloatSubclass(42.) + f = FloatLike(OtherFloatSubclass(42.)) with self.assertWarns(DeprecationWarning): - self.assertEqual(float(F()), 42.) + self.assertEqual(float(f), 42.) with self.assertWarns(DeprecationWarning): - self.assertIs(type(float(F())), float) + self.assertIs(type(float(f)), float) with self.assertWarns(DeprecationWarning): - self.assertEqual(FloatSubclass(F()), 42.) + self.assertEqual(FloatSubclass(f), 42.) with self.assertWarns(DeprecationWarning): - self.assertIs(type(FloatSubclass(F())), FloatSubclass) - - class MyIndex: - def __init__(self, value): - self.value = value - def __index__(self): - return self.value + self.assertIs(type(FloatSubclass(f)), FloatSubclass) self.assertEqual(float(MyIndex(42)), 42.0) self.assertRaises(OverflowError, float, MyIndex(2**2000)) - - class MyInt: - def __int__(self): - return 42 - - self.assertRaises(TypeError, float, MyInt()) + self.assertRaises(TypeError, float, MyInt(42)) def test_keyword_args(self): with self.assertRaisesRegex(TypeError, 'keyword argument'): From 944de0773c389f0bdba559dcf0dff9afdcf56c46 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 17 Oct 2023 07:31:32 +0300 Subject: [PATCH 02/11] A continuation: factor out Complex/FloatLikeSubclass'es --- Lib/test/test_complex.py | 36 +++++++++++------------------------- Lib/test/test_float.py | 35 +++++++++++++---------------------- 2 files changed, 24 insertions(+), 47 deletions(-) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 74349c187d8ba0..1ec070ebdd2912 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -45,6 +45,13 @@ def __init__(self, value): def __complex__(self): return self.value +class ComplexLikeSubclass(complex): + def __init__(self, value): + self.value = value + + def __complex__(self): + return self.value + # These tests ensure that complex math does the right thing @@ -487,38 +494,17 @@ def __complex__(self): self.assertRaises(TypeError, complex, MyInt(42)) self.assertRaises(TypeError, complex, 123, MyInt(42)) - class complex0(complex): - """Test usage of __complex__() when inheriting from 'complex'""" - def __complex__(self): - return 42j - - class complex1(complex): - """Test usage of __complex__() with a __new__() method""" - def __new__(self, value=0j): - return complex.__new__(self, 2*value) - def __complex__(self): - return self - - class complex2(complex): - """Make sure that __complex__() calls fail if anything other than a - complex is returned""" - def __complex__(self): - return None - - self.assertEqual(complex(complex0(1j)), 42j) + self.assertEqual(complex(ComplexLikeSubclass(42j)), 42j) with self.assertWarns(DeprecationWarning): - self.assertEqual(complex(complex1(1j)), 2j) - self.assertRaises(TypeError, complex, complex2(1j)) + self.assertEqual(complex(ComplexLikeSubclass(ComplexSubclass(2j))), 2j) + self.assertRaises(TypeError, complex, ComplexLikeSubclass(42)) def test___complex__(self): z = 3 + 4j self.assertEqual(z.__complex__(), z) self.assertEqual(type(z.__complex__()), complex) - class complex_subclass(complex): - pass - - z = complex_subclass(3 + 4j) + z = ComplexSubclass(3 + 4j) self.assertEqual(z.__complex__(), 3 + 4j) self.assertEqual(type(z.__complex__()), complex) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 8999e7a8ea4534..a3c54bf3f5118d 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -54,6 +54,13 @@ def __init__(self, value): def __float__(self): return self.value +class FloatLikeSubclass(float): + def __init__(self, value): + self.value = value + + def __float__(self): + return self.value + class GeneralFloatCases(unittest.TestCase): @@ -204,20 +211,6 @@ def test_float_with_comma(self): def test_floatconversion(self): # Make sure that calls to __float__() work properly - class Foo2(float): - def __float__(self): - return 42. - - class Foo3(float): - def __new__(cls, value=0.): - return float.__new__(cls, 2*value) - - def __float__(self): - return self - - class Foo4(float): - def __float__(self): - return 42 # Issue 5759: __float__ not called on str subclasses (though it is on # unicode subclasses). @@ -226,10 +219,10 @@ def __float__(self): return float(str(self)) + 1 self.assertEqual(float(FloatLike(42.)), 42.) - self.assertEqual(float(Foo2()), 42.) + self.assertEqual(float(FloatLikeSubclass(42.)), 42.) with self.assertWarns(DeprecationWarning): - self.assertEqual(float(Foo3(21)), 42.) - self.assertRaises(TypeError, float, Foo4(42)) + self.assertEqual(float(FloatLikeSubclass(FloatSubclass(42))), 42.) + self.assertRaises(TypeError, float, FloatLikeSubclass(42)) self.assertEqual(float(FooStr('8')), 9.) self.assertRaises(TypeError, time.sleep, FloatLike("")) @@ -254,13 +247,11 @@ def test_keyword_args(self): float(x='3.14') def test_keywords_in_subclass(self): - class subclass(float): - pass - u = subclass(2.5) - self.assertIs(type(u), subclass) + u = FloatSubclass(2.5) + self.assertIs(type(u), FloatSubclass) self.assertEqual(float(u), 2.5) with self.assertRaises(TypeError): - subclass(x=0) + FloatSubclass(x=0) class subclass_with_init(float): def __init__(self, arg, newarg=None): From 0dedc809fdc796645f9dc5e51a82c33139f80b5e Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 4 Nov 2023 06:32:21 +0300 Subject: [PATCH 03/11] Move support classes to Lib/test/support/classes.py --- Lib/test/support/classes.py | 58 +++++++++++++++++++++++++++++++++++++ Lib/test/test_complex.py | 44 ++-------------------------- Lib/test/test_float.py | 36 ++--------------------- 3 files changed, 63 insertions(+), 75 deletions(-) create mode 100644 Lib/test/support/classes.py diff --git a/Lib/test/support/classes.py b/Lib/test/support/classes.py new file mode 100644 index 00000000000000..72ac9fb1e50e76 --- /dev/null +++ b/Lib/test/support/classes.py @@ -0,0 +1,58 @@ +# Common test classes. + +class MyIndex: + def __init__(self, value): + self.value = value + + def __index__(self): + return self.value + + +class MyInt: + def __init__(self, value): + self.value = value + + def __int__(self): + return self.value + + +class FloatSubclass(float): + pass + +class OtherFloatSubclass(float): + pass + +class FloatLike: + def __init__(self, value): + self.value = value + + def __float__(self): + return self.value + +class FloatLikeSubclass(float): + def __init__(self, value): + self.value = value + + def __float__(self): + return self.value + + +class ComplexSubclass(complex): + pass + +class OtherComplexSubclass(complex): + pass + +class ComplexLike: + def __init__(self, value): + self.value = value + + def __complex__(self): + return self.value + +class ComplexLikeSubclass(complex): + def __init__(self, value): + self.value = value + + def __complex__(self): + return self.value diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 1ec070ebdd2912..55e6a9749ce8bc 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -3,6 +3,9 @@ from test import support from test.test_grammar import (VALID_UNDERSCORE_LITERALS, INVALID_UNDERSCORE_LITERALS) +from test.support.classes import (ComplexSubclass, ComplexLike, + ComplexLikeSubclass, FloatLike, + MyIndex, MyInt) from random import random from math import atan2, isnan, copysign @@ -11,47 +14,6 @@ INF = float("inf") NAN = float("nan") -class ComplexSubclass(complex): - pass - -class OtherComplexSubclass(complex): - pass - -class MyIndex: - def __init__(self, value): - self.value = value - - def __index__(self): - return self.value - -class MyInt: - def __init__(self, value): - self.value = value - - def __int__(self): - return self.value - -class FloatLike: - def __init__(self, value): - self.value = value - - def __float__(self): - return self.value - -class ComplexLike: - def __init__(self, value): - self.value = value - - def __complex__(self): - return self.value - -class ComplexLikeSubclass(complex): - def __init__(self, value): - self.value = value - - def __complex__(self): - return self.value - # These tests ensure that complex math does the right thing diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index a3c54bf3f5118d..d1158f8d3b8a50 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -10,6 +10,8 @@ from test import support from test.test_grammar import (VALID_UNDERSCORE_LITERALS, INVALID_UNDERSCORE_LITERALS) +from test.support.classes import (FloatSubclass, OtherFloatSubclass, FloatLike, + FloatLikeSubclass, MyIndex, MyInt) from math import isinf, isnan, copysign, ldexp import math @@ -27,40 +29,6 @@ test_dir = os.path.dirname(__file__) or os.curdir format_testfile = os.path.join(test_dir, 'mathdata', 'formatfloat_testcases.txt') -class FloatSubclass(float): - pass - -class OtherFloatSubclass(float): - pass - -class MyIndex: - def __init__(self, value): - self.value = value - - def __index__(self): - return self.value - -class MyInt: - def __init__(self, value): - self.value = value - - def __int__(self): - return self.value - -class FloatLike: - def __init__(self, value): - self.value = value - - def __float__(self): - return self.value - -class FloatLikeSubclass(float): - def __init__(self, value): - self.value = value - - def __float__(self): - return self.value - class GeneralFloatCases(unittest.TestCase): From aa0fe8935e06a1c7caa354ebeb4cf41646d11e4e Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 4 Nov 2023 06:36:55 +0300 Subject: [PATCH 04/11] Rename MyIndex -> IndexLike, MyInt -> IntLike --- Lib/test/support/classes.py | 4 ++-- Lib/test/test_complex.py | 14 +++++++------- Lib/test/test_float.py | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Lib/test/support/classes.py b/Lib/test/support/classes.py index 72ac9fb1e50e76..ddb6e7b7e92786 100644 --- a/Lib/test/support/classes.py +++ b/Lib/test/support/classes.py @@ -1,6 +1,6 @@ # Common test classes. -class MyIndex: +class IndexLike: def __init__(self, value): self.value = value @@ -8,7 +8,7 @@ def __index__(self): return self.value -class MyInt: +class IntLike: def __init__(self, value): self.value = value diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 55e6a9749ce8bc..9d621c5e3ac41a 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -5,7 +5,7 @@ INVALID_UNDERSCORE_LITERALS) from test.support.classes import (ComplexSubclass, ComplexLike, ComplexLikeSubclass, FloatLike, - MyIndex, MyInt) + IndexLike, IntLike) from random import random from math import atan2, isnan, copysign @@ -448,13 +448,13 @@ def __complex__(self): self.assertAlmostEqual(complex(real=FloatLike(17.), imag=FloatLike(23.)), 17+23j) self.assertRaises(TypeError, complex, FloatLike(None)) - self.assertAlmostEqual(complex(MyIndex(42)), 42.0+0.0j) - self.assertAlmostEqual(complex(123, MyIndex(42)), 123.0+42.0j) - self.assertRaises(OverflowError, complex, MyIndex(2**2000)) - self.assertRaises(OverflowError, complex, 123, MyIndex(2**2000)) + self.assertAlmostEqual(complex(IndexLike(42)), 42.0+0.0j) + self.assertAlmostEqual(complex(123, IndexLike(42)), 123.0+42.0j) + self.assertRaises(OverflowError, complex, IndexLike(2**2000)) + self.assertRaises(OverflowError, complex, 123, IndexLike(2**2000)) - self.assertRaises(TypeError, complex, MyInt(42)) - self.assertRaises(TypeError, complex, 123, MyInt(42)) + self.assertRaises(TypeError, complex, IntLike(42)) + self.assertRaises(TypeError, complex, 123, IntLike(42)) self.assertEqual(complex(ComplexLikeSubclass(42j)), 42j) with self.assertWarns(DeprecationWarning): diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index d1158f8d3b8a50..6d59f2fbc1507a 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -11,7 +11,7 @@ from test.test_grammar import (VALID_UNDERSCORE_LITERALS, INVALID_UNDERSCORE_LITERALS) from test.support.classes import (FloatSubclass, OtherFloatSubclass, FloatLike, - FloatLikeSubclass, MyIndex, MyInt) + FloatLikeSubclass, IndexLike, IntLike) from math import isinf, isnan, copysign, ldexp import math @@ -206,9 +206,9 @@ def __float__(self): with self.assertWarns(DeprecationWarning): self.assertIs(type(FloatSubclass(f)), FloatSubclass) - self.assertEqual(float(MyIndex(42)), 42.0) - self.assertRaises(OverflowError, float, MyIndex(2**2000)) - self.assertRaises(TypeError, float, MyInt(42)) + self.assertEqual(float(IndexLike(42)), 42.0) + self.assertRaises(OverflowError, float, IndexLike(2**2000)) + self.assertRaises(TypeError, float, IntLike(42)) def test_keyword_args(self): with self.assertRaisesRegex(TypeError, 'keyword argument'): From aeeba488fce64febb78de71b16cdbc095e4688cf Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 4 Nov 2023 06:46:46 +0300 Subject: [PATCH 05/11] Refactor Lib/test/test_capi/test_long.py --- Lib/test/support/classes.py | 13 +++++++ Lib/test/test_capi/test_long.py | 68 +++++++++++++-------------------- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/Lib/test/support/classes.py b/Lib/test/support/classes.py index ddb6e7b7e92786..d76e1cd2adc251 100644 --- a/Lib/test/support/classes.py +++ b/Lib/test/support/classes.py @@ -8,6 +8,9 @@ def __index__(self): return self.value +class IntSubclass(int): + pass + class IntLike: def __init__(self, value): self.value = value @@ -15,6 +18,16 @@ def __init__(self, value): def __int__(self): return self.value +class IntAndIndexLike: + def __init__(self, value): + self.value = value + + def __index__(self): + return self.value + + def __int__(self): + return self.value + 12 + class FloatSubclass(float): pass diff --git a/Lib/test/test_capi/test_long.py b/Lib/test/test_capi/test_long.py index 8e3ef25d1ff86f..a3445a8ac61330 100644 --- a/Lib/test/test_capi/test_long.py +++ b/Lib/test/test_capi/test_long.py @@ -2,29 +2,13 @@ import sys from test.support import import_helper +from test.support.classes import IntSubclass, IndexLike, IntAndIndexLike # Skip this test if the _testcapi module isn't available. _testcapi = import_helper.import_module('_testcapi') NULL = None -class IntSubclass(int): - pass - -class Index: - def __init__(self, value): - self.value = value - - def __index__(self): - return self.value - -# use __index__(), not __int__() -class MyIndexAndInt: - def __index__(self): - return 10 - def __int__(self): - return 22 - class LongTests(unittest.TestCase): @@ -170,8 +154,8 @@ def test_long_asint(self): with self.subTest(value=value): self.assertEqual(PyLong_AsInt(value), value) self.assertEqual(PyLong_AsInt(IntSubclass(42)), 42) - self.assertEqual(PyLong_AsInt(Index(42)), 42) - self.assertEqual(PyLong_AsInt(MyIndexAndInt()), 10) + self.assertEqual(PyLong_AsInt(IndexLike(42)), 42) + self.assertEqual(PyLong_AsInt(IntAndIndexLike(10)), 10) # bound checking self.assertRaises(OverflowError, PyLong_AsInt, INT_MIN - 1) @@ -193,8 +177,8 @@ def test_long_aslong(self): self.assertEqual(aslong(value), value) self.assertEqual(aslong(IntSubclass(42)), 42) - self.assertEqual(aslong(Index(42)), 42) - self.assertEqual(aslong(MyIndexAndInt()), 10) + self.assertEqual(aslong(IndexLike(42)), 42) + self.assertEqual(aslong(IntAndIndexLike(10)), 10) self.assertRaises(OverflowError, aslong, LONG_MIN - 1) self.assertRaises(OverflowError, aslong, LONG_MAX + 1) @@ -213,8 +197,8 @@ def test_long_aslongandoverflow(self): self.assertEqual(aslongandoverflow(value), (value, 0)) self.assertEqual(aslongandoverflow(IntSubclass(42)), (42, 0)) - self.assertEqual(aslongandoverflow(Index(42)), (42, 0)) - self.assertEqual(aslongandoverflow(MyIndexAndInt()), (10, 0)) + self.assertEqual(aslongandoverflow(IndexLike(42)), (42, 0)) + self.assertEqual(aslongandoverflow(IntAndIndexLike(10)), (10, 0)) self.assertEqual(aslongandoverflow(LONG_MIN - 1), (-1, -1)) self.assertEqual(aslongandoverflow(LONG_MAX + 1), (-1, 1)) @@ -231,8 +215,8 @@ def test_long_asunsignedlong(self): self.assertEqual(asunsignedlong(value), value) self.assertEqual(asunsignedlong(IntSubclass(42)), 42) - self.assertRaises(TypeError, asunsignedlong, Index(42)) - self.assertRaises(TypeError, asunsignedlong, MyIndexAndInt()) + self.assertRaises(TypeError, asunsignedlong, IndexLike(42)) + self.assertRaises(TypeError, asunsignedlong, IntAndIndexLike(10)) self.assertRaises(OverflowError, asunsignedlong, -1) self.assertRaises(OverflowError, asunsignedlong, ULONG_MAX + 1) @@ -251,8 +235,8 @@ def test_long_asunsignedlongmask(self): self.assertEqual(asunsignedlongmask(value), value) self.assertEqual(asunsignedlongmask(IntSubclass(42)), 42) - self.assertEqual(asunsignedlongmask(Index(42)), 42) - self.assertEqual(asunsignedlongmask(MyIndexAndInt()), 10) + self.assertEqual(asunsignedlongmask(IndexLike(42)), 42) + self.assertEqual(asunsignedlongmask(IntAndIndexLike(10)), 10) self.assertEqual(asunsignedlongmask(-1), ULONG_MAX) self.assertEqual(asunsignedlongmask(ULONG_MAX + 1), 0) @@ -271,8 +255,8 @@ def test_long_aslonglong(self): self.assertEqual(aslonglong(value), value) self.assertEqual(aslonglong(IntSubclass(42)), 42) - self.assertEqual(aslonglong(Index(42)), 42) - self.assertEqual(aslonglong(MyIndexAndInt()), 10) + self.assertEqual(aslonglong(IndexLike(42)), 42) + self.assertEqual(aslonglong(IntAndIndexLike(10)), 10) self.assertRaises(OverflowError, aslonglong, LLONG_MIN - 1) self.assertRaises(OverflowError, aslonglong, LLONG_MAX + 1) @@ -291,8 +275,8 @@ def test_long_aslonglongandoverflow(self): self.assertEqual(aslonglongandoverflow(value), (value, 0)) self.assertEqual(aslonglongandoverflow(IntSubclass(42)), (42, 0)) - self.assertEqual(aslonglongandoverflow(Index(42)), (42, 0)) - self.assertEqual(aslonglongandoverflow(MyIndexAndInt()), (10, 0)) + self.assertEqual(aslonglongandoverflow(IndexLike(42)), (42, 0)) + self.assertEqual(aslonglongandoverflow(IntAndIndexLike(10)), (10, 0)) self.assertEqual(aslonglongandoverflow(LLONG_MIN - 1), (-1, -1)) self.assertEqual(aslonglongandoverflow(LLONG_MAX + 1), (-1, 1)) @@ -309,8 +293,8 @@ def test_long_asunsignedlonglong(self): self.assertEqual(asunsignedlonglong(value), value) self.assertEqual(asunsignedlonglong(IntSubclass(42)), 42) - self.assertRaises(TypeError, asunsignedlonglong, Index(42)) - self.assertRaises(TypeError, asunsignedlonglong, MyIndexAndInt()) + self.assertRaises(TypeError, asunsignedlonglong, IndexLike(42)) + self.assertRaises(TypeError, asunsignedlonglong, IntAndIndexLike(10)) self.assertRaises(OverflowError, asunsignedlonglong, -1) self.assertRaises(OverflowError, asunsignedlonglong, ULLONG_MAX + 1) @@ -329,8 +313,8 @@ def test_long_asunsignedlonglongmask(self): self.assertEqual(asunsignedlonglongmask(value), value) self.assertEqual(asunsignedlonglongmask(IntSubclass(42)), 42) - self.assertEqual(asunsignedlonglongmask(Index(42)), 42) - self.assertEqual(asunsignedlonglongmask(MyIndexAndInt()), 10) + self.assertEqual(asunsignedlonglongmask(IndexLike(42)), 42) + self.assertEqual(asunsignedlonglongmask(IntAndIndexLike(10)), 10) self.assertEqual(asunsignedlonglongmask(-1), ULLONG_MAX) self.assertEqual(asunsignedlonglongmask(ULLONG_MAX + 1), 0) @@ -349,8 +333,8 @@ def test_long_as_ssize_t(self): self.assertEqual(as_ssize_t(value), value) self.assertEqual(as_ssize_t(IntSubclass(42)), 42) - self.assertRaises(TypeError, as_ssize_t, Index(42)) - self.assertRaises(TypeError, as_ssize_t, MyIndexAndInt()) + self.assertRaises(TypeError, as_ssize_t, IndexLike(42)) + self.assertRaises(TypeError, as_ssize_t, IntAndIndexLike(10)) self.assertRaises(OverflowError, as_ssize_t, PY_SSIZE_T_MIN - 1) self.assertRaises(OverflowError, as_ssize_t, PY_SSIZE_T_MAX + 1) @@ -369,8 +353,8 @@ def test_long_as_size_t(self): self.assertEqual(as_size_t(value), value) self.assertEqual(as_size_t(IntSubclass(42)), 42) - self.assertRaises(TypeError, as_size_t, Index(42)) - self.assertRaises(TypeError, as_size_t, MyIndexAndInt()) + self.assertRaises(TypeError, as_size_t, IndexLike(42)) + self.assertRaises(TypeError, as_size_t, IntAndIndexLike(10)) self.assertRaises(OverflowError, as_size_t, -1) self.assertRaises(OverflowError, as_size_t, SIZE_MAX + 1) @@ -389,8 +373,8 @@ def test_long_asdouble(self): self.assertIsInstance(asdouble(value), float) self.assertEqual(asdouble(IntSubclass(42)), 42.0) - self.assertRaises(TypeError, asdouble, Index(42)) - self.assertRaises(TypeError, asdouble, MyIndexAndInt()) + self.assertRaises(TypeError, asdouble, IndexLike(42)) + self.assertRaises(TypeError, asdouble, IntAndIndexLike(10)) self.assertRaises(OverflowError, asdouble, 2 * MAX) self.assertRaises(OverflowError, asdouble, -2 * MAX) @@ -417,7 +401,7 @@ def test_long_asvoidptr(self): if y >= M//2: self.assertIs(asvoidptr(y - M), NULL) - self.assertRaises(TypeError, asvoidptr, Index(x)) + self.assertRaises(TypeError, asvoidptr, IndexLike(x)) self.assertRaises(TypeError, asvoidptr, object()) self.assertRaises(OverflowError, asvoidptr, 2**1000) self.assertRaises(OverflowError, asvoidptr, -2**1000) From 223c50970f420541d79b07382089f0e11da4e56a Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 4 Nov 2023 07:32:28 +0300 Subject: [PATCH 06/11] Redo Float/ComplexSubclass to return a different value from dunder method --- Lib/test/support/classes.py | 4 ++-- Lib/test/test_complex.py | 4 ++-- Lib/test/test_float.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/test/support/classes.py b/Lib/test/support/classes.py index d76e1cd2adc251..50db0916a06545 100644 --- a/Lib/test/support/classes.py +++ b/Lib/test/support/classes.py @@ -47,7 +47,7 @@ def __init__(self, value): self.value = value def __float__(self): - return self.value + return self.value.__class__(self.value*2) class ComplexSubclass(complex): @@ -68,4 +68,4 @@ def __init__(self, value): self.value = value def __complex__(self): - return self.value + return self.value.__class__(self.value*2) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 9d621c5e3ac41a..3523f0765ecd78 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -456,9 +456,9 @@ def __complex__(self): self.assertRaises(TypeError, complex, IntLike(42)) self.assertRaises(TypeError, complex, 123, IntLike(42)) - self.assertEqual(complex(ComplexLikeSubclass(42j)), 42j) + self.assertEqual(complex(ComplexLikeSubclass(21j)), 42j) with self.assertWarns(DeprecationWarning): - self.assertEqual(complex(ComplexLikeSubclass(ComplexSubclass(2j))), 2j) + self.assertEqual(complex(ComplexLikeSubclass(ComplexSubclass(1j))), 2j) self.assertRaises(TypeError, complex, ComplexLikeSubclass(42)) def test___complex__(self): diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 6d59f2fbc1507a..a722089af6602a 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -187,9 +187,9 @@ def __float__(self): return float(str(self)) + 1 self.assertEqual(float(FloatLike(42.)), 42.) - self.assertEqual(float(FloatLikeSubclass(42.)), 42.) + self.assertEqual(float(FloatLikeSubclass(21.)), 42.) with self.assertWarns(DeprecationWarning): - self.assertEqual(float(FloatLikeSubclass(FloatSubclass(42))), 42.) + self.assertEqual(float(FloatLikeSubclass(FloatSubclass(21))), 42.) self.assertRaises(TypeError, float, FloatLikeSubclass(42)) self.assertEqual(float(FooStr('8')), 9.) From c2604afb1a52beeaed4da6ec6d94f1455b3246de Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 1 Jul 2024 03:49:11 +0300 Subject: [PATCH 07/11] +1 --- Lib/test/test_capi/test_long.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_capi/test_long.py b/Lib/test/test_capi/test_long.py index 27508f8cc27e1e..123478cab4f980 100644 --- a/Lib/test/test_capi/test_long.py +++ b/Lib/test/test_capi/test_long.py @@ -594,9 +594,9 @@ def test_long_asnativebytes(self): # Ensure omitting Py_ASNATIVEBYTES_ALLOW_INDEX raises on __index__ value with self.assertRaises(TypeError): - asnativebytes(Index(1), buffer, 0, -1) + asnativebytes(WithIndex(1), buffer, 0, -1) with self.assertRaises(TypeError): - asnativebytes(Index(1), buffer, 0, 3) + asnativebytes(WithIndex(1), buffer, 0, 3) # Check a few error conditions. These are validated in code, but are # unspecified in docs, so if we make changes to the implementation, it's @@ -724,7 +724,7 @@ def test_long_getsign(self): self.assertEqual(getsign(False), 0) self.assertRaises(TypeError, getsign, 1.0) - self.assertRaises(TypeError, getsign, Index(123)) + self.assertRaises(TypeError, getsign, WithIndex(123)) # CRASHES getsign(NULL) From 09c824109398fa42494dd624eb69e844a6dfda0e Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 1 Jul 2024 03:50:12 +0300 Subject: [PATCH 08/11] + convert Lib/test/test_capi/test_complex.py --- Lib/test/test_capi/test_complex.py | 51 +++++++++++++++--------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/Lib/test/test_capi/test_complex.py b/Lib/test/test_capi/test_complex.py index 328ea12f97462c..9ba6b94d8e4154 100644 --- a/Lib/test/test_capi/test_complex.py +++ b/Lib/test/test_capi/test_complex.py @@ -3,10 +3,9 @@ import unittest import warnings -from test.test_capi.test_getargs import (BadComplex, BadComplex2, Complex, - FloatSubclass, Float, BadFloat, - BadFloat2, ComplexSubclass) from test.support import import_helper +from test.support.classes import (ComplexSubclass, FloatSubclass, + WithFloat, WithComplex) _testcapi = import_helper.import_module('_testcapi') @@ -30,7 +29,7 @@ def test_check(self): self.assertTrue(check(1+2j)) self.assertTrue(check(ComplexSubclass(1+2j))) - self.assertFalse(check(Complex())) + self.assertFalse(check(WithComplex(4.25+0.5j))) self.assertFalse(check(3)) self.assertFalse(check(3.0)) self.assertFalse(check(object())) @@ -43,7 +42,7 @@ def test_checkexact(self): self.assertTrue(checkexact(1+2j)) self.assertFalse(checkexact(ComplexSubclass(1+2j))) - self.assertFalse(checkexact(Complex())) + self.assertFalse(checkexact(WithComplex(4.25+0.5j))) self.assertFalse(checkexact(3)) self.assertFalse(checkexact(3.0)) self.assertFalse(checkexact(object())) @@ -78,20 +77,20 @@ def test_realasdouble(self): self.assertEqual(realasdouble(FloatSubclass(4.25)), 4.25) # Test types with __complex__ dunder method - self.assertEqual(realasdouble(Complex()), 4.25) - self.assertRaises(TypeError, realasdouble, BadComplex()) + self.assertEqual(realasdouble(WithComplex(4.25+0.5j)), 4.25) + self.assertRaises(TypeError, realasdouble, WithComplex(1.25)) with self.assertWarns(DeprecationWarning): - self.assertEqual(realasdouble(BadComplex2()), 4.25) + self.assertEqual(realasdouble(WithComplex(ComplexSubclass(4.25+0.5j))), 4.25) with warnings.catch_warnings(): warnings.simplefilter("error", DeprecationWarning) - self.assertRaises(DeprecationWarning, realasdouble, BadComplex2()) + self.assertRaises(DeprecationWarning, realasdouble, WithComplex(ComplexSubclass(4.25+0.5j))) self.assertRaises(RuntimeError, realasdouble, BadComplex3()) # Test types with __float__ dunder method - self.assertEqual(realasdouble(Float()), 4.25) - self.assertRaises(TypeError, realasdouble, BadFloat()) + self.assertEqual(realasdouble(WithFloat(4.25)), 4.25) + self.assertRaises(TypeError, realasdouble, WithFloat(687)) with self.assertWarns(DeprecationWarning): - self.assertEqual(realasdouble(BadFloat2()), 4.25) + self.assertEqual(realasdouble(WithFloat(FloatSubclass(4.25))), 4.25) self.assertRaises(TypeError, realasdouble, object()) @@ -111,20 +110,20 @@ def test_imagasdouble(self): self.assertEqual(imagasdouble(FloatSubclass(4.25)), 0.0) # Test types with __complex__ dunder method - self.assertEqual(imagasdouble(Complex()), 0.5) - self.assertRaises(TypeError, imagasdouble, BadComplex()) + self.assertEqual(imagasdouble(WithComplex(4.25+0.5j)), 0.5) + self.assertRaises(TypeError, imagasdouble, WithComplex(1.25)) with self.assertWarns(DeprecationWarning): - self.assertEqual(imagasdouble(BadComplex2()), 0.5) + self.assertEqual(imagasdouble(WithComplex(ComplexSubclass(4.25+0.5j))), 0.5) with warnings.catch_warnings(): warnings.simplefilter("error", DeprecationWarning) - self.assertRaises(DeprecationWarning, imagasdouble, BadComplex2()) + self.assertRaises(DeprecationWarning, imagasdouble, WithComplex(ComplexSubclass(4.25+0.5j))) self.assertRaises(RuntimeError, imagasdouble, BadComplex3()) # Test types with __float__ dunder method - self.assertEqual(imagasdouble(Float()), 0.0) - self.assertRaises(TypeError, imagasdouble, BadFloat()) + self.assertEqual(imagasdouble(WithFloat(4.25)), 0.0) + self.assertRaises(TypeError, imagasdouble, WithFloat(687)) with self.assertWarns(DeprecationWarning): - self.assertEqual(imagasdouble(BadFloat2()), 0.0) + self.assertEqual(imagasdouble(WithFloat(FloatSubclass(4.25))), 0.0) self.assertRaises(TypeError, imagasdouble, object()) @@ -146,20 +145,20 @@ def test_asccomplex(self): self.assertEqual(asccomplex(FloatSubclass(4.25)), 4.25+0.0j) # Test types with __complex__ dunder method - self.assertEqual(asccomplex(Complex()), 4.25+0.5j) - self.assertRaises(TypeError, asccomplex, BadComplex()) + self.assertEqual(asccomplex(WithComplex(4.25+0.5j)), 4.25+0.5j) + self.assertRaises(TypeError, asccomplex, WithComplex(1.25)) with self.assertWarns(DeprecationWarning): - self.assertEqual(asccomplex(BadComplex2()), 4.25+0.5j) + self.assertEqual(asccomplex(WithComplex(ComplexSubclass(4.25+0.5j))), 4.25+0.5j) with warnings.catch_warnings(): warnings.simplefilter("error", DeprecationWarning) - self.assertRaises(DeprecationWarning, asccomplex, BadComplex2()) + self.assertRaises(DeprecationWarning, asccomplex, WithComplex(ComplexSubclass(4.25+0.5j))) self.assertRaises(RuntimeError, asccomplex, BadComplex3()) # Test types with __float__ dunder method - self.assertEqual(asccomplex(Float()), 4.25+0.0j) - self.assertRaises(TypeError, asccomplex, BadFloat()) + self.assertEqual(asccomplex(WithFloat(4.25)), 4.25+0.0j) + self.assertRaises(TypeError, asccomplex, WithFloat(687)) with self.assertWarns(DeprecationWarning): - self.assertEqual(asccomplex(BadFloat2()), 4.25+0.0j) + self.assertEqual(asccomplex(WithFloat(FloatSubclass(4.25))), 4.25+0.0j) self.assertRaises(TypeError, asccomplex, object()) From dae5b2cb4d060c38e3a59169bb5c230d299969da Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 1 Jul 2024 04:28:59 +0300 Subject: [PATCH 09/11] + convert Lib/test/test_capi/test_float.py --- Lib/test/test_capi/test_float.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index 92c987794142c9..684d825c55805b 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -3,10 +3,8 @@ import unittest import warnings -from test.test_capi.test_getargs import (Float, FloatSubclass, FloatSubclass2, - BadIndex2, BadFloat2, Index, BadIndex, - BadFloat) from test.support import import_helper +from test.support.classes import FloatSubclass, WithIndex, WithFloat _testcapi = import_helper.import_module('_testcapi') _testlimitedcapi = import_helper.import_module('_testlimitedcapi') @@ -27,6 +25,10 @@ NAN = float("nan") +class FloatSubclass2(WithFloat, FloatSubclass): + pass + + class CAPIFloatTest(unittest.TestCase): def test_check(self): # Test PyFloat_Check() @@ -34,7 +36,7 @@ def test_check(self): self.assertTrue(check(4.25)) self.assertTrue(check(FloatSubclass(4.25))) - self.assertFalse(check(Float())) + self.assertFalse(check(WithFloat(4.25))) self.assertFalse(check(3)) self.assertFalse(check(object())) @@ -46,7 +48,7 @@ def test_checkexact(self): self.assertTrue(checkexact(4.25)) self.assertFalse(checkexact(FloatSubclass(4.25))) - self.assertFalse(checkexact(Float())) + self.assertFalse(checkexact(WithFloat(4.25))) self.assertFalse(checkexact(3)) self.assertFalse(checkexact(object())) @@ -93,18 +95,18 @@ def __float__(self): self.assertEqual(asdouble(FloatSubclass(4.25)), 4.25) self.assertEqual(asdouble(FloatSubclass2(4.25)), 4.25) - self.assertEqual(asdouble(Index()), 99.) + self.assertEqual(asdouble(WithIndex(99)), 99.) - self.assertRaises(TypeError, asdouble, BadIndex()) - self.assertRaises(TypeError, asdouble, BadFloat()) + self.assertRaises(TypeError, asdouble, WithIndex(1.0)) + self.assertRaises(TypeError, asdouble, WithFloat(687)) self.assertRaises(RuntimeError, asdouble, BadFloat3()) with self.assertWarns(DeprecationWarning): - self.assertEqual(asdouble(BadIndex2()), 1.) + self.assertEqual(asdouble(WithIndex(True)), 1.) with self.assertWarns(DeprecationWarning): - self.assertEqual(asdouble(BadFloat2()), 4.25) + self.assertEqual(asdouble(WithFloat(FloatSubclass(4.25))), 4.25) with warnings.catch_warnings(): warnings.simplefilter("error", DeprecationWarning) - self.assertRaises(DeprecationWarning, asdouble, BadFloat2()) + self.assertRaises(DeprecationWarning, asdouble, WithFloat(FloatSubclass(4.25))) self.assertRaises(TypeError, asdouble, object()) self.assertRaises(TypeError, asdouble, NULL) From bfd4e36fef403bbea2352a477c3e16e601db4fd3 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 3 Jul 2024 10:42:17 +0300 Subject: [PATCH 10/11] + convert Lib/test/test_capi/test_getargs.py --- Lib/test/test_capi/test_getargs.py | 220 +++++++++++------------------ 1 file changed, 83 insertions(+), 137 deletions(-) diff --git a/Lib/test/test_capi/test_getargs.py b/Lib/test/test_capi/test_getargs.py index 232aa2a80025dc..3e1e90256efeac 100644 --- a/Lib/test/test_capi/test_getargs.py +++ b/Lib/test/test_capi/test_getargs.py @@ -6,6 +6,8 @@ from test.support import import_helper from test.support import script_helper from test.support import warnings_helper +from test.support.classes import (ComplexSubclass, FloatSubclass, WithComplex, + WithFloat, WithIndex, WithInt) # Skip this test if the _testcapi module isn't available. _testcapi = import_helper.import_module('_testcapi') from _testcapi import getargs_keywords, getargs_keyword_only @@ -63,91 +65,35 @@ NULL = None -class Index: - def __index__(self): - return 99 - class IndexIntSubclass(int): def __index__(self): return 99 -class BadIndex: - def __index__(self): - return 1.0 - -class BadIndex2: - def __index__(self): - return True - class BadIndex3(int): def __index__(self): return True -class Int: - def __int__(self): - return 99 - class IntSubclass(int): def __int__(self): return 99 -class BadInt: - def __int__(self): - return 1.0 - -class BadInt2: - def __int__(self): - return True - class BadInt3(int): def __int__(self): return True - -class Float: - def __float__(self): - return 4.25 - -class FloatSubclass(float): - pass - class FloatSubclass2(float): def __float__(self): return 4.25 -class BadFloat: - def __float__(self): - return 687 - -class BadFloat2: - def __float__(self): - return FloatSubclass(4.25) - class BadFloat3(float): def __float__(self): return FloatSubclass(4.25) - -class Complex: - def __complex__(self): - return 4.25+0.5j - -class ComplexSubclass(complex): - pass - class ComplexSubclass2(complex): def __complex__(self): return 4.25+0.5j -class BadComplex: - def __complex__(self): - return 1.25 - -class BadComplex2: - def __complex__(self): - return ComplexSubclass(4.25+0.5j) - class BadComplex3(complex): def __complex__(self): return ComplexSubclass(4.25+0.5j) @@ -167,16 +113,16 @@ def test_b(self): from _testcapi import getargs_b # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) self.assertRaises(TypeError, getargs_b, 3.14) - self.assertEqual(99, getargs_b(Index())) + self.assertEqual(99, getargs_b(WithIndex(99))) self.assertEqual(0, getargs_b(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_b, BadIndex()) + self.assertRaises(TypeError, getargs_b, WithIndex(1.0)) with self.assertWarns(DeprecationWarning): - self.assertEqual(1, getargs_b(BadIndex2())) + self.assertEqual(1, getargs_b(WithIndex(True))) self.assertEqual(0, getargs_b(BadIndex3())) - self.assertRaises(TypeError, getargs_b, Int()) + self.assertRaises(TypeError, getargs_b, WithInt(99)) self.assertEqual(0, getargs_b(IntSubclass())) - self.assertRaises(TypeError, getargs_b, BadInt()) - self.assertRaises(TypeError, getargs_b, BadInt2()) + self.assertRaises(TypeError, getargs_b, WithInt(1.0)) + self.assertRaises(TypeError, getargs_b, WithInt(True)) self.assertEqual(0, getargs_b(BadInt3())) self.assertRaises(OverflowError, getargs_b, -1) @@ -191,16 +137,16 @@ def test_B(self): from _testcapi import getargs_B # B returns 'unsigned char', no range checking self.assertRaises(TypeError, getargs_B, 3.14) - self.assertEqual(99, getargs_B(Index())) + self.assertEqual(99, getargs_B(WithIndex(99))) self.assertEqual(0, getargs_B(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_B, BadIndex()) + self.assertRaises(TypeError, getargs_B, WithIndex(1.0)) with self.assertWarns(DeprecationWarning): - self.assertEqual(1, getargs_B(BadIndex2())) + self.assertEqual(1, getargs_B(WithIndex(True))) self.assertEqual(0, getargs_B(BadIndex3())) - self.assertRaises(TypeError, getargs_B, Int()) + self.assertRaises(TypeError, getargs_B, WithInt(99)) self.assertEqual(0, getargs_B(IntSubclass())) - self.assertRaises(TypeError, getargs_B, BadInt()) - self.assertRaises(TypeError, getargs_B, BadInt2()) + self.assertRaises(TypeError, getargs_B, WithInt(1.0)) + self.assertRaises(TypeError, getargs_B, WithInt(True)) self.assertEqual(0, getargs_B(BadInt3())) self.assertEqual(UCHAR_MAX, getargs_B(-1)) @@ -215,16 +161,16 @@ def test_H(self): from _testcapi import getargs_H # H returns 'unsigned short', no range checking self.assertRaises(TypeError, getargs_H, 3.14) - self.assertEqual(99, getargs_H(Index())) + self.assertEqual(99, getargs_H(WithIndex(99))) self.assertEqual(0, getargs_H(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_H, BadIndex()) + self.assertRaises(TypeError, getargs_H, WithIndex(1.0)) with self.assertWarns(DeprecationWarning): - self.assertEqual(1, getargs_H(BadIndex2())) + self.assertEqual(1, getargs_H(WithIndex(True))) self.assertEqual(0, getargs_H(BadIndex3())) - self.assertRaises(TypeError, getargs_H, Int()) + self.assertRaises(TypeError, getargs_H, WithInt(99)) self.assertEqual(0, getargs_H(IntSubclass())) - self.assertRaises(TypeError, getargs_H, BadInt()) - self.assertRaises(TypeError, getargs_H, BadInt2()) + self.assertRaises(TypeError, getargs_H, WithInt(1.0)) + self.assertRaises(TypeError, getargs_H, WithInt(True)) self.assertEqual(0, getargs_H(BadInt3())) self.assertEqual(USHRT_MAX, getargs_H(-1)) @@ -240,16 +186,16 @@ def test_I(self): from _testcapi import getargs_I # I returns 'unsigned int', no range checking self.assertRaises(TypeError, getargs_I, 3.14) - self.assertEqual(99, getargs_I(Index())) + self.assertEqual(99, getargs_I(WithIndex(99))) self.assertEqual(0, getargs_I(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_I, BadIndex()) + self.assertRaises(TypeError, getargs_I, WithIndex(1.0)) with self.assertWarns(DeprecationWarning): - self.assertEqual(1, getargs_I(BadIndex2())) + self.assertEqual(1, getargs_I(WithIndex(True))) self.assertEqual(0, getargs_I(BadIndex3())) - self.assertRaises(TypeError, getargs_I, Int()) + self.assertRaises(TypeError, getargs_I, WithInt(99)) self.assertEqual(0, getargs_I(IntSubclass())) - self.assertRaises(TypeError, getargs_I, BadInt()) - self.assertRaises(TypeError, getargs_I, BadInt2()) + self.assertRaises(TypeError, getargs_I, WithInt(1.0)) + self.assertRaises(TypeError, getargs_I, WithInt(True)) self.assertEqual(0, getargs_I(BadInt3())) self.assertEqual(UINT_MAX, getargs_I(-1)) @@ -266,15 +212,15 @@ def test_k(self): # k returns 'unsigned long', no range checking # it does not accept float, or instances with __int__ self.assertRaises(TypeError, getargs_k, 3.14) - self.assertRaises(TypeError, getargs_k, Index()) + self.assertRaises(TypeError, getargs_k, WithIndex(99)) self.assertEqual(0, getargs_k(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_k, BadIndex()) - self.assertRaises(TypeError, getargs_k, BadIndex2()) + self.assertRaises(TypeError, getargs_k, WithIndex(1.0)) + self.assertRaises(TypeError, getargs_k, WithIndex(True)) self.assertEqual(0, getargs_k(BadIndex3())) - self.assertRaises(TypeError, getargs_k, Int()) + self.assertRaises(TypeError, getargs_k, WithInt(99)) self.assertEqual(0, getargs_k(IntSubclass())) - self.assertRaises(TypeError, getargs_k, BadInt()) - self.assertRaises(TypeError, getargs_k, BadInt2()) + self.assertRaises(TypeError, getargs_k, WithInt(1.0)) + self.assertRaises(TypeError, getargs_k, WithInt(True)) self.assertEqual(0, getargs_k(BadInt3())) self.assertEqual(ULONG_MAX, getargs_k(-1)) @@ -291,16 +237,16 @@ def test_h(self): from _testcapi import getargs_h # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) self.assertRaises(TypeError, getargs_h, 3.14) - self.assertEqual(99, getargs_h(Index())) + self.assertEqual(99, getargs_h(WithIndex(99))) self.assertEqual(0, getargs_h(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_h, BadIndex()) + self.assertRaises(TypeError, getargs_h, WithIndex(1.0)) with self.assertWarns(DeprecationWarning): - self.assertEqual(1, getargs_h(BadIndex2())) + self.assertEqual(1, getargs_h(WithIndex(True))) self.assertEqual(0, getargs_h(BadIndex3())) - self.assertRaises(TypeError, getargs_h, Int()) + self.assertRaises(TypeError, getargs_h, WithInt(99)) self.assertEqual(0, getargs_h(IntSubclass())) - self.assertRaises(TypeError, getargs_h, BadInt()) - self.assertRaises(TypeError, getargs_h, BadInt2()) + self.assertRaises(TypeError, getargs_h, WithInt(1.0)) + self.assertRaises(TypeError, getargs_h, WithInt(True)) self.assertEqual(0, getargs_h(BadInt3())) self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1) @@ -315,16 +261,16 @@ def test_i(self): from _testcapi import getargs_i # i returns 'int', and does range checking (INT_MIN ... INT_MAX) self.assertRaises(TypeError, getargs_i, 3.14) - self.assertEqual(99, getargs_i(Index())) + self.assertEqual(99, getargs_i(WithIndex(99))) self.assertEqual(0, getargs_i(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_i, BadIndex()) + self.assertRaises(TypeError, getargs_i, WithIndex(1.0)) with self.assertWarns(DeprecationWarning): - self.assertEqual(1, getargs_i(BadIndex2())) + self.assertEqual(1, getargs_i(WithIndex(True))) self.assertEqual(0, getargs_i(BadIndex3())) - self.assertRaises(TypeError, getargs_i, Int()) + self.assertRaises(TypeError, getargs_i, WithInt(99)) self.assertEqual(0, getargs_i(IntSubclass())) - self.assertRaises(TypeError, getargs_i, BadInt()) - self.assertRaises(TypeError, getargs_i, BadInt2()) + self.assertRaises(TypeError, getargs_i, WithInt(1.0)) + self.assertRaises(TypeError, getargs_i, WithInt(True)) self.assertEqual(0, getargs_i(BadInt3())) self.assertRaises(OverflowError, getargs_i, INT_MIN-1) @@ -339,16 +285,16 @@ def test_l(self): from _testcapi import getargs_l # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) self.assertRaises(TypeError, getargs_l, 3.14) - self.assertEqual(99, getargs_l(Index())) + self.assertEqual(99, getargs_l(WithIndex(99))) self.assertEqual(0, getargs_l(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_l, BadIndex()) + self.assertRaises(TypeError, getargs_l, WithIndex(1.0)) with self.assertWarns(DeprecationWarning): - self.assertEqual(1, getargs_l(BadIndex2())) + self.assertEqual(1, getargs_l(WithIndex(True))) self.assertEqual(0, getargs_l(BadIndex3())) - self.assertRaises(TypeError, getargs_l, Int()) + self.assertRaises(TypeError, getargs_l, WithInt(99)) self.assertEqual(0, getargs_l(IntSubclass())) - self.assertRaises(TypeError, getargs_l, BadInt()) - self.assertRaises(TypeError, getargs_l, BadInt2()) + self.assertRaises(TypeError, getargs_l, WithInt(1.0)) + self.assertRaises(TypeError, getargs_l, WithInt(True)) self.assertEqual(0, getargs_l(BadInt3())) self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) @@ -364,16 +310,16 @@ def test_n(self): # n returns 'Py_ssize_t', and does range checking # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX) self.assertRaises(TypeError, getargs_n, 3.14) - self.assertEqual(99, getargs_n(Index())) + self.assertEqual(99, getargs_n(WithIndex(99))) self.assertEqual(0, getargs_n(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_n, BadIndex()) + self.assertRaises(TypeError, getargs_n, WithIndex(1.0)) with self.assertWarns(DeprecationWarning): - self.assertEqual(1, getargs_n(BadIndex2())) + self.assertEqual(1, getargs_n(WithIndex(True))) self.assertEqual(0, getargs_n(BadIndex3())) - self.assertRaises(TypeError, getargs_n, Int()) + self.assertRaises(TypeError, getargs_n, WithInt(99)) self.assertEqual(0, getargs_n(IntSubclass())) - self.assertRaises(TypeError, getargs_n, BadInt()) - self.assertRaises(TypeError, getargs_n, BadInt2()) + self.assertRaises(TypeError, getargs_n, WithInt(1.0)) + self.assertRaises(TypeError, getargs_n, WithInt(True)) self.assertEqual(0, getargs_n(BadInt3())) self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1) @@ -392,16 +338,16 @@ def test_L(self): # ... LLONG_MAX) self.assertRaises(TypeError, getargs_L, 3.14) self.assertRaises(TypeError, getargs_L, "Hello") - self.assertEqual(99, getargs_L(Index())) + self.assertEqual(99, getargs_L(WithIndex(99))) self.assertEqual(0, getargs_L(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_L, BadIndex()) + self.assertRaises(TypeError, getargs_L, WithIndex(1.0)) with self.assertWarns(DeprecationWarning): - self.assertEqual(1, getargs_L(BadIndex2())) + self.assertEqual(1, getargs_L(WithIndex(True))) self.assertEqual(0, getargs_L(BadIndex3())) - self.assertRaises(TypeError, getargs_L, Int()) + self.assertRaises(TypeError, getargs_L, WithInt(99)) self.assertEqual(0, getargs_L(IntSubclass())) - self.assertRaises(TypeError, getargs_L, BadInt()) - self.assertRaises(TypeError, getargs_L, BadInt2()) + self.assertRaises(TypeError, getargs_L, WithInt(1.0)) + self.assertRaises(TypeError, getargs_L, WithInt(True)) self.assertEqual(0, getargs_L(BadInt3())) self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) @@ -416,15 +362,15 @@ def test_K(self): from _testcapi import getargs_K # K return 'unsigned long long', no range checking self.assertRaises(TypeError, getargs_K, 3.14) - self.assertRaises(TypeError, getargs_K, Index()) + self.assertRaises(TypeError, getargs_K, WithIndex(99)) self.assertEqual(0, getargs_K(IndexIntSubclass())) - self.assertRaises(TypeError, getargs_K, BadIndex()) - self.assertRaises(TypeError, getargs_K, BadIndex2()) + self.assertRaises(TypeError, getargs_K, WithIndex(1.0)) + self.assertRaises(TypeError, getargs_K, WithIndex(True)) self.assertEqual(0, getargs_K(BadIndex3())) - self.assertRaises(TypeError, getargs_K, Int()) + self.assertRaises(TypeError, getargs_K, WithInt(99)) self.assertEqual(0, getargs_K(IntSubclass())) - self.assertRaises(TypeError, getargs_K, BadInt()) - self.assertRaises(TypeError, getargs_K, BadInt2()) + self.assertRaises(TypeError, getargs_K, WithInt(1.0)) + self.assertRaises(TypeError, getargs_K, WithInt(True)) self.assertEqual(0, getargs_K(BadInt3())) self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) @@ -446,15 +392,15 @@ def test_f(self): self.assertEqual(getargs_f(4.25), 4.25) self.assertEqual(getargs_f(4), 4.0) self.assertRaises(TypeError, getargs_f, 4.25+0j) - self.assertEqual(getargs_f(Float()), 4.25) + self.assertEqual(getargs_f(WithFloat(4.25)), 4.25) self.assertEqual(getargs_f(FloatSubclass(7.5)), 7.5) self.assertEqual(getargs_f(FloatSubclass2(7.5)), 7.5) - self.assertRaises(TypeError, getargs_f, BadFloat()) + self.assertRaises(TypeError, getargs_f, WithFloat(687)) with self.assertWarns(DeprecationWarning): - self.assertEqual(getargs_f(BadFloat2()), 4.25) + self.assertEqual(getargs_f(WithFloat(FloatSubclass(4.25))), 4.25) self.assertEqual(getargs_f(BadFloat3(7.5)), 7.5) - self.assertEqual(getargs_f(Index()), 99.0) - self.assertRaises(TypeError, getargs_f, Int()) + self.assertEqual(getargs_f(WithIndex(99)), 99.0) + self.assertRaises(TypeError, getargs_f, WithInt(99)) for x in (FLT_MIN, -FLT_MIN, FLT_MAX, -FLT_MAX, INF, -INF): self.assertEqual(getargs_f(x), x) @@ -480,15 +426,15 @@ def test_d(self): self.assertEqual(getargs_d(4.25), 4.25) self.assertEqual(getargs_d(4), 4.0) self.assertRaises(TypeError, getargs_d, 4.25+0j) - self.assertEqual(getargs_d(Float()), 4.25) + self.assertEqual(getargs_d(WithFloat(4.25)), 4.25) self.assertEqual(getargs_d(FloatSubclass(7.5)), 7.5) self.assertEqual(getargs_d(FloatSubclass2(7.5)), 7.5) - self.assertRaises(TypeError, getargs_d, BadFloat()) + self.assertRaises(TypeError, getargs_d, WithFloat(687)) with self.assertWarns(DeprecationWarning): - self.assertEqual(getargs_d(BadFloat2()), 4.25) + self.assertEqual(getargs_d(WithFloat(FloatSubclass(4.25))), 4.25) self.assertEqual(getargs_d(BadFloat3(7.5)), 7.5) - self.assertEqual(getargs_d(Index()), 99.0) - self.assertRaises(TypeError, getargs_d, Int()) + self.assertEqual(getargs_d(WithIndex(99)), 99.0) + self.assertRaises(TypeError, getargs_d, WithInt(99)) for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): self.assertEqual(getargs_d(x), x) @@ -504,15 +450,15 @@ def test_D(self): self.assertEqual(getargs_D(4.25+0.5j), 4.25+0.5j) self.assertEqual(getargs_D(4.25), 4.25+0j) self.assertEqual(getargs_D(4), 4.0+0j) - self.assertEqual(getargs_D(Complex()), 4.25+0.5j) + self.assertEqual(getargs_D(WithComplex(4.25+0.5j)), 4.25+0.5j) self.assertEqual(getargs_D(ComplexSubclass(7.5+0.25j)), 7.5+0.25j) self.assertEqual(getargs_D(ComplexSubclass2(7.5+0.25j)), 7.5+0.25j) - self.assertRaises(TypeError, getargs_D, BadComplex()) + self.assertRaises(TypeError, getargs_D, WithComplex(1.25)) with self.assertWarns(DeprecationWarning): - self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j) + self.assertEqual(getargs_D(WithComplex(ComplexSubclass(4.25+0.5j))), 4.25+0.5j) self.assertEqual(getargs_D(BadComplex3(7.5+0.25j)), 7.5+0.25j) - self.assertEqual(getargs_D(Index()), 99.0+0j) - self.assertRaises(TypeError, getargs_D, Int()) + self.assertEqual(getargs_D(WithIndex(99)), 99.0+0j) + self.assertRaises(TypeError, getargs_D, WithInt(99)) for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): c = complex(x, 1.0) From ed6a546ae4bf94a85c895003044e7e28b02a8d9b Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 17 Apr 2025 13:37:22 +0300 Subject: [PATCH 11/11] address review: classes.py -> number_helper.py --- Lib/test/support/{classes.py => number_helper.py} | 2 +- Lib/test/test_capi/test_complex.py | 4 ++-- Lib/test/test_capi/test_float.py | 2 +- Lib/test/test_capi/test_getargs.py | 5 +++-- Lib/test/test_capi/test_long.py | 2 +- Lib/test/test_complex.py | 6 +++--- Lib/test/test_float.py | 5 +++-- 7 files changed, 14 insertions(+), 12 deletions(-) rename Lib/test/support/{classes.py => number_helper.py} (96%) diff --git a/Lib/test/support/classes.py b/Lib/test/support/number_helper.py similarity index 96% rename from Lib/test/support/classes.py rename to Lib/test/support/number_helper.py index 52cc112956ce3b..a39da4482f9c28 100644 --- a/Lib/test/support/classes.py +++ b/Lib/test/support/number_helper.py @@ -1,4 +1,4 @@ -# Common test classes. +# Common test classes for numeric types. class WithIndex: def __init__(self, value): diff --git a/Lib/test/test_capi/test_complex.py b/Lib/test/test_capi/test_complex.py index f267ce48e5d40c..efbb293759e145 100644 --- a/Lib/test/test_capi/test_complex.py +++ b/Lib/test/test_capi/test_complex.py @@ -4,8 +4,8 @@ import warnings from test.support import import_helper -from test.support.classes import (ComplexSubclass, FloatSubclass, - WithFloat, WithComplex) +from test.support.number_helper import (ComplexSubclass, FloatSubclass, + WithFloat, WithComplex) from test.support.testcase import ComplexesAreIdenticalMixin diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index 684d825c55805b..7cd8b8f36547be 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -4,7 +4,7 @@ import warnings from test.support import import_helper -from test.support.classes import FloatSubclass, WithIndex, WithFloat +from test.support.number_helper import FloatSubclass, WithIndex, WithFloat _testcapi = import_helper.import_module('_testcapi') _testlimitedcapi = import_helper.import_module('_testlimitedcapi') diff --git a/Lib/test/test_capi/test_getargs.py b/Lib/test/test_capi/test_getargs.py index f1401f8a9702fb..eb5032ae3817c1 100644 --- a/Lib/test/test_capi/test_getargs.py +++ b/Lib/test/test_capi/test_getargs.py @@ -5,8 +5,9 @@ from test.support import import_helper from test.support import script_helper from test.support import warnings_helper -from test.support.classes import (ComplexSubclass, FloatSubclass, WithComplex, - WithFloat, WithIndex, WithInt) +from test.support.number_helper import (ComplexSubclass, FloatSubclass, + WithComplex, WithFloat, WithIndex, + WithInt) from test.support.testcase import FloatsAreIdenticalMixin # Skip this test if the _testcapi module isn't available. _testcapi = import_helper.import_module('_testcapi') diff --git a/Lib/test/test_capi/test_long.py b/Lib/test/test_capi/test_long.py index 48687ec3b02ea7..359207dc053c8e 100644 --- a/Lib/test/test_capi/test_long.py +++ b/Lib/test/test_capi/test_long.py @@ -3,7 +3,7 @@ import test.support as support from test.support import import_helper -from test.support.classes import IntSubclass, WithIndex, WithIntAndIndex +from test.support.number_helper import IntSubclass, WithIndex, WithIntAndIndex # Skip this test if the _testcapi and _testlimitedcapi modules isn't available. _testcapi = import_helper.import_module('_testcapi') diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index a41c51a5dee228..7cbe60d41689ef 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -6,9 +6,9 @@ VALID_UNDERSCORE_LITERALS, INVALID_UNDERSCORE_LITERALS, ) -from test.support.classes import (ComplexSubclass, WithComplex, - WithFloat, WithIndex, WithInt, - OtherComplexSubclass) +from test.support.number_helper import (ComplexSubclass, WithComplex, + WithFloat, WithIndex, WithInt, + OtherComplexSubclass) from random import random from math import isnan, copysign diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 69cb0704ea8600..84d035e02b2541 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -13,8 +13,9 @@ VALID_UNDERSCORE_LITERALS, INVALID_UNDERSCORE_LITERALS, ) -from test.support.classes import (FloatSubclass, OtherFloatSubclass, WithFloat, - FloatLikeSubclass, WithIndex, WithInt) +from test.support.number_helper import (FloatSubclass, OtherFloatSubclass, + WithFloat, FloatLikeSubclass, + WithIndex, WithInt) from math import isinf, isnan, copysign, ldexp import math