Mercurial > pytave
changeset 356:6cd581661176
pyobject.int64: New method to convert integer object to Octave int64
* __py_struct_from_dict__.cc (F__py_int64_scalar_value__): New function to
extract int64 scalar value from a pyobject.
* @pyobject/pyobject.m: Add conversion method int64 as a wrapper around
__py_int64_scalar_value__. Add %!tests of int64 value extraction.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Mon, 22 Aug 2016 17:33:20 -0700 |
parents | 8cdc94b83e98 |
children | ebd83497ebda |
files | @pyobject/pyobject.m __py_struct_from_dict__.cc |
diffstat | 2 files changed, 56 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/@pyobject/pyobject.m Mon Aug 22 17:27:46 2016 -0700 +++ b/@pyobject/pyobject.m Mon Aug 22 17:33:20 2016 -0700 @@ -138,6 +138,10 @@ endif endfunction + function y = int64 (x) + y = __py_int64_scalar_value__ (x); + endfunction + function y = struct (x) y = __py_struct_from_dict__ (x); endfunction @@ -344,6 +348,14 @@ %!error double (pyobject ()) %!error double (pyeval ("[1, 2, 3]")) +## Test conversion method pyobject.int64 +%!assert (int64 (pyobject (int8 (0))), int64 (0)) +%!assert (int64 (pyobject (int64 (42))), int64 (42)) +%!assert (int64 (pyobject (intmax ("int64"))), intmax ("int64")) +%!assert (int64 (pyobject (intmin ("int64"))), intmin ("int64")) +%!assert (int64 (pycall ("int", 1e100)), intmax ("int64")) +%!assert (int64 (pycall ("int", -1e100)), intmin ("int64")) + ## Test conversion method pyobject.struct %!assert (struct (pycall ("dict")), struct ()) %!assert (struct (pyobject (struct ())), struct ())
--- a/__py_struct_from_dict__.cc Mon Aug 22 17:27:46 2016 -0700 +++ b/__py_struct_from_dict__.cc Mon Aug 22 17:33:20 2016 -0700 @@ -33,6 +33,50 @@ #include "oct-py-types.h" #include "octave_to_python.h" +DEFUN_DLD (__py_int64_scalar_value__, args, nargout, + "-*- texinfo -*-\n\ +@deftypefn {} {} __py_int64_scalar_value__ (@var{x})\n\ +Extract a scalar int64 value from the Python integer @var{x}.\n\ +\n\ +This is a private internal function not intended for direct use.\n\ +@end deftypefn") +{ + octave_value_list retval; + + int nargin = args.length (); + if (nargin != 1) + { + print_usage (); + return retval; + } + + if (! (args(0).is_object () && args(0).class_name () == "pyobject")) + error ("pyobject.int64: argument must be a Python object"); + + Py_Initialize (); + + try + { + // FIXME: PyObject *obj = look up stored pyobject reference (args(0)); + boost::python::object arg; + pytave::octvalue_to_pyobj (arg, args(0)); + PyObject *obj = arg.ptr (); + + retval(0) = octave_int64 (pytave::extract_py_int64 (obj)); + } + catch (pytave::object_convert_exception const &) + { + error ("pyobject.int64: error in return value type conversion"); + } + catch (boost::python::error_already_set const &) + { + std::string message = pytave::fetch_exception_message (); + error ("pyobject.int64: %s", message.c_str ()); + } + + return retval; +} + DEFUN_DLD (__py_struct_from_dict__, args, nargout, "-*- texinfo -*-\n\ @deftypefn {} {} __py_struct_from_dict__ (@var{dict})\n\