comparison oct-py-types.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 f833e29b2c12
comparison
equal deleted inserted replaced
403:3644df6564bc 404:aef165ff92b0
29 #include <octave/oct-map.h> 29 #include <octave/oct-map.h>
30 #include <octave/quit.h> 30 #include <octave/quit.h>
31 31
32 #include "exceptions.h" 32 #include "exceptions.h"
33 #include "oct-py-eval.h" 33 #include "oct-py-eval.h"
34 #include "oct-py-object.h"
34 #include "oct-py-types.h" 35 #include "oct-py-types.h"
35 36
36 // FIXME: only here to bootstrap nested conversions needed in this file 37 // FIXME: only here to bootstrap nested conversions needed in this file
37 #include "octave_to_python.h" 38 #include "octave_to_python.h"
38 #include "python_to_octave.h" 39 #include "python_to_octave.h"
141 if (! typecode) 142 if (! typecode)
142 throw object_convert_exception 143 throw object_convert_exception
143 ("unable to create array from Octave data"); 144 ("unable to create array from Octave data");
144 145
145 std::string arg { typecode }; 146 std::string arg { typecode };
146 PyObject *array = py_call_function ("array.array", ovl (arg)); 147 python_object array = py_call_function ("array.array", ovl (arg));
147 148
148 if (len > 0) 149 if (len > 0)
149 { 150 {
150 // create a byte buffer containing a copy of the array binary data 151 // create a byte buffer containing a copy of the array binary data
151 const char *cdata = reinterpret_cast<const char *> (data); 152 const char *cdata = reinterpret_cast<const char *> (data);
152 PyObject *buf = PyBytes_FromStringAndSize (cdata, len); 153 python_object buf = PyBytes_FromStringAndSize (cdata, len);
153 if (! buf) 154 if (! buf)
154 octave_throw_bad_alloc (); 155 octave_throw_bad_alloc ();
155 156
156 PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ? 157 PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ?
157 PyObject_GetAttrString (array, "frombytes") : 158 PyObject_GetAttrString (array, "frombytes") :
158 PyObject_GetAttrString (array, "fromstring")); 159 PyObject_GetAttrString (array, "fromstring"));
159 PyObject *args = PyTuple_Pack (1, buf); 160 python_object args = PyTuple_Pack (1, buf.release ());
160 py_call_function (frombytes, args); 161 py_call_function (frombytes, args);
161 Py_DECREF (args); 162 }
162 Py_DECREF (buf); 163
163 } 164 return array.release ();
164
165 return array;
166 } 165 }
167 166
168 // Prefer the 'q' and 'Q' typecodes if they are available (if Python 3 and 167 // Prefer the 'q' and 'Q' typecodes if they are available (if Python 3 and
169 // built with support for long long integers) 168 // built with support for long long integers)
170 169
483 { 482 {
484 retval.assign (PyBytes_AsString (obj), PyBytes_Size (obj)); 483 retval.assign (PyBytes_AsString (obj), PyBytes_Size (obj));
485 } 484 }
486 else if (PyUnicode_Check (obj)) 485 else if (PyUnicode_Check (obj))
487 { 486 {
488 bool ok = false; 487 python_object enc = PyUnicode_AsUTF8String (obj);
489 PyObject *enc = PyUnicode_AsUTF8String (obj); 488 if (enc && PyBytes_Check (enc))
490 if (enc) 489 retval.assign (PyBytes_AsString (enc), PyBytes_Size (enc));
491 { 490 else
492 if (PyBytes_Check (enc))
493 {
494 ok = true;
495 retval.assign (PyBytes_AsString (enc), PyBytes_Size (enc));
496 }
497 Py_DECREF (enc);
498 }
499 if (! ok)
500 throw object_convert_exception 491 throw object_convert_exception
501 ("failed to extract string: UTF-8 error"); 492 ("failed to extract string: UTF-8 error");
502 } 493 }
503 else 494 else
504 throw object_convert_exception ("failed to extract string: wrong type"); 495 throw object_convert_exception ("failed to extract string: wrong type");