annotate __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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
345
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
1 /*
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
2
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
3 Copyright (C) 2016 Mike Miller
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
4
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
5 This file is part of Pytave.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
6
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
7 Pytave is free software: you can redistribute it and/or modify it
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
9 Free Software Foundation, either version 3 of the License, or (at your
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
10 option) any later version.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
11
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
12 Pytave is distributed in the hope that it will be useful, but WITHOUT
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
15 for more details.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
16
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
18 along with Pytave; see the file COPYING. If not, see
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
20
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
21 */
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
22
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
23 #if defined (HAVE_CONFIG_H)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
24 # include <config.h>
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
25 #endif
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
26
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
27 #include <Python.h>
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
28 #include <octave/oct.h>
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
29
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
30 #define PYTAVE_DO_DECLARE_SYMBOL
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
31 #include "arrayobjectdefs.h"
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
32 #include "exceptions.h"
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
33 #include "oct-py-types.h"
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
34 #include "octave_to_python.h"
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
35
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
36 DEFUN_DLD (__py_int64_scalar_value__, args, nargout,
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
37 "-*- texinfo -*-\n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
38 @deftypefn {} {} __py_int64_scalar_value__ (@var{x})\n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
39 Extract a scalar int64 value from the Python integer @var{x}.\n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
40 \n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
41 This is a private internal function not intended for direct use.\n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
42 @end deftypefn")
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
43 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
44 octave_value_list retval;
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
45
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
46 int nargin = args.length ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
47 if (nargin != 1)
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
48 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
49 print_usage ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
50 return retval;
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
51 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
52
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
53 if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
54 error ("pyobject.int64: argument must be a Python object");
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
55
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
56 Py_Initialize ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
57
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
58 try
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
59 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
60 // FIXME: PyObject *obj = look up stored pyobject reference (args(0));
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
61 boost::python::object arg;
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
62 pytave::octvalue_to_pyobj (arg, args(0));
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
63 PyObject *obj = arg.ptr ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
64
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
65 retval(0) = octave_int64 (pytave::extract_py_int64 (obj));
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
66 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
67 catch (pytave::object_convert_exception const &)
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
68 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
69 error ("pyobject.int64: error in return value type conversion");
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
70 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
71 catch (boost::python::error_already_set const &)
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
72 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
73 std::string message = pytave::fetch_exception_message ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
74 error ("pyobject.int64: %s", message.c_str ());
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
75 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
76
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
77 return retval;
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
78 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
79
345
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
80 DEFUN_DLD (__py_struct_from_dict__, args, nargout,
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
81 "-*- texinfo -*-\n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
82 @deftypefn {} {} __py_struct_from_dict__ (@var{dict})\n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
83 Extract a scalar struct from the Python dict @var{dict}.\n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
84 \n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
85 This is a private internal function not intended for direct use.\n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
86 @end deftypefn")
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
87 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
88 octave_value_list retval;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
89 std::string id;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
90
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
91 int nargin = args.length ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
92
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
93 if (nargin != 1)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
94 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
95 print_usage ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
96 return retval;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
97 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
98
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
99 if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
100 error ("pyobject.struct: argument must be a Python object");
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
101
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
102 Py_Initialize ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
103
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
104 try
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
105 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
106 // FIXME: PyObject *obj = look up stored pyobject reference (args(0));
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
107 boost::python::object arg;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
108 pytave::octvalue_to_pyobj (arg, args(0));
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
109 PyObject *obj = arg.ptr ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
110
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
111 retval(0) = pytave::extract_py_scalar_map (obj);
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
112 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
113 catch (pytave::object_convert_exception const &)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
114 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
115 error ("pyobject.struct: error in return value type conversion");
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
116 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
117 catch (boost::python::error_already_set const &)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
118 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
119 std::string message = pytave::fetch_exception_message ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
120 error ("pyobject.struct: %s", message.c_str ());
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
121 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
122
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
123 return retval;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
124 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
125
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
126 /*
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
127 ## No test needed for internal helper function.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
128 %!assert (1)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
129 */