Mercurial > pytave
comparison __py_struct_from_dict__.cc @ 374:d362cdd1ddeb
pyobject: add conversion methods for single, intX, and uintX types
* oct-py-types.cc, oct-py-types.h (pytave::extract_py_uint64): New function.
* __py_struct_from_dict__.cc (F__py_uint64_scalar_value__): New function.
(F__py_int64_scalar_value__): Simplify and clean up style. Add %!tests.
* @pyobject/pyobject.m (pyobject.single, pyobject.int8, pyobject.int16,
pyobject.int32, pyobject.uint8, pyobject.uint16, pyobject.uint32,
pyobject.uint64): New conversion methods.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Fri, 26 Aug 2016 18:51:42 -0700 |
parents | 0e4097c66788 |
children | d0a7f66393fc |
comparison
equal
deleted
inserted
replaced
373:0e4097c66788 | 374:d362cdd1ddeb |
---|---|
69 %!error __py_class_name__ (1, 2) | 69 %!error __py_class_name__ (1, 2) |
70 */ | 70 */ |
71 | 71 |
72 DEFUN_DLD (__py_int64_scalar_value__, args, nargout, | 72 DEFUN_DLD (__py_int64_scalar_value__, args, nargout, |
73 "-*- texinfo -*-\n\ | 73 "-*- texinfo -*-\n\ |
74 @deftypefn {} {} __py_int64_scalar_value__ (@var{x})\n\ | 74 @deftypefn {} {} __py_int64_scalar_value__ (@var{x})\n\ |
75 Extract a scalar int64 value from the Python integer @var{x}.\n\ | 75 Extract a scalar int64 value from the Python integer @var{x}.\n\ |
76 \n\ | 76 \n\ |
77 This is a private internal function not intended for direct use.\n\ | 77 This is a private internal function not intended for direct use.\n\ |
78 @end deftypefn") | 78 @end deftypefn") |
79 { | 79 { |
80 octave_value_list retval; | 80 if (args.length () != 1) |
81 | 81 print_usage (); |
82 int nargin = args.length (); | |
83 if (nargin != 1) | |
84 { | |
85 print_usage (); | |
86 return retval; | |
87 } | |
88 | 82 |
89 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 83 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
90 error ("pyobject.int64: argument must be a Python object"); | 84 error ("pyobject.int64: argument must be a Python object"); |
91 | 85 |
92 Py_Initialize (); | 86 Py_Initialize (); |
93 | 87 |
88 PyObject *obj = pytave::pyobject_unwrap_object (args(0)); | |
89 if (! obj) | |
90 error ("pyobject.int64: argument must be a valid Python object"); | |
91 | |
92 octave_int64 retval; | |
93 | |
94 try | 94 try |
95 { | 95 { |
96 // FIXME: PyObject *obj = look up stored pyobject reference (args(0)); | 96 retval = pytave::extract_py_int64 (obj); |
97 boost::python::object arg; | |
98 pytave::octvalue_to_pyobj (arg, args(0)); | |
99 PyObject *obj = arg.ptr (); | |
100 | |
101 retval(0) = octave_int64 (pytave::extract_py_int64 (obj)); | |
102 } | 97 } |
103 catch (pytave::object_convert_exception const &) | 98 catch (pytave::object_convert_exception const &) |
104 { | 99 { |
105 error ("pyobject.int64: error in return value type conversion"); | 100 error ("pyobject.int64: argument must be a Python int or long object"); |
106 } | 101 } |
107 catch (boost::python::error_already_set const &) | 102 catch (boost::python::error_already_set const &) |
108 { | 103 { |
109 std::string message = pytave::fetch_exception_message (); | 104 std::string message = pytave::fetch_exception_message (); |
110 error ("pyobject.int64: %s", message.c_str ()); | 105 error ("pyobject.int64: %s", message.c_str ()); |
111 } | 106 } |
112 | 107 Py_DECREF (obj); |
113 return retval; | 108 |
114 } | 109 return ovl (retval); |
110 } | |
111 | |
112 /* | |
113 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("0"))), int64 (0)) | |
114 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("2**62"))), int64 (2^62)) | |
115 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("-2**62"))), int64 (-2^62)) | |
116 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("2**128"))), intmax ("int64")) | |
117 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("-2**128"))), intmin ("int64")) | |
118 | |
119 %!error __py_int64_scalar_value__ () | |
120 %!error __py_int64_scalar_value__ (1) | |
121 %!error __py_int64_scalar_value__ (pyeval ("None")) | |
122 %!error __py_int64_scalar_value__ (1, 2) | |
123 */ | |
124 | |
125 DEFUN_DLD (__py_uint64_scalar_value__, args, nargout, | |
126 "-*- texinfo -*-\n\ | |
127 @deftypefn {} {} __py_uint64_scalar_value__ (@var{x})\n\ | |
128 Extract a scalar uint64 value from the Python integer @var{x}.\n\ | |
129 \n\ | |
130 This is a private internal function not intended for direct use.\n\ | |
131 @end deftypefn") | |
132 { | |
133 if (args.length () != 1) | |
134 print_usage (); | |
135 | |
136 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | |
137 error ("pyobject.uint64: argument must be a Python object"); | |
138 | |
139 Py_Initialize (); | |
140 | |
141 PyObject *obj = pytave::pyobject_unwrap_object (args(0)); | |
142 if (! obj) | |
143 error ("pyobject.uint64: argument must be a valid Python object"); | |
144 | |
145 octave_uint64 retval; | |
146 | |
147 try | |
148 { | |
149 retval = pytave::extract_py_uint64 (obj); | |
150 } | |
151 catch (pytave::object_convert_exception const &) | |
152 { | |
153 error ("pyobject.uint64: argument must be a Python int or long object"); | |
154 } | |
155 catch (boost::python::error_already_set const &) | |
156 { | |
157 std::string message = pytave::fetch_exception_message (); | |
158 error ("pyobject.uint64: %s", message.c_str ()); | |
159 } | |
160 Py_DECREF (obj); | |
161 | |
162 return ovl (retval); | |
163 } | |
164 | |
165 /* | |
166 %!assert (__py_uint64_scalar_value__ (pyobject (pyeval ("0"))), uint64 (0)) | |
167 %!assert (__py_uint64_scalar_value__ (pyobject (pyeval ("2**62"))), uint64 (2^62)) | |
168 %!assert (__py_uint64_scalar_value__ (pyobject (pyeval ("2**128"))), intmax ("uint64")) | |
169 %!assert (__py_uint64_scalar_value__ (pyobject (pyeval ("-2**128"))), intmin ("uint64")) | |
170 | |
171 %!error __py_uint64_scalar_value__ () | |
172 %!error __py_uint64_scalar_value__ (1) | |
173 %!error __py_uint64_scalar_value__ (pyeval ("None")) | |
174 %!error __py_uint64_scalar_value__ (1, 2) | |
175 */ | |
115 | 176 |
116 DEFUN_DLD (__py_is_none__, args, nargout, | 177 DEFUN_DLD (__py_is_none__, args, nargout, |
117 "-*- texinfo -*-\n\ | 178 "-*- texinfo -*-\n\ |
118 @deftypefn {} {} __py_is_none__ (@var{x})\n\ | 179 @deftypefn {} {} __py_is_none__ (@var{x})\n\ |
119 Check whether the Python object @var{obj} is the @code{None} object.\n\ | 180 Check whether the Python object @var{obj} is the @code{None} object.\n\ |