Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_ctypes/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`ctypes`: Don't raise exceptions on overflows in floating-point types.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the change correctly, only ctypes.c_float_complex is affected. So I suggest to mention the type directly:

Suggested change
: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.
8 changes: 4 additions & 4 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,14 +865,14 @@ Zf_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
return NULL;
}
#ifdef WORDS_BIGENDIAN

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of the casts is not obvious to me. IMO it's worth it to add a comment to explain they.

Suggested change
#ifdef WORDS_BIGENDIAN
// gh-156865: Cast double to float to avoid overflow in PyFloat_Pack4()
#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))
Comment thread
skirpichev marked this conversation as resolved.
{
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;
}
Expand Down
Loading