https://github.com/python/cpython/commit/5ec9283f0c69d7820b663d42437be0bdd8a8b5ae commit: 5ec9283f0c69d7820b663d42437be0bdd8a8b5ae branch: main author: Sergey B Kirpichev <[email protected]> committer: vstinner <[email protected]> date: 2026-09-24T10:26:16+02:00 summary:
gh-156865: Don't raise exceptions for ctypes.c_float_complex overflows (#156919) Co-authored-by: Victor Stinner <[email protected]> files: A Misc/NEWS.d/next/Library/2026-09-04-11-45-08.gh-issue-156865.mrdfKJ.rst M Lib/test/test_ctypes/test_numbers.py M Modules/_ctypes/cfield.c diff --git a/Lib/test/test_ctypes/test_numbers.py b/Lib/test/test_ctypes/test_numbers.py index b7df08f6079cd31..f67cdf5f7d4e945 100644 --- a/Lib/test/test_ctypes/test_numbers.py +++ b/Lib/test/test_ctypes/test_numbers.py @@ -240,6 +240,26 @@ 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')) + + @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')) + 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 000000000000000..e27cd622562a96c --- /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 :class:`ctypes.c_float_complex`. +Patch by Sergey B Kirpichev. diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index 00b075bccff6647..193c60a1afee85c 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; } _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
