comparison 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
comparison
equal deleted inserted replaced
327:15c20ab4b80a 328:5782d7932529
213 213
214 %!error <argument type conversion> 214 %!error <argument type conversion>
215 %! pyexec ("def intwrapper(x): return int(x)"); 215 %! pyexec ("def intwrapper(x): return int(x)");
216 %! pycall ("intwrapper", ftp ()); 216 %! pycall ("intwrapper", ftp ());
217 217
218 ## Test conversion of integer types from Python
219 %!test
220 %! if (pyeval ("__import__('sys').hexversion >= 0x03000000"))
221 %! assert (isa (pycall ("int", 0), "pyobject"))
222 %! assert (isa (pycall ("int", 2^31-1), "pyobject"))
223 %! assert (isa (pycall ("int", -2^31), "pyobject"))
224 %! assert (double (pycall ("int", 0)), 0)
225 %! assert (double (pycall ("int", 2^31-1)), 2^31-1)
226 %! assert (double (pycall ("int", -2^31)), -2^31)
227 %! else
228 %! assert (pycall ("int", 0), int64 (0))
229 %! assert (pycall ("int", 2^31-1), int64 (2^31-1))
230 %! assert (pycall ("int", -2^31), int64 (-2^31))
231 %! assert (isa (pycall ("long", 0), "pyobject"))
232 %! assert (isa (pycall ("long", 2^31-1), "pyobject"))
233 %! assert (isa (pycall ("long", -2^31), "pyobject"))
234 %! assert (double (pycall ("long", 0)), 0)
235 %! assert (double (pycall ("long", 2^31-1)), 2^31-1)
236 %! assert (double (pycall ("long", -2^31)), -2^31)
237 %! endif
238 %!assert (isa (pycall ("int", 2^100), "pyobject"))
239
218 %!test 240 %!test
219 %! pyexec ("def pyfunc(x): return 2*x"); 241 %! pyexec ("def pyfunc(x): return 2*x");
220 %! z = pycall ("pyfunc", [20 20]); 242 %! z = pycall ("pyfunc", [20 20]);
221 %! assert (z, [40 40]) 243 %! assert (z, [40 40])
222 244