From 2f2e77f819c9522b848bec41f0073a92582c9a93 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 1 Nov 2023 20:27:08 +0300 Subject: [PATCH 01/12] gh-111495: Add tests for PyFloat C API --- Lib/test/test_capi/test_float.py | 102 +++++++++++++++++++++++++++++++ Modules/_testcapi/float.c | 60 ++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 Lib/test/test_capi/test_float.py diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py new file mode 100644 index 00000000000000..cf3d20e2c62735 --- /dev/null +++ b/Lib/test/test_capi/test_float.py @@ -0,0 +1,102 @@ +import math +import sys +import unittest + +from test.test_capi.test_getargs import (Float, FloatSubclass, FloatSubclass2, + BadIndex2, BadFloat2, Index, BadIndex, + BadFloat) +from test.support import import_helper + +_testcapi = import_helper.import_module('_testcapi') + +NULL = None + + +class CAPIFloatTest(unittest.TestCase): + def assertEqualWithSign(self, actual, expected): + self.assertEqual(actual, expected) + self.assertEqual(math.copysign(1, actual), math.copysign(1, expected)) + + def test_check(self): + # Test PyFloat_Check() + check = _testcapi.float_check + + self.assertTrue(check(3.14)) + self.assertTrue(check(FloatSubclass(3.14))) + self.assertFalse(check(Float())) + self.assertFalse(check(3)) + self.assertFalse(check([])) + self.assertFalse(check(object())) + + # CRASHES check(NULL) + + def test_checkexact(self): + # Test PyFloat_CheckExact() + checkexact = _testcapi.float_checkexact + + self.assertTrue(checkexact(3.14)) + self.assertFalse(checkexact(FloatSubclass(3.14))) + self.assertFalse(checkexact(Float())) + self.assertFalse(checkexact(3)) + self.assertFalse(checkexact([])) + self.assertFalse(checkexact(object())) + + # CRASHES checkexact(NULL) + + def test_fromstring(self): + # Test PyFloat_FromString() + fromstring = _testcapi.float_fromstring + + self.assertEqual(fromstring("3.14"), 3.14) + self.assertEqual(fromstring("10."), 10.) + self.assertEqual(fromstring("-1e10"), -1e10) + self.assertEqual(fromstring("3.14e+42"), 3.14e+42) + self.assertEqual(fromstring("0e0"), 0.0) + self.assertEqual(fromstring("3.14_15_93"), 3.141593) + self.assertEqualWithSign(fromstring("-0.0"), -0.0) + + self.assertRaises(TypeError, fromstring, 3.14) + self.assertRaises(TypeError, fromstring, object()) + self.assertRaises(ValueError, fromstring, "spam") + + # CRASHES fromstring(NULL) + + def test_asdouble(self): + # Test PyFloat_AsDouble() + asdouble = _testcapi.float_asdouble + + class BadFloat3: + def __float__(self): + raise RuntimeError + + self.assertEqual(asdouble(3.14), 3.14) + self.assertEqual(asdouble(FloatSubclass(3.14)), 3.14) + self.assertEqual(asdouble(FloatSubclass2(3.14)), 3.14) + self.assertEqual(asdouble(Index()), 99.) + + self.assertRaises(TypeError, asdouble, BadIndex()) + self.assertRaises(TypeError, asdouble, BadFloat()) + self.assertRaises(RuntimeError, asdouble, BadFloat3()) + with self.assertWarns(DeprecationWarning): + self.assertEqual(asdouble(BadIndex2()), 1.) + with self.assertWarns(DeprecationWarning): + self.assertEqual(asdouble(BadFloat2()), 4.25) + self.assertRaises(TypeError, asdouble, NULL) + + def test_getinfo(self): + # Test PyFloat_GetInfo() + getinfo = _testcapi.float_getinfo + + self.assertEqual(getinfo(), sys.float_info) + + def test_getmax(self): + # Test PyFloat_GetMax() + getmax = _testcapi.float_getmax + + self.assertEqual(getmax(), sys.float_info.max) + + def test_getmin(self): + # Test PyFloat_GetMax() + getmin = _testcapi.float_getmin + + self.assertEqual(getmin(), sys.float_info.min) diff --git a/Modules/_testcapi/float.c b/Modules/_testcapi/float.c index 2a7d9799ae8bd1..251bba0ae30281 100644 --- a/Modules/_testcapi/float.c +++ b/Modules/_testcapi/float.c @@ -2,9 +2,62 @@ #define PYTESTCAPI_NEED_INTERNAL_API #include "parts.h" +#include "util.h" #include "clinic/float.c.h" +static PyObject * +float_check(PyObject *Py_UNUSED(module), PyObject *obj) +{ + NULLABLE(obj); + return PyLong_FromLong(PyFloat_Check(obj)); +} + +static PyObject * +float_checkexact(PyObject *Py_UNUSED(module), PyObject *obj) +{ + NULLABLE(obj); + return PyLong_FromLong(PyFloat_CheckExact(obj)); +} + +static PyObject * +float_fromstring(PyObject *Py_UNUSED(module), PyObject *obj) +{ + NULLABLE(obj); + return PyFloat_FromString(obj); +} + +static PyObject * +float_asdouble(PyObject *Py_UNUSED(module), PyObject *obj) +{ + double d; + + d = PyFloat_AsDouble(obj); + if (d == -1. && PyErr_Occurred()) { + return NULL; + } + + return PyFloat_FromDouble(d); +} + +static PyObject* +float_getinfo(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(arg)) +{ + return PyFloat_GetInfo(); +} + +static PyObject * +float_getmax(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(arg)) +{ + return PyFloat_FromDouble(PyFloat_GetMax()); +} + +static PyObject * +float_getmin(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(arg)) +{ + return PyFloat_FromDouble(PyFloat_GetMin()); +} + /*[clinic input] module _testcapi [clinic start generated code]*/ @@ -99,6 +152,13 @@ _testcapi_float_unpack_impl(PyObject *module, const char *data, } static PyMethodDef test_methods[] = { + {"float_check", float_check, METH_O}, + {"float_checkexact", float_checkexact, METH_O}, + {"float_fromstring", float_fromstring, METH_O}, + {"float_asdouble", float_asdouble, METH_O}, + {"float_getinfo", float_getinfo, METH_NOARGS}, + {"float_getmax", float_getmax, METH_NOARGS}, + {"float_getmin", float_getmin, METH_NOARGS}, _TESTCAPI_FLOAT_PACK_METHODDEF _TESTCAPI_FLOAT_UNPACK_METHODDEF {NULL}, From 1d12623db9e7b9401ddfca7c84663de748d27229 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 2 Nov 2023 08:50:09 +0300 Subject: [PATCH 02/12] Add missing coverage tests --- Lib/test/test_capi/test_float.py | 9 +++++++++ Modules/_testcapi/float.c | 1 + 2 files changed, 10 insertions(+) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index cf3d20e2c62735..898a2daaae10ff 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -1,6 +1,8 @@ +import array import math import sys import unittest +import warnings from test.test_capi.test_getargs import (Float, FloatSubclass, FloatSubclass2, BadIndex2, BadFloat2, Index, BadIndex, @@ -48,6 +50,9 @@ def test_fromstring(self): fromstring = _testcapi.float_fromstring self.assertEqual(fromstring("3.14"), 3.14) + self.assertEqual(fromstring(b"3.14"), 3.14) + self.assertEqual(fromstring(bytearray(b"3.14")), 3.14) + self.assertEqual(fromstring(array.array('b', b'3.14')), 3.14) self.assertEqual(fromstring("10."), 10.) self.assertEqual(fromstring("-1e10"), -1e10) self.assertEqual(fromstring("3.14e+42"), 3.14e+42) @@ -81,6 +86,10 @@ def __float__(self): self.assertEqual(asdouble(BadIndex2()), 1.) with self.assertWarns(DeprecationWarning): self.assertEqual(asdouble(BadFloat2()), 4.25) + with warnings.catch_warnings(): + warnings.simplefilter("error", DeprecationWarning) + self.assertRaises(DeprecationWarning, asdouble, BadFloat2()) + self.assertRaises(TypeError, asdouble, object()) self.assertRaises(TypeError, asdouble, NULL) def test_getinfo(self): diff --git a/Modules/_testcapi/float.c b/Modules/_testcapi/float.c index 251bba0ae30281..58bcca52af9392 100644 --- a/Modules/_testcapi/float.c +++ b/Modules/_testcapi/float.c @@ -32,6 +32,7 @@ float_asdouble(PyObject *Py_UNUSED(module), PyObject *obj) { double d; + NULLABLE(obj); d = PyFloat_AsDouble(obj); if (d == -1. && PyErr_Occurred()) { return NULL; From 3e9c05ba98a9136b8e5e267d513cdf6d07b78802 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 2 Nov 2023 17:18:07 +0300 Subject: [PATCH 03/12] remove redundant tests for PyFloat_FromString() --- Lib/test/test_capi/test_float.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index 898a2daaae10ff..ac435678075af5 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -53,16 +53,7 @@ def test_fromstring(self): self.assertEqual(fromstring(b"3.14"), 3.14) self.assertEqual(fromstring(bytearray(b"3.14")), 3.14) self.assertEqual(fromstring(array.array('b', b'3.14')), 3.14) - self.assertEqual(fromstring("10."), 10.) - self.assertEqual(fromstring("-1e10"), -1e10) - self.assertEqual(fromstring("3.14e+42"), 3.14e+42) - self.assertEqual(fromstring("0e0"), 0.0) - self.assertEqual(fromstring("3.14_15_93"), 3.141593) - self.assertEqualWithSign(fromstring("-0.0"), -0.0) - self.assertRaises(TypeError, fromstring, 3.14) - self.assertRaises(TypeError, fromstring, object()) - self.assertRaises(ValueError, fromstring, "spam") # CRASHES fromstring(NULL) From 04718df1bc778bf2c30c86f9db1e8779d45fcc8c Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 3 Nov 2023 12:45:06 +0300 Subject: [PATCH 04/12] Update Lib/test/test_capi/test_float.py --- Lib/test/test_capi/test_float.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index ac435678075af5..e8d6e6be21045c 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -66,6 +66,7 @@ def __float__(self): raise RuntimeError self.assertEqual(asdouble(3.14), 3.14) + self.assertEqual(asdouble(-1), -1.0) self.assertEqual(asdouble(FloatSubclass(3.14)), 3.14) self.assertEqual(asdouble(FloatSubclass2(3.14)), 3.14) self.assertEqual(asdouble(Index()), 99.) From 71e9cd0a1350205fab08c713391ce6a66da8d161 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 4 Nov 2023 18:04:47 +0300 Subject: [PATCH 05/12] Address review (partially) --- Lib/test/test_capi/test_float.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index e8d6e6be21045c..5a07f9c821bfdd 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -51,6 +51,8 @@ def test_fromstring(self): self.assertEqual(fromstring("3.14"), 3.14) self.assertEqual(fromstring(b"3.14"), 3.14) + self.assertRaises(ValueError, fromstring, "3.14\0") + self.assertRaises(ValueError, fromstring, b"3.14\0") self.assertEqual(fromstring(bytearray(b"3.14")), 3.14) self.assertEqual(fromstring(array.array('b', b'3.14')), 3.14) self.assertRaises(TypeError, fromstring, 3.14) @@ -67,6 +69,8 @@ def __float__(self): self.assertEqual(asdouble(3.14), 3.14) self.assertEqual(asdouble(-1), -1.0) + self.assertEqual(asdouble(42), 42.0) + self.assertEqual(asdouble(2**1000), float(2**1000)) self.assertEqual(asdouble(FloatSubclass(3.14)), 3.14) self.assertEqual(asdouble(FloatSubclass2(3.14)), 3.14) self.assertEqual(asdouble(Index()), 99.) From 4a00f1d55965436c8b807cad4aed63284f1aa962 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 4 Nov 2023 18:48:19 +0300 Subject: [PATCH 06/12] Use PyArg_Parse() in float_asdouble() --- Modules/_testcapi/float.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Modules/_testcapi/float.c b/Modules/_testcapi/float.c index 58bcca52af9392..b2015c22a6ef11 100644 --- a/Modules/_testcapi/float.c +++ b/Modules/_testcapi/float.c @@ -32,9 +32,7 @@ float_asdouble(PyObject *Py_UNUSED(module), PyObject *obj) { double d; - NULLABLE(obj); - d = PyFloat_AsDouble(obj); - if (d == -1. && PyErr_Occurred()) { + if (!PyArg_Parse(obj, "d", &d)) { return NULL; } From 918b56ea0c83252d0cc4b4aebd3c58e60c7fb86d Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 5 Nov 2023 04:57:59 +0300 Subject: [PATCH 07/12] Add float_asdouble() --- Lib/test/test_capi/test_float.py | 6 ++++++ Modules/_testcapi/float.c | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index 5a07f9c821bfdd..4d6fec5c561181 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -59,6 +59,12 @@ def test_fromstring(self): # CRASHES fromstring(NULL) + def test_fromdouble(self): + # Test PyFloat_FromDouble() + fromdouble = _testcapi.float_fromdouble + + self.assertEqual(fromdouble(4.25), 4.25) + def test_asdouble(self): # Test PyFloat_AsDouble() asdouble = _testcapi.float_asdouble diff --git a/Modules/_testcapi/float.c b/Modules/_testcapi/float.c index b2015c22a6ef11..6fe346578c36fb 100644 --- a/Modules/_testcapi/float.c +++ b/Modules/_testcapi/float.c @@ -28,7 +28,7 @@ float_fromstring(PyObject *Py_UNUSED(module), PyObject *obj) } static PyObject * -float_asdouble(PyObject *Py_UNUSED(module), PyObject *obj) +float_fromdouble(PyObject *Py_UNUSED(module), PyObject *obj) { double d; @@ -39,6 +39,20 @@ float_asdouble(PyObject *Py_UNUSED(module), PyObject *obj) return PyFloat_FromDouble(d); } +static PyObject * +float_asdouble(PyObject *Py_UNUSED(module), PyObject *obj) +{ + double d; + + NULLABLE(obj); + d = PyFloat_AsDouble(obj); + if (d == -1. && PyErr_Occurred()) { + return NULL; + } + + return PyFloat_FromDouble(d); +} + static PyObject* float_getinfo(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(arg)) { @@ -154,6 +168,7 @@ static PyMethodDef test_methods[] = { {"float_check", float_check, METH_O}, {"float_checkexact", float_checkexact, METH_O}, {"float_fromstring", float_fromstring, METH_O}, + {"float_fromdouble", float_fromdouble, METH_O}, {"float_asdouble", float_asdouble, METH_O}, {"float_getinfo", float_getinfo, METH_NOARGS}, {"float_getmax", float_getmax, METH_NOARGS}, From 44ff4b27b3fee703374b5ca27bbaaf29015a3e9a Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 5 Nov 2023 04:59:07 +0300 Subject: [PATCH 08/12] s/3.14/4.25/g --- Lib/test/test_capi/test_float.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index 4d6fec5c561181..5e3e49d9acf244 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -23,8 +23,8 @@ def test_check(self): # Test PyFloat_Check() check = _testcapi.float_check - self.assertTrue(check(3.14)) - self.assertTrue(check(FloatSubclass(3.14))) + self.assertTrue(check(4.25)) + self.assertTrue(check(FloatSubclass(4.25))) self.assertFalse(check(Float())) self.assertFalse(check(3)) self.assertFalse(check([])) @@ -36,8 +36,8 @@ def test_checkexact(self): # Test PyFloat_CheckExact() checkexact = _testcapi.float_checkexact - self.assertTrue(checkexact(3.14)) - self.assertFalse(checkexact(FloatSubclass(3.14))) + self.assertTrue(checkexact(4.25)) + self.assertFalse(checkexact(FloatSubclass(4.25))) self.assertFalse(checkexact(Float())) self.assertFalse(checkexact(3)) self.assertFalse(checkexact([])) @@ -49,13 +49,13 @@ def test_fromstring(self): # Test PyFloat_FromString() fromstring = _testcapi.float_fromstring - self.assertEqual(fromstring("3.14"), 3.14) - self.assertEqual(fromstring(b"3.14"), 3.14) - self.assertRaises(ValueError, fromstring, "3.14\0") - self.assertRaises(ValueError, fromstring, b"3.14\0") - self.assertEqual(fromstring(bytearray(b"3.14")), 3.14) - self.assertEqual(fromstring(array.array('b', b'3.14')), 3.14) - self.assertRaises(TypeError, fromstring, 3.14) + self.assertEqual(fromstring("4.25"), 4.25) + self.assertEqual(fromstring(b"4.25"), 4.25) + self.assertRaises(ValueError, fromstring, "4.25\0") + self.assertRaises(ValueError, fromstring, b"4.25\0") + self.assertEqual(fromstring(bytearray(b"4.25")), 4.25) + self.assertEqual(fromstring(array.array('b', b'4.25')), 4.25) + self.assertRaises(TypeError, fromstring, 4.25) # CRASHES fromstring(NULL) @@ -73,12 +73,12 @@ class BadFloat3: def __float__(self): raise RuntimeError - self.assertEqual(asdouble(3.14), 3.14) + self.assertEqual(asdouble(4.25), 4.25) self.assertEqual(asdouble(-1), -1.0) self.assertEqual(asdouble(42), 42.0) self.assertEqual(asdouble(2**1000), float(2**1000)) - self.assertEqual(asdouble(FloatSubclass(3.14)), 3.14) - self.assertEqual(asdouble(FloatSubclass2(3.14)), 3.14) + self.assertEqual(asdouble(FloatSubclass(4.25)), 4.25) + self.assertEqual(asdouble(FloatSubclass2(4.25)), 4.25) self.assertEqual(asdouble(Index()), 99.) self.assertRaises(TypeError, asdouble, BadIndex()) From 14c932eedad5f080b91c704b61d4d2bb07d8b99c Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 5 Nov 2023 05:33:15 +0300 Subject: [PATCH 09/12] memoryview tests --- Lib/test/test_capi/test_float.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index 5e3e49d9acf244..253af677495f33 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -1,4 +1,3 @@ -import array import math import sys import unittest @@ -54,7 +53,9 @@ def test_fromstring(self): self.assertRaises(ValueError, fromstring, "4.25\0") self.assertRaises(ValueError, fromstring, b"4.25\0") self.assertEqual(fromstring(bytearray(b"4.25")), 4.25) - self.assertEqual(fromstring(array.array('b', b'4.25')), 4.25) + self.assertEqual(fromstring(memoryview(b"4.25")), 4.25) + self.assertEqual(fromstring(memoryview(b"4.255")[:-1]), 4.25) + self.assertRaises(TypeError, fromstring, memoryview(b"4.25")[::2]) self.assertRaises(TypeError, fromstring, 4.25) # CRASHES fromstring(NULL) From 26f843c77e25c614efb39d475d64ca92c16e8d39 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 5 Nov 2023 05:34:50 +0300 Subject: [PATCH 10/12] Group logical blocks --- Lib/test/test_capi/test_float.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index 253af677495f33..dea41c8a5948c8 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -52,10 +52,13 @@ def test_fromstring(self): self.assertEqual(fromstring(b"4.25"), 4.25) self.assertRaises(ValueError, fromstring, "4.25\0") self.assertRaises(ValueError, fromstring, b"4.25\0") + self.assertEqual(fromstring(bytearray(b"4.25")), 4.25) + self.assertEqual(fromstring(memoryview(b"4.25")), 4.25) self.assertEqual(fromstring(memoryview(b"4.255")[:-1]), 4.25) self.assertRaises(TypeError, fromstring, memoryview(b"4.25")[::2]) + self.assertRaises(TypeError, fromstring, 4.25) # CRASHES fromstring(NULL) @@ -78,6 +81,7 @@ def __float__(self): self.assertEqual(asdouble(-1), -1.0) self.assertEqual(asdouble(42), 42.0) self.assertEqual(asdouble(2**1000), float(2**1000)) + self.assertEqual(asdouble(FloatSubclass(4.25)), 4.25) self.assertEqual(asdouble(FloatSubclass2(4.25)), 4.25) self.assertEqual(asdouble(Index()), 99.) From 54ec7b33d0f5132a318316e53fad41d69129caaa Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 5 Nov 2023 08:02:13 +0300 Subject: [PATCH 11/12] make test module executable --- Lib/test/test_capi/test_float.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index dea41c8a5948c8..79c81203d192c6 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -116,3 +116,7 @@ def test_getmin(self): getmin = _testcapi.float_getmin self.assertEqual(getmin(), sys.float_info.min) + + +if __name__ == "__main__": + unittest.main() From 463ec446a43fabfa359049a8d6ea5d0edad984de Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 5 Nov 2023 08:52:17 +0200 Subject: [PATCH 12/12] Apply suggestions from code review --- Lib/test/test_capi/test_float.py | 9 ++------- Modules/_testcapi/float.c | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_capi/test_float.py b/Lib/test/test_capi/test_float.py index 79c81203d192c6..95635e822a1245 100644 --- a/Lib/test/test_capi/test_float.py +++ b/Lib/test/test_capi/test_float.py @@ -14,10 +14,6 @@ class CAPIFloatTest(unittest.TestCase): - def assertEqualWithSign(self, actual, expected): - self.assertEqual(actual, expected) - self.assertEqual(math.copysign(1, actual), math.copysign(1, expected)) - def test_check(self): # Test PyFloat_Check() check = _testcapi.float_check @@ -26,7 +22,6 @@ def test_check(self): self.assertTrue(check(FloatSubclass(4.25))) self.assertFalse(check(Float())) self.assertFalse(check(3)) - self.assertFalse(check([])) self.assertFalse(check(object())) # CRASHES check(NULL) @@ -39,7 +34,6 @@ def test_checkexact(self): self.assertFalse(checkexact(FloatSubclass(4.25))) self.assertFalse(checkexact(Float())) self.assertFalse(checkexact(3)) - self.assertFalse(checkexact([])) self.assertFalse(checkexact(object())) # CRASHES checkexact(NULL) @@ -78,8 +72,9 @@ def __float__(self): raise RuntimeError self.assertEqual(asdouble(4.25), 4.25) - self.assertEqual(asdouble(-1), -1.0) + self.assertEqual(asdouble(-1.0), -1.0) self.assertEqual(asdouble(42), 42.0) + self.assertEqual(asdouble(-1), -1.0) self.assertEqual(asdouble(2**1000), float(2**1000)) self.assertEqual(asdouble(FloatSubclass(4.25)), 4.25) diff --git a/Modules/_testcapi/float.c b/Modules/_testcapi/float.c index 6fe346578c36fb..4fcbaf3bb2aa1e 100644 --- a/Modules/_testcapi/float.c +++ b/Modules/_testcapi/float.c @@ -53,7 +53,7 @@ float_asdouble(PyObject *Py_UNUSED(module), PyObject *obj) return PyFloat_FromDouble(d); } -static PyObject* +static PyObject * float_getinfo(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(arg)) { return PyFloat_GetInfo();