changeset 271:0a4f620da178

Convert numeric value to long instead of int to avoid overflow (fixes issue #40) * python_to_octave.cc: Try conversion of py_object to 'long' instead of 'int' * pyeval.cc: Add tests to verify no positive/negative overflow
author Abhinav Tripathi <genuinelucifer@gmail.com>
date Thu, 28 Jul 2016 11:50:29 -0700
parents c8da556b6793
children 1446812ec1de
files pyeval.cc python_to_octave.cc
diffstat 2 files changed, 8 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/pyeval.cc	Wed Jul 27 20:30:35 2016 -0700
+++ b/pyeval.cc	Thu Jul 28 11:50:29 2016 -0700
@@ -141,6 +141,11 @@
 
 %!assert (isa (pyeval ("object()"), "pyobject"))
 
+%!assert (isnumeric (pyeval ("sys.maxsize")))
+%!assert (isnumeric (pyeval ("-sys.maxsize")))
+%!assert (pyeval ("99999999999999"), 99999999999999)
+%!assert (pyeval ("-99999999999999"), -99999999999999)
+
 ## FIXME: these will change when dict, list, and tuple are not converted
 %!assert (pyeval ("{'x': 1, 'y': 2}"), struct ("x", 1, "y", 2))
 %!assert (pyeval ("[1, 2, 3]"), {1, 2, 3})
--- a/python_to_octave.cc	Wed Jul 27 20:30:35 2016 -0700
+++ b/python_to_octave.cc	Thu Jul 28 11:50:29 2016 -0700
@@ -477,7 +477,7 @@
   void pyobj_to_octvalue (octave_value& oct_value,
                           const boost::python::object& py_object)
   {
-    extract<int> intx (py_object);
+    extract<long> longx (py_object);
     extract<bool> boolx (py_object);
     extract<double> doublex (py_object);
     extract<Complex> complexx (py_object);
@@ -490,8 +490,8 @@
 
     if (boolx.check () && PyBool_Check ((PyArrayObject*)py_object.ptr ()))
       oct_value = boolx ();
-    else if (intx.check ())
-      oct_value = intx ();
+    else if (longx.check ())
+      oct_value = longx ();
     else if (doublex.check ())
       oct_value = doublex ();
     else if (complexx.check ())