comparison __py_struct_from_dict__.cc @ 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 baff3b90dcb1
children d41fc23d4b9f
comparison
equal deleted inserted replaced
355:8cdc94b83e98 356:6cd581661176
30 #define PYTAVE_DO_DECLARE_SYMBOL 30 #define PYTAVE_DO_DECLARE_SYMBOL
31 #include "arrayobjectdefs.h" 31 #include "arrayobjectdefs.h"
32 #include "exceptions.h" 32 #include "exceptions.h"
33 #include "oct-py-types.h" 33 #include "oct-py-types.h"
34 #include "octave_to_python.h" 34 #include "octave_to_python.h"
35
36 DEFUN_DLD (__py_int64_scalar_value__, args, nargout,
37 "-*- texinfo -*-\n\
38 @deftypefn {} {} __py_int64_scalar_value__ (@var{x})\n\
39 Extract a scalar int64 value from the Python integer @var{x}.\n\
40 \n\
41 This is a private internal function not intended for direct use.\n\
42 @end deftypefn")
43 {
44 octave_value_list retval;
45
46 int nargin = args.length ();
47 if (nargin != 1)
48 {
49 print_usage ();
50 return retval;
51 }
52
53 if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
54 error ("pyobject.int64: argument must be a Python object");
55
56 Py_Initialize ();
57
58 try
59 {
60 // FIXME: PyObject *obj = look up stored pyobject reference (args(0));
61 boost::python::object arg;
62 pytave::octvalue_to_pyobj (arg, args(0));
63 PyObject *obj = arg.ptr ();
64
65 retval(0) = octave_int64 (pytave::extract_py_int64 (obj));
66 }
67 catch (pytave::object_convert_exception const &)
68 {
69 error ("pyobject.int64: error in return value type conversion");
70 }
71 catch (boost::python::error_already_set const &)
72 {
73 std::string message = pytave::fetch_exception_message ();
74 error ("pyobject.int64: %s", message.c_str ());
75 }
76
77 return retval;
78 }
35 79
36 DEFUN_DLD (__py_struct_from_dict__, args, nargout, 80 DEFUN_DLD (__py_struct_from_dict__, args, nargout,
37 "-*- texinfo -*-\n\ 81 "-*- texinfo -*-\n\
38 @deftypefn {} {} __py_struct_from_dict__ (@var{dict})\n\ 82 @deftypefn {} {} __py_struct_from_dict__ (@var{dict})\n\
39 Extract a scalar struct from the Python dict @var{dict}.\n\ 83 Extract a scalar struct from the Python dict @var{dict}.\n\