changeset 355:8cdc94b83e98

Fix int64 value extraction from long to work on 32-bit, use Octave saturation * oct-py-types.cc (pytave::extract_py_int64): Extract value to appropriately sized integer type for platform architecture. Saturate value on overflow.
author Mike Miller <mtmiller@octave.org>
date Mon, 22 Aug 2016 17:27:46 -0700
parents eec3ed1c0578
children 6cd581661176
files oct-py-types.cc
diffstat 1 files changed, 15 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/oct-py-types.cc	Mon Aug 22 17:24:13 2016 -0700
+++ b/oct-py-types.cc	Mon Aug 22 17:27:46 2016 -0700
@@ -24,6 +24,7 @@
 #  include <config.h>
 #endif
 
+#include <limits>
 #include <octave/Cell.h>
 #include <octave/oct-map.h>
 #include <octave/quit.h>
@@ -386,7 +387,20 @@
     throw object_convert_exception ("failed to extract integer: null object");
 
   if (PyLong_Check (obj))
-    return PyLong_AsLong (obj);
+    {
+      int overflow = 0;
+#if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG == 8))
+      PY_LONG_LONG value = PyLong_AsLongLongAndOverflow (obj, &overflow);
+#else
+      long value = PyLong_AsLongAndOverflow (obj, &overflow);
+#endif
+      if (overflow)
+        if (overflow > 0)
+          value = std::numeric_limits<int64_t>::max ();
+        else
+          value = std::numeric_limits<int64_t>::min ();
+      return static_cast<int64_t> (value);
+    }
 #if PY_VERSION_HEX < 0x03000000
   else if (PyInt_Check (obj))
     return PyInt_AsLong (obj);