Skip to content

Commit

Permalink
Added missing type check to prevent coercion of decimal to float
Browse files Browse the repository at this point in the history
(#68).
  • Loading branch information
anthony-tuininga committed Aug 18, 2017
1 parent 6582aac commit 7b6276f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/NumberVar.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ static int NumberVar_SetValueDecimal(udt_Variable *var, uint32_t pos,
static int NumberVar_SetValueFloat(udt_Variable *var, uint32_t pos,
dpiData *data, PyObject *value)
{
if (!PyFloat_Check(value) &&
#if PY_MAJOR_VERSION < 3
!PyInt_Check(value) &&
#endif
!PyLong_Check(value)) {
PyErr_SetString(PyExc_TypeError, "expecting float");
return -1;
}
data->value.asDouble = PyFloat_AsDouble(value);
if (PyErr_Occurred())
return -1;
Expand Down
17 changes: 17 additions & 0 deletions test/NumberVar.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

class TestNumberVar(BaseTestCase):

def outputTypeHandlerDecimal(self, cursor, name, defaultType, size,
precision, scale):
if defaultType == cx_Oracle.NUMBER:
return cursor.var(str, 255, outconverter = decimal.Decimal,
arraysize = cursor.arraysize)

def setUp(self):
BaseTestCase.setUp(self)
self.rawData = []
Expand Down Expand Up @@ -103,6 +109,17 @@ def testBindIntegerAfterString(self):
value = 3)
self.assertEqual(self.cursor.fetchall(), [self.dataByKey[3]])

def testBindDecimalAfterNumber(self):
"test binding in a decimal value after setting input sizes to a number"
cursor = self.connection.cursor()
value = decimal.Decimal("319438950232418390.273596")
cursor.setinputsizes(value = cx_Oracle.NUMBER)
cursor.outputtypehandler = self.outputTypeHandlerDecimal
cursor.execute("select :value from dual",
value = value)
outValue, = cursor.fetchone()
self.assertEqual(outValue, value)

def testBindNull(self):
"test binding in a null"
self.cursor.execute("""
Expand Down

0 comments on commit 7b6276f

Please sign in to comment.