diff options
author | Alec Cooper <ahnolds@gmail.com> | 2015-12-14 22:38:40 -0500 |
---|---|---|
committer | Alec Cooper <ahnolds@gmail.com> | 2015-12-23 19:04:19 -0500 |
commit | ba01182ec4806667f4b220b88cc7e25da08d834d (patch) | |
tree | e6da778be137588d13489466746d4799d519eacb | |
parent | adc773455da96598bb8f1099b8adc2898aa1e274 (diff) | |
download | swig-ba01182ec4806667f4b220b88cc7e25da08d834d.tar.gz |
Fixing Python primitive conversions
Don't mistakenly treat PyLong objects as PyInt objects in Python3.
This resolves issues of large integers being incorrectly treated as -1 while also having
an OverflowError set internally for converting PyLong->long and PyLong->double
Conversions from PyLong to long, unsigned long, long long, and unsigned long long now
raise OverflowError rather than TypeError when given an out of range value.
Removing unnecessary check for PyLong_AsLong when converting PyLong->unsigned long since the
call to PyLong_AsUnsignedLong will have covered this case.
-rw-r--r-- | Lib/python/pyprimtypes.swg | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/Lib/python/pyprimtypes.swg b/Lib/python/pyprimtypes.swg index 30bb64f66..73a97bc5a 100644 --- a/Lib/python/pyprimtypes.swg +++ b/Lib/python/pyprimtypes.swg @@ -75,16 +75,20 @@ SWIGINTERNINLINE PyObject* SWIGINTERN int SWIG_AsVal_dec(long)(PyObject *obj, long* val) { +%#if PY_VERSION_HEX < 0x03000000 if (PyInt_Check(obj)) { if (val) *val = PyInt_AsLong(obj); return SWIG_OK; - } else if (PyLong_Check(obj)) { + } else +%#endif + if (PyLong_Check(obj)) { long v = PyLong_AsLong(obj); if (!PyErr_Occurred()) { if (val) *val = v; return SWIG_OK; } else { PyErr_Clear(); + return SWIG_OverflowError; } } %#ifdef SWIG_PYTHON_CAST_MODE @@ -146,18 +150,7 @@ SWIG_AsVal_dec(unsigned long)(PyObject *obj, unsigned long *val) return SWIG_OK; } else { PyErr_Clear(); -%#if PY_VERSION_HEX >= 0x03000000 - { - long v = PyLong_AsLong(obj); - if (!PyErr_Occurred()) { - if (v < 0) { - return SWIG_OverflowError; - } - } else { - PyErr_Clear(); - } - } -%#endif + return SWIG_OverflowError; } } %#ifdef SWIG_PYTHON_CAST_MODE @@ -212,6 +205,7 @@ SWIG_AsVal_dec(long long)(PyObject *obj, long long *val) return SWIG_OK; } else { PyErr_Clear(); + res = SWIG_OverflowError; } } else { long v; @@ -266,6 +260,7 @@ SWIG_AsVal_dec(unsigned long long)(PyObject *obj, unsigned long long *val) return SWIG_OK; } else { PyErr_Clear(); + res = SWIG_OverflowError; } } else { unsigned long v; @@ -305,9 +300,11 @@ SWIG_AsVal_dec(double)(PyObject *obj, double *val) if (PyFloat_Check(obj)) { if (val) *val = PyFloat_AsDouble(obj); return SWIG_OK; +%#if PY_VERSION_HEX < 0x03000000 } else if (PyInt_Check(obj)) { if (val) *val = PyInt_AsLong(obj); return SWIG_OK; +%#endif } else if (PyLong_Check(obj)) { double v = PyLong_AsDouble(obj); if (!PyErr_Occurred()) { |