Mercurial > pytave
comparison __py_struct_from_dict__.cc @ 404:aef165ff92b0
Adopt pytave::python_object where objects are local or created and returned
* __py_struct_from_dict__.cc, exceptions.cc, oct-py-eval.cc, oct-py-types.cc,
pycall.cc: Use pytave::python_object in place of PyObject where objects are
created locally and destroyed or returned to the caller.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Fri, 28 Apr 2017 16:21:39 -0700 |
parents | 3644df6564bc |
children | 478d83448b0b |
comparison
equal
deleted
inserted
replaced
403:3644df6564bc | 404:aef165ff92b0 |
---|---|
28 #include <octave/oct.h> | 28 #include <octave/oct.h> |
29 | 29 |
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-object.h" | |
33 #include "oct-py-types.h" | 34 #include "oct-py-types.h" |
34 #include "oct-py-util.h" | 35 #include "oct-py-util.h" |
35 #include "octave_to_python.h" | 36 #include "octave_to_python.h" |
36 | 37 |
37 DEFUN_DLD (__py_class_name__, args, , | 38 DEFUN_DLD (__py_class_name__, args, , |
48 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 49 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
49 error ("__py_class_name__: argument must be a valid Python object"); | 50 error ("__py_class_name__: argument must be a valid Python object"); |
50 | 51 |
51 Py_Initialize (); | 52 Py_Initialize (); |
52 | 53 |
53 PyObject *obj = pytave::pyobject_unwrap_object (args(0)); | 54 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
54 std::string name = pytave::py_object_class_name (obj); | 55 std::string name = pytave::py_object_class_name (obj); |
55 Py_DECREF (obj); | |
56 | 56 |
57 return ovl (name); | 57 return ovl (name); |
58 } | 58 } |
59 | 59 |
60 /* | 60 /* |
87 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 87 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
88 error ("pyobject.int64: argument must be a Python object"); | 88 error ("pyobject.int64: argument must be a Python object"); |
89 | 89 |
90 Py_Initialize (); | 90 Py_Initialize (); |
91 | 91 |
92 PyObject *obj = pytave::pyobject_unwrap_object (args(0)); | 92 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
93 if (! obj) | 93 if (! obj) |
94 error ("pyobject.int64: argument must be a valid Python object"); | 94 error ("pyobject.int64: argument must be a valid Python object"); |
95 | 95 |
96 octave_int64 retval; | 96 octave_int64 retval; |
97 | 97 |
106 catch (boost::python::error_already_set const &) | 106 catch (boost::python::error_already_set const &) |
107 { | 107 { |
108 std::string message = pytave::fetch_exception_message (); | 108 std::string message = pytave::fetch_exception_message (); |
109 error ("pyobject.int64: %s", message.c_str ()); | 109 error ("pyobject.int64: %s", message.c_str ()); |
110 } | 110 } |
111 Py_DECREF (obj); | |
112 | 111 |
113 return ovl (retval); | 112 return ovl (retval); |
114 } | 113 } |
115 | 114 |
116 /* | 115 /* |
140 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 139 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
141 error ("pyobject.uint64: argument must be a Python object"); | 140 error ("pyobject.uint64: argument must be a Python object"); |
142 | 141 |
143 Py_Initialize (); | 142 Py_Initialize (); |
144 | 143 |
145 PyObject *obj = pytave::pyobject_unwrap_object (args(0)); | 144 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
146 if (! obj) | 145 if (! obj) |
147 error ("pyobject.uint64: argument must be a valid Python object"); | 146 error ("pyobject.uint64: argument must be a valid Python object"); |
148 | 147 |
149 octave_uint64 retval; | 148 octave_uint64 retval; |
150 | 149 |
159 catch (boost::python::error_already_set const &) | 158 catch (boost::python::error_already_set const &) |
160 { | 159 { |
161 std::string message = pytave::fetch_exception_message (); | 160 std::string message = pytave::fetch_exception_message (); |
162 error ("pyobject.uint64: %s", message.c_str ()); | 161 error ("pyobject.uint64: %s", message.c_str ()); |
163 } | 162 } |
164 Py_DECREF (obj); | |
165 | 163 |
166 return ovl (retval); | 164 return ovl (retval); |
167 } | 165 } |
168 | 166 |
169 /* | 167 /* |
189 if (args.length () != 1) | 187 if (args.length () != 1) |
190 print_usage (); | 188 print_usage (); |
191 | 189 |
192 Py_Initialize (); | 190 Py_Initialize (); |
193 | 191 |
194 PyObject *obj = pytave::pyobject_unwrap_object (args(0)); | 192 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
195 | 193 |
196 bool retval = (obj && (obj == Py_None)); | 194 return ovl (obj.is_none ()); |
197 Py_XDECREF (obj); | |
198 | |
199 return ovl (retval); | |
200 } | 195 } |
201 | 196 |
202 /* | 197 /* |
203 %!assert (__py_is_none__ (pyobject ())) | 198 %!assert (__py_is_none__ (pyobject ())) |
204 %!assert (__py_is_none__ (pyeval ("None"))) | 199 %!assert (__py_is_none__ (pyeval ("None"))) |
248 // FIXME: PyObject *obj = look up stored pyobject reference (args(0)); | 243 // FIXME: PyObject *obj = look up stored pyobject reference (args(0)); |
249 boost::python::object arg; | 244 boost::python::object arg; |
250 pytave::octvalue_to_pyobj (arg, args(0)); | 245 pytave::octvalue_to_pyobj (arg, args(0)); |
251 PyObject *obj = arg.ptr (); | 246 PyObject *obj = arg.ptr (); |
252 | 247 |
253 PyObject *type = pytave::py_find_type (typestr); | 248 pytave::python_object type = pytave::py_find_type (typestr); |
254 retval(0) = pytave::py_isinstance (obj, type); | 249 retval(0) = pytave::py_isinstance (obj, type); |
255 Py_XDECREF (type); | |
256 } | 250 } |
257 catch (pytave::object_convert_exception const &) | 251 catch (pytave::object_convert_exception const &) |
258 { | 252 { |
259 error ("pyobject.isa: error in return value type conversion"); | 253 error ("pyobject.isa: error in return value type conversion"); |
260 } | 254 } |
358 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 352 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
359 error ("pyobject.char: argument must be a valid Python object"); | 353 error ("pyobject.char: argument must be a valid Python object"); |
360 | 354 |
361 Py_Initialize (); | 355 Py_Initialize (); |
362 | 356 |
363 PyObject *obj = pytave::pyobject_unwrap_object (args(0)); | 357 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
364 if (! obj) | 358 if (! obj) |
365 error ("pyobject.char: argument must be a valid Python object"); | 359 error ("pyobject.char: argument must be a valid Python object"); |
366 | 360 |
367 std::string str; | 361 std::string str; |
368 | 362 |
369 if (PyBytes_Check (obj) || PyUnicode_Check (obj)) | 363 if (PyBytes_Check (obj) || PyUnicode_Check (obj)) |
370 str = pytave::extract_py_str (obj); | 364 str = pytave::extract_py_str (obj); |
371 else if (Py_TYPE (obj)->tp_str != nullptr) | 365 else if (Py_TYPE (obj)->tp_str != nullptr) |
372 { | 366 { |
373 PyObject *s = PyObject_Str (obj); | 367 pytave::python_object s = PyObject_Str (obj); |
374 str = pytave::extract_py_str (s); | 368 str = pytave::extract_py_str (s); |
375 Py_DECREF (s); | |
376 } | 369 } |
377 else | 370 else |
378 { | 371 error ("pyobject.char: cannot convert Python object to string"); |
379 Py_DECREF (obj); | |
380 error ("pyobject.char: cannot convert Python object to string"); | |
381 } | |
382 | |
383 Py_DECREF (obj); | |
384 | 372 |
385 return ovl (str); | 373 return ovl (str); |
386 } | 374 } |
387 | 375 |
388 DEFUN_DLD (__py_struct_from_dict__, args, nargout, | 376 DEFUN_DLD (__py_struct_from_dict__, args, nargout, |