Skip to content

Commit

Permalink
Ensure that values that exceed sizeof(long) for Python 2 on Windows w…
Browse files Browse the repository at this point in the history
…hen using

the Oracle type NATIVE_INT are not silently truncated
(#257).
  • Loading branch information
anthony-tuininga committed Jan 22, 2019
1 parent 43a4850 commit 1860fdb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/cxoTransform.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,12 @@ PyObject *cxoTransform_toPython(cxoTransformNum transformNum,
case CXO_TRANSFORM_NATIVE_FLOAT:
return PyFloat_FromDouble(dbValue->asFloat);
case CXO_TRANSFORM_NATIVE_INT:
return PyInt_FromLong((long) dbValue->asInt64);
#if PY_MAJOR_VERSION < 3
if (sizeof(long) == 8 || (dbValue->asInt64 <= INT_MAX &&
dbValue->asInt64 >= -INT_MAX))
return PyInt_FromLong((long) dbValue->asInt64);
#endif
return PyLong_FromLongLong(dbValue->asInt64);
case CXO_TRANSFORM_DECIMAL:
case CXO_TRANSFORM_INT:
case CXO_TRANSFORM_FLOAT:
Expand Down
12 changes: 12 additions & 0 deletions test/NumberVar.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

class TestNumberVar(BaseTestCase):

def outputTypeHandlerNativeInt(self, cursor, name, defaultType, size,
precision, scale):
return cursor.var(cx_Oracle.NATIVE_INT, arraysize=cursor.arraysize)

def outputTypeHandlerDecimal(self, cursor, name, defaultType, size,
precision, scale):
if defaultType == cx_Oracle.NUMBER:
Expand Down Expand Up @@ -380,3 +384,11 @@ def testBindNativeFloat(self):
value, = self.cursor.fetchone()
self.assertEqual(str(value), str(float("NaN")))

def testFetchNativeInt(self):
"test fetching numbers as native integers"
self.cursor.outputtypehandler = self.outputTypeHandlerNativeInt
for value in (1, 2 ** 31, 2 ** 63 - 1, -1, -2 ** 31, -2 ** 63 + 1):
self.cursor.execute("select :1 from dual", [str(value)])
fetchedValue, = self.cursor.fetchone()
self.assertEqual(value, fetchedValue)

0 comments on commit 1860fdb

Please sign in to comment.