From cc067bf83e304f5177374cb0c792568c2ef9f3de Mon Sep 17 00:00:00 2001 From: Anthony Tuininga Date: Tue, 8 Jun 2021 11:15:13 -0600 Subject: [PATCH] Binary integer variables now explicitly convert values to integers (since implicit conversion to integer has become an error in Python 3.10) and values that are not `int`, `float` or `decimal.Decimal` are explicitly rejected. --- doc/src/release_notes.rst | 4 ++++ src/cxoTransform.c | 27 +++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/doc/src/release_notes.rst b/doc/src/release_notes.rst index f308aa8..e2fdf4e 100644 --- a/doc/src/release_notes.rst +++ b/doc/src/release_notes.rst @@ -13,6 +13,10 @@ Version 8.3 (TBD) #) Updated embedded ODPI-C to `version 4.3.0 `__. +#) Binary integer variables now explicitly convert values to integers (since + implicit conversion to integer has become an error in Python 3.10) and + values that are not `int`, `float` or `decimal.Decimal` are explicitly + rejected. Version 8.2.1 (June 2021) diff --git a/src/cxoTransform.c b/src/cxoTransform.c index b7bc59d..dfe51e5 100644 --- a/src/cxoTransform.c +++ b/src/cxoTransform.c @@ -244,7 +244,7 @@ int cxoTransform_fromPython(cxoTransformNum transformNum, dpiIntervalDS *interval; PyDateTime_Delta *delta; int32_t deltaSeconds; - PyObject *textValue; + PyObject *tempValue; cxoObject *obj; cxoLob *lob; int status; @@ -316,10 +316,21 @@ int cxoTransform_fromPython(cxoTransformNum transformNum, dbValue->asInt64 = (pyValue == Py_True); return 0; } - dbValue->asInt64 = PyLong_AsLong(pyValue); - if (PyErr_Occurred()) + if (!PyFloat_Check(pyValue) && + !PyLong_Check(pyValue) && + !PyObject_TypeCheck(pyValue, cxoPyTypeDecimal)) { + PyErr_SetString(PyExc_TypeError, + "expecting number or boolean"); return -1; - return 0; + } + tempValue = PyObject_CallFunctionObjArgs((PyObject*) &PyLong_Type, + pyValue, NULL); + if (!tempValue) + return -1; + dbValue->asInt64 = PyLong_AsLong(tempValue); + status = (PyErr_Occurred()) ? -1 : 0; + Py_DECREF(tempValue); + return status; case CXO_TRANSFORM_INT: case CXO_TRANSFORM_DECIMAL: case CXO_TRANSFORM_FLOAT: @@ -334,11 +345,11 @@ int cxoTransform_fromPython(cxoTransformNum transformNum, PyErr_SetString(PyExc_TypeError, "expecting number"); return -1; } - textValue = PyObject_Str(pyValue); - if (!textValue) + tempValue = PyObject_Str(pyValue); + if (!tempValue) return -1; - status = cxoBuffer_fromObject(buffer, textValue, encoding); - Py_DECREF(textValue); + status = cxoBuffer_fromObject(buffer, tempValue, encoding); + Py_DECREF(tempValue); if (status < 0) return -1; }