diff --git a/src/cxoTransform.c b/src/cxoTransform.c index d6cbcd7d..e29b413c 100644 --- a/src/cxoTransform.c +++ b/src/cxoTransform.c @@ -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: diff --git a/test/NumberVar.py b/test/NumberVar.py index a297b430..0d84e83c 100644 --- a/test/NumberVar.py +++ b/test/NumberVar.py @@ -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: @@ -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) +