Mercurial > pytave
diff oct-py-types.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 | fd5881d48238 |
children | c081e30c2f64 |
line wrap: on
line diff
--- a/oct-py-types.cc Sat Aug 13 21:24:29 2016 -0700 +++ b/oct-py-types.cc Sat Aug 13 23:52:57 2016 -0700 @@ -68,6 +68,24 @@ return dict; } +int64_t +extract_py_int64 (PyObject *obj) +{ + if (! obj) + throw object_convert_exception ("failed to extract integer: null object"); + + if (PyLong_Check (obj)) + return PyLong_AsLong (obj); +#if PY_VERSION_HEX < 0x03000000 + else if (PyInt_Check (obj)) + return PyInt_AsLong (obj); +#endif + else + throw object_convert_exception ("failed to extract integer: wrong type"); + + return 0; +} + std::string extract_py_str (PyObject *obj) {