From c5ace142e651a4ea8635b39a717870c37ee9f696 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 5 Nov 2021 15:37:17 +1100 Subject: Fix PyAPI integer conversion error handling Non integer types raised an OverflowError, even when non-number types were passed in. Now the error from Python is kept. --- source/blender/python/generic/py_capi_utils.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'source/blender/python') diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index e847a5a5b5c..9cccc2f608f 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -1677,6 +1677,9 @@ bool PyC_RunString_AsString(const char *imports[], int PyC_Long_AsBool(PyObject *value) { const int test = _PyLong_AsInt(value); + if (UNLIKELY(test == -1 && PyErr_Occurred())) { + return -1; + } if (UNLIKELY((uint)test > 1)) { PyErr_SetString(PyExc_TypeError, "Python number not a bool (0/1)"); return -1; @@ -1687,6 +1690,9 @@ int PyC_Long_AsBool(PyObject *value) int8_t PyC_Long_AsI8(PyObject *value) { const int test = _PyLong_AsInt(value); + if (UNLIKELY(test == -1 && PyErr_Occurred())) { + return -1; + } if (UNLIKELY(test < INT8_MIN || test > INT8_MAX)) { PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C int8"); return -1; @@ -1697,6 +1703,9 @@ int8_t PyC_Long_AsI8(PyObject *value) int16_t PyC_Long_AsI16(PyObject *value) { const int test = _PyLong_AsInt(value); + if (UNLIKELY(test == -1 && PyErr_Occurred())) { + return -1; + } if (UNLIKELY(test < INT16_MIN || test > INT16_MAX)) { PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C int16"); return -1; @@ -1712,6 +1721,9 @@ int16_t PyC_Long_AsI16(PyObject *value) uint8_t PyC_Long_AsU8(PyObject *value) { const ulong test = PyLong_AsUnsignedLong(value); + if (UNLIKELY(test == (ulong)-1 && PyErr_Occurred())) { + return (uint8_t)-1; + } if (UNLIKELY(test > UINT8_MAX)) { PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C uint8"); return (uint8_t)-1; @@ -1722,6 +1734,9 @@ uint8_t PyC_Long_AsU8(PyObject *value) uint16_t PyC_Long_AsU16(PyObject *value) { const ulong test = PyLong_AsUnsignedLong(value); + if (UNLIKELY(test == (ulong)-1 && PyErr_Occurred())) { + return (uint16_t)-1; + } if (UNLIKELY(test > UINT16_MAX)) { PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C uint16"); return (uint16_t)-1; @@ -1732,6 +1747,9 @@ uint16_t PyC_Long_AsU16(PyObject *value) uint32_t PyC_Long_AsU32(PyObject *value) { const ulong test = PyLong_AsUnsignedLong(value); + if (UNLIKELY(test == (ulong)-1 && PyErr_Occurred())) { + return (uint32_t)-1; + } if (UNLIKELY(test > UINT32_MAX)) { PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C uint32"); return (uint32_t)-1; -- cgit v1.2.3