From ca96502433c468235b9bf66e924a2919fefa87fa Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 4 Sep 2026 04:42:00 +0300 Subject: [PATCH 1/5] gh-156865: don't raise exceptions for overflows in the ctypes module --- Lib/test/test_ctypes/test_numbers.py | 15 +++++++++++++++ ...2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst | 2 ++ Modules/_ctypes/cfield.c | 8 ++++---- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst diff --git a/Lib/test/test_ctypes/test_numbers.py b/Lib/test/test_ctypes/test_numbers.py index b7df08f6079cd3..ca8a55eae3121d 100644 --- a/Lib/test/test_ctypes/test_numbers.py +++ b/Lib/test/test_ctypes/test_numbers.py @@ -240,6 +240,21 @@ def test_float_overflow(self): if (hasattr(t, "__ctype_le__")): self.assertRaises(OverflowError, t.__ctype_le__, big_int) + # gh-156865: be silent in overflows of C types + self.assertEqual(ctypes.c_float(3e300).value, float('inf')) + self.assertEqual(ctypes.c_float.__ctype_le__(3e300).value, float('inf')) + self.assertEqual(ctypes.c_float.__ctype_be__(3e300).value, float('inf')) + self.assertEqual(ctypes.c_float_complex(3e300).value, complex('inf')) + self.assertEqual(ctypes.c_float_complex.__ctype_le__(3e300).value, + complex('inf')) + self.assertEqual(ctypes.c_float_complex.__ctype_be__(3e300).value, + complex('inf')) + self.assertEqual(ctypes.c_float_complex(3e300j).value, complex('infj')) + self.assertEqual(ctypes.c_float_complex.__ctype_le__(3e300j).value, + complex('infj')) + self.assertEqual(ctypes.c_float_complex.__ctype_be__(3e300j).value, + complex('infj')) + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst b/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst new file mode 100644 index 00000000000000..26cdad81424ee7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst @@ -0,0 +1,2 @@ +Don't raise exceptions on overflows in floating-point types of the +:mod:`ctypes`. Patch by Sergey B Kirpichev. diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index 00b075bccff664..193c60a1afee85 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -865,14 +865,14 @@ Zf_set_sw(void *ptr, PyObject *value, Py_ssize_t size) return NULL; } #ifdef WORDS_BIGENDIAN - if (PyFloat_Pack4(c.real, ptr, 1) - || PyFloat_Pack4(c.imag, ptr + sizeof(float), 1)) + if (PyFloat_Pack4((float)c.real, ptr, 1) + || PyFloat_Pack4((float)c.imag, ptr + sizeof(float), 1)) { return NULL; } #else - if (PyFloat_Pack4(c.real, ptr, 0) - || PyFloat_Pack4(c.imag, ptr + sizeof(float), 0)) + if (PyFloat_Pack4((float)c.real, ptr, 0) + || PyFloat_Pack4((float)c.imag, ptr + sizeof(float), 0)) { return NULL; } From bb3fd9ec6fd03734b3f71559ec44a8d261e092ce Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 4 Sep 2026 13:06:29 +0300 Subject: [PATCH 2/5] skip that for poor childrens --- Lib/test/test_ctypes/test_numbers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_ctypes/test_numbers.py b/Lib/test/test_ctypes/test_numbers.py index ca8a55eae3121d..64bd0919d19335 100644 --- a/Lib/test/test_ctypes/test_numbers.py +++ b/Lib/test/test_ctypes/test_numbers.py @@ -231,6 +231,8 @@ def test_init(self): # probably be changed: self.assertRaises(TypeError, c_int, c_long(42)) + @unittest.skipUnless(hasattr(ctypes, "c_double_complex"), + "requires C11 complex type") def test_float_overflow(self): big_int = int(sys.float_info.max) * 2 for t in float_types + [c_longdouble]: From 309d8f39001c86b888d61c86a246f981f56302e9 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 19 Sep 2026 02:42:01 +0300 Subject: [PATCH 3/5] Update Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst Co-authored-by: Victor Stinner --- .../Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst b/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst index 26cdad81424ee7..c97aa41766398e 100644 --- a/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst +++ b/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst @@ -1,2 +1,2 @@ -Don't raise exceptions on overflows in floating-point types of the -:mod:`ctypes`. Patch by Sergey B Kirpichev. +:mod:`ctypes`: Don't raise exceptions on overflows in floating-point types. +Patch by Sergey B Kirpichev. From c00602e41e4694fa7a35c41b37f51245303070c5 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 19 Sep 2026 02:46:23 +0300 Subject: [PATCH 4/5] address review: split tests --- Lib/test/test_ctypes/test_numbers.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_ctypes/test_numbers.py b/Lib/test/test_ctypes/test_numbers.py index 64bd0919d19335..f67cdf5f7d4e94 100644 --- a/Lib/test/test_ctypes/test_numbers.py +++ b/Lib/test/test_ctypes/test_numbers.py @@ -231,8 +231,6 @@ def test_init(self): # probably be changed: self.assertRaises(TypeError, c_int, c_long(42)) - @unittest.skipUnless(hasattr(ctypes, "c_double_complex"), - "requires C11 complex type") def test_float_overflow(self): big_int = int(sys.float_info.max) * 2 for t in float_types + [c_longdouble]: @@ -246,6 +244,11 @@ def test_float_overflow(self): self.assertEqual(ctypes.c_float(3e300).value, float('inf')) self.assertEqual(ctypes.c_float.__ctype_le__(3e300).value, float('inf')) self.assertEqual(ctypes.c_float.__ctype_be__(3e300).value, float('inf')) + + @unittest.skipUnless(hasattr(ctypes, "c_float_complex"), + "requires C11 complex type") + def test_complex_overflow(self): + # gh-156865: be silent in overflows of C types self.assertEqual(ctypes.c_float_complex(3e300).value, complex('inf')) self.assertEqual(ctypes.c_float_complex.__ctype_le__(3e300).value, complex('inf')) From 19ce6e267236fa8f570e1dadc5b2347d3a643231 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 24 Sep 2026 03:22:47 +0300 Subject: [PATCH 5/5] Apply batched suggestions from code review Co-authored-by: Victor Stinner --- .../next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst b/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst index c97aa41766398e..e27cd622562a96 100644 --- a/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst +++ b/Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst @@ -1,2 +1,2 @@ -:mod:`ctypes`: Don't raise exceptions on overflows in floating-point types. +Don't raise exceptions on overflows in :class:`ctypes.c_float_complex`. Patch by Sergey B Kirpichev.