diff pycall.cc @ 328:5782d7932529

Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56) * oct-py-types.cc, oct-py-types.h (pytave::extract_py_int64): New function. * python_to_octave.cc (pytave::pyobj_to_octvalue): Convert Python 2 'int' to Octave int64 type. * @py/py.m, pycall.cc: Add %!tests for Python 'int' and 'long' type handling, conditional on Python version.
author Mike Miller <mtmiller@octave.org>
date Sat, 13 Aug 2016 23:52:57 -0700
parents 15c20ab4b80a
children cee203ea6245
line wrap: on
line diff
--- a/pycall.cc	Sat Aug 13 21:24:29 2016 -0700
+++ b/pycall.cc	Sat Aug 13 23:52:57 2016 -0700
@@ -215,6 +215,28 @@
 %! pyexec ("def intwrapper(x): return int(x)");
 %! pycall ("intwrapper", ftp ());
 
+## Test conversion of integer types from Python
+%!test
+%! if (pyeval ("__import__('sys').hexversion >= 0x03000000"))
+%!   assert (isa (pycall ("int",  0), "pyobject"))
+%!   assert (isa (pycall ("int",  2^31-1), "pyobject"))
+%!   assert (isa (pycall ("int", -2^31), "pyobject"))
+%!   assert (double (pycall ("int",  0)), 0)
+%!   assert (double (pycall ("int",  2^31-1)), 2^31-1)
+%!   assert (double (pycall ("int", -2^31)), -2^31)
+%! else
+%!   assert (pycall ("int",  0), int64 (0))
+%!   assert (pycall ("int",  2^31-1), int64 (2^31-1))
+%!   assert (pycall ("int", -2^31), int64 (-2^31))
+%!   assert (isa (pycall ("long",  0), "pyobject"))
+%!   assert (isa (pycall ("long",  2^31-1), "pyobject"))
+%!   assert (isa (pycall ("long", -2^31), "pyobject"))
+%!   assert (double (pycall ("long",  0)), 0)
+%!   assert (double (pycall ("long",  2^31-1)), 2^31-1)
+%!   assert (double (pycall ("long", -2^31)), -2^31)
+%! endif
+%!assert (isa (pycall ("int", 2^100), "pyobject"))
+
 %!test
 %! pyexec ("def pyfunc(x): return 2*x");
 %! z = pycall ("pyfunc", [20 20]);