annotate oct-py-types.cc @ 411:3613ffbd52b2

Overhaul implicit conversion of arguments and return values * oct-py-types.cc, oct-py-types.h (pytave::py_implicitly_convert_argument, pytave::py_implicitly_convert_return_value): New functions. * __py_struct_from_dict__.cc, oct-py-eval.cc, pycall.cc, pyeval.cc, pyexec.cc: Use them instead of legacy conversion functions. Add necessary #includes, remove #includes of legacy header files. * @pyobject/subsasgn.m, @pyobject/subsref.m: Change %!tests that depend on NumPy implicit conversion into %!xtests. * octave_to_python.cc, octave_to_python.h, python_to_octave.cc, python_to_octave.h: Delete, no longer used. * Makefile.am (COMMON_SOURCE_FILES, PYTAVE_HEADER_FILES): Remove the files.
author Mike Miller <mtmiller@octave.org>
date Wed, 03 May 2017 16:30:45 -0700
parents f833e29b2c12
children 9bf8ba050122
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
1 /*
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
2
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
3 Copyright (C) 2016 Mike Miller
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
4
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
5 This file is part of Pytave.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
6
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
7 Pytave is free software; you can redistribute it and/or modify it
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
10 option) any later version.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
11
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
12 Pytave is distributed in the hope that it will be useful, but WITHOUT
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
15 for more details.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
16
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
18 along with Pytave; see the file COPYING. If not, see
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
20
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
21 */
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
22
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
23 #if defined (HAVE_CONFIG_H)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
24 # include <config.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
25 #endif
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
26
355
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
27 #include <limits>
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
28 #include <octave/Cell.h>
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
29 #include <octave/oct-map.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
30 #include <octave/quit.h>
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
31 #include <octave/ov-null-mat.h>
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
32
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
33 #include "exceptions.h"
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
34 #include "oct-py-eval.h"
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
35 #include "oct-py-object.h"
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
36 #include "oct-py-types.h"
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
37 #include "oct-py-util.h"
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
38
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
39 // FIXME: only here for exception types still used in this file
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
40 #include <boost/python/errors.hpp>
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
41
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
42 namespace pytave
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
43 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
44
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
45 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
46 make_py_bool (bool value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
47 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
48 if (value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
49 Py_RETURN_TRUE;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
50 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
51 Py_RETURN_FALSE;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
52 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
53
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
54 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
55 make_py_complex (std::complex<double> value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
56 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
57 Py_complex& py_complex_value = reinterpret_cast<Py_complex&> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
58 return PyComplex_FromCComplex (py_complex_value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
59 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
60
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
61 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
62 make_py_float (double value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
63 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
64 return PyFloat_FromDouble (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
65 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
66
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
67 bool
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
68 extract_py_bool (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
69 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
70 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
71 throw object_convert_exception ("failed to extract boolean: null object");
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
72
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
73 if (! PyBool_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
74 throw object_convert_exception ("failed to extract boolean: wrong type");
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
75
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
76 return (obj == Py_True);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
77 }
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
78
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
79 std::complex<double>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
80 extract_py_complex (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
81 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
82 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
83 throw object_convert_exception ("failed to extract complex: null object");
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
84
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
85 if (! PyComplex_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
86 throw object_convert_exception ("failed to extract complex: wrong type");
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
87
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
88 Py_complex value = PyComplex_AsCComplex (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
89 return reinterpret_cast<std::complex<double>&> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
90 }
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
91
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
92 double
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
93 extract_py_float (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
94 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
95 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
96 throw object_convert_exception ("failed to extract float: null object");
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
97
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
98 if (! PyFloat_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
99 throw object_convert_exception ("failed to extract float: wrong type");
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
100
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
101 return PyFloat_AsDouble (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
102 }
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
103
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
104 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
105 make_py_int (int32_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
106 {
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
107 #if PY_VERSION_HEX >= 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
108 return PyLong_FromLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
109 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
110 return PyInt_FromLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
111 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
112 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
113
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
114 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
115 make_py_int (uint32_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
116 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
117 return PyLong_FromUnsignedLong (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
118 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
119
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
120 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
121 make_py_int (int64_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
122 {
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
123 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG))
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
124 return PyLong_FromLongLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
125 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
126 return PyLong_FromLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
127 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
128 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
129
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
130 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
131 make_py_int (uint64_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
132 {
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
133 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG))
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
134 return PyLong_FromUnsignedLongLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
135 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
136 return PyLong_FromUnsignedLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
137 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
138 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
139
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
140 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
141 make_py_array (const void *data, size_t len, char typecode)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
142 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
143 if (! typecode)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
144 throw object_convert_exception
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
145 ("unable to create array from Octave data");
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
146
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
147 std::string arg { typecode };
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
148 python_object array = py_call_function ("array.array", ovl (arg));
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
149
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
150 if (len > 0)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
151 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
152 // create a byte buffer containing a copy of the array binary data
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
153 const char *cdata = reinterpret_cast<const char *> (data);
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
154 python_object buf = PyBytes_FromStringAndSize (cdata, len);
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
155 if (! buf)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
156 octave_throw_bad_alloc ();
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
157
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
158 PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ?
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
159 PyObject_GetAttrString (array, "frombytes") :
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
160 PyObject_GetAttrString (array, "fromstring"));
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
161 python_object args = PyTuple_Pack (1, buf.release ());
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
162 py_call_function (frombytes, args);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
163 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
164
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
165 return array.release ();
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
166 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
167
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
168 // Prefer the 'q' and 'Q' typecodes if they are available (if Python 3 and
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
169 // built with support for long long integers)
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
170
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
171 #if (PY_VERSION_HEX >= 0x03000000) && defined (HAVE_LONG_LONG)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
172 # define ARRAY_INT64_TYPECODE 'q'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
173 # define ARRAY_UINT64_TYPECODE 'Q'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
174 #elif (SIZEOF_LONG == 8)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
175 # define ARRAY_INT64_TYPECODE 'l'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
176 # define ARRAY_UINT64_TYPECODE 'L'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
177 #else
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
178 # define ARRAY_INT64_TYPECODE 0
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
179 # define ARRAY_UINT64_TYPECODE 0
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
180 #endif
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
181
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
182 template <typename T>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
183 struct py_array_info { };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
184
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
185 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
186 struct py_array_info<octave_int8> { static const char typecode = 'b'; };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
187
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
188 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
189 struct py_array_info<octave_int16> { static const char typecode = 'h'; };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
190
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
191 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
192 struct py_array_info<octave_int32> { static const char typecode = 'i'; };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
193
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
194 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
195 struct py_array_info<octave_int64>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
196 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
197 static const char typecode = ARRAY_INT64_TYPECODE;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
198 };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
199
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
200 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
201 struct py_array_info<octave_uint8> { static const char typecode = 'B'; };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
202
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
203 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
204 struct py_array_info<octave_uint16> { static const char typecode = 'H'; };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
205
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
206 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
207 struct py_array_info<octave_uint32> { static const char typecode = 'I'; };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
208
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
209 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
210 struct py_array_info<octave_uint64> {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
211 static const char typecode = ARRAY_UINT64_TYPECODE;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
212 };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
213
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
214 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
215 make_py_array (const NDArray& nda)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
216 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
217 return make_py_array (nda.data (), nda.numel () * sizeof (double), 'd');
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
218 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
219
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
220 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
221 make_py_array (const FloatNDArray& nda)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
222 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
223 return make_py_array (nda.data (), nda.numel () * sizeof (float), 'f');
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
224 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
225
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
226 template <typename T>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
227 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
228 make_py_array (const intNDArray<T>& nda)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
229 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
230 return make_py_array (nda.data (), nda.numel () * sizeof (T),
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
231 py_array_info<T>::typecode);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
232 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
233
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
234 // Instantiate all possible integer array template functions needed
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
235
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
236 template PyObject * make_py_array<octave_int8> (const int8NDArray&);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
237 template PyObject * make_py_array<octave_int16> (const int16NDArray&);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
238 template PyObject * make_py_array<octave_int32> (const int32NDArray&);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
239 template PyObject * make_py_array<octave_int64> (const int64NDArray&);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
240 template PyObject * make_py_array<octave_uint8> (const uint8NDArray&);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
241 template PyObject * make_py_array<octave_uint16> (const uint16NDArray&);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
242 template PyObject * make_py_array<octave_uint32> (const uint32NDArray&);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
243 template PyObject * make_py_array<octave_uint64> (const uint64NDArray&);
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
244
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
245 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
246 make_py_numeric_value (const octave_value& value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
247 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
248 if (value.is_scalar_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
249 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
250 if (value.is_bool_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
251 return make_py_bool (value.bool_value ());
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
252
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
253 else if (value.is_int8_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
254 return make_py_int (value.int8_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
255 else if (value.is_int16_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
256 return make_py_int (value.int16_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
257 else if (value.is_int32_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
258 return make_py_int (value.int32_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
259 else if (value.is_int64_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
260 return make_py_int (value.int64_scalar_value ().value ());
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
261
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
262 else if (value.is_uint8_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
263 return make_py_int (value.uint8_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
264 else if (value.is_uint16_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
265 return make_py_int (value.uint16_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
266 else if (value.is_uint32_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
267 return make_py_int (value.uint32_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
268 else if (value.is_uint64_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
269 return make_py_int (value.uint64_scalar_value ().value ());
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
270
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
271 else if (value.is_complex_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
272 return make_py_complex (value.complex_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
273 else if (value.is_float_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
274 return make_py_float (value.double_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
275 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
276
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
277 throw value_convert_exception ("unhandled scalar type");
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
278 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
279 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
280
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
281 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
282 make_py_array (const octave_value& value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
283 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
284 if (value.is_numeric_type () && ! value.is_complex_type ()
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
285 && value.ndims () == 2 && (value.columns () <= 1 || value.rows () <= 1))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
286 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
287 if (value.is_double_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
288 return make_py_array (value.array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
289 else if (value.is_single_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
290 return make_py_array (value.float_array_value ());
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
291
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
292 else if (value.is_int8_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
293 return make_py_array (value.int8_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
294 else if (value.is_int16_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
295 return make_py_array (value.int16_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
296 else if (value.is_int32_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
297 return make_py_array (value.int32_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
298 else if (value.is_int64_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
299 return make_py_array (value.int64_array_value ());
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
300
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
301 else if (value.is_uint8_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
302 return make_py_array (value.uint8_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
303 else if (value.is_uint16_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
304 return make_py_array (value.uint16_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
305 else if (value.is_uint32_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
306 return make_py_array (value.uint32_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
307 else if (value.is_uint64_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
308 return make_py_array (value.uint64_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
309 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
310
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
311 throw value_convert_exception ("unhandled Octave numeric vector type");
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
312 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
313 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
314
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
315 octave_scalar_map
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
316 extract_py_scalar_map (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
317 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
318 if (! obj)
408
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
319 error ("unable to convert to an Octave struct, invalid Python object");
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
320
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
321 if (! PyDict_Check (obj))
408
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
322 error ("unable to convert to an Octave struct, must be a Python dict");
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
323
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
324 octave_scalar_map map;
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
325
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
326 Py_ssize_t pos = 0;
403
3644df6564bc maint: use C++11 nullptr rather than 0 or NULL
Mike Miller <mtmiller@octave.org>
parents: 402
diff changeset
327 PyObject *py_key = nullptr;
3644df6564bc maint: use C++11 nullptr rather than 0 or NULL
Mike Miller <mtmiller@octave.org>
parents: 402
diff changeset
328 PyObject *py_value = nullptr;
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
329
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
330 while (PyDict_Next (obj, &pos, &py_key, &py_value))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
331 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
332 if (! PyBytes_Check (py_key) && ! PyUnicode_Check (py_key))
408
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
333 error ("unable to convert Python dict to Octave struct, "
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
334 "all keys in the dict must be strings");
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
335
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
336 std::string key = extract_py_str (py_key);
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
337 octave_value value = py_implicitly_convert_return_value (py_value);
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
338 map.setfield (key, value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
339 }
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
340
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
341 return map;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
342 }
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
343
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
344 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
345 make_py_dict (const octave_scalar_map& map)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
346 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
347 PyObject *dict = PyDict_New ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
348 if (! dict)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
349 octave_throw_bad_alloc ();
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
350
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
351 for (auto p = map.begin (); p != map.end (); ++p)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
352 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
353 PyObject *key = make_py_str (map.key (p));
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
354 if (! key)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
355 octave_throw_bad_alloc ();
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
356
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
357 PyObject *item = py_implicitly_convert_argument (map.contents (p));
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
358
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
359 if (PyDict_SetItem (dict, key, item) < 0)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
360 throw boost::python::error_already_set ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
361 }
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
362
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
363 return dict;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
364 }
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
365
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
366 int64_t
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
367 extract_py_int64 (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
368 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
369 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
370 throw object_convert_exception ("failed to extract integer: null object");
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
371
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
372 if (PyLong_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
373 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
374 int overflow = 0;
355
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
375 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG == 8))
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
376 PY_LONG_LONG value = PyLong_AsLongLongAndOverflow (obj, &overflow);
355
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
377 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
378 long value = PyLong_AsLongAndOverflow (obj, &overflow);
355
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
379 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
380 if (overflow)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
381 if (overflow > 0)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
382 value = std::numeric_limits<int64_t>::max ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
383 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
384 value = std::numeric_limits<int64_t>::min ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
385 return static_cast<int64_t> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
386 }
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
387 #if PY_VERSION_HEX < 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
388 else if (PyInt_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
389 return PyInt_AsLong (obj);
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
390 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
391 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
392 throw object_convert_exception ("failed to extract integer: wrong type");
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
393
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
394 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
395 }
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
396
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
397 uint64_t
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
398 extract_py_uint64 (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
399 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
400 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
401 throw object_convert_exception ("failed to extract integer: null object");
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
402
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
403 if (PyLong_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
404 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
405 // FIXME: if (value < 0), may be very implementation dependent
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
406 if (Py_SIZE (obj) < 0)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
407 return 0;
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
408
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
409 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG == 8))
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
410 unsigned PY_LONG_LONG value = PyLong_AsUnsignedLongLong (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
411 bool overflow = (value == static_cast<unsigned PY_LONG_LONG> (-1));
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
412 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
413 unsigned long value = PyLong_AsUnsignedLong (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
414 bool overflow = (value == static_cast<unsigned long> (-1));
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
415 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
416 if (overflow)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
417 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
418 value = std::numeric_limits<uint64_t>::max ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
419 PyErr_Clear ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
420 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
421
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
422 return static_cast<uint64_t> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
423 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
424 #if PY_VERSION_HEX < 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
425 else if (PyInt_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
426 return static_cast<uint64_t> (PyInt_AsLong (obj));
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
427 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
428 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
429 throw object_convert_exception ("failed to extract integer: wrong type");
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
430
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
431 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
432 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
433
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
434 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
435 make_py_tuple (const Cell& cell)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
436 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
437 if (! (cell.is_empty () || cell.is_vector ()))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
438 throw value_convert_exception (
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
439 "unable to convert multidimensional cell array into Python tuple");
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
440
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
441 octave_idx_type size = cell.numel ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
442 PyObject *tuple = PyTuple_New (size);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
443 if (! tuple)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
444 octave_throw_bad_alloc ();
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
445
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
446 for (octave_idx_type i = 0; i < size; ++i)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
447 {
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
448 PyObject *item = py_implicitly_convert_argument (cell.xelem (i));
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
449 PyTuple_SET_ITEM (tuple, i, item);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
450 }
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
451
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
452 return tuple;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
453 }
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
454
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
455 std::string
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
456 extract_py_str (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
457 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
458 std::string retval;
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
459
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
460 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
461 throw object_convert_exception ("failed to extract string: null object");
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
462 if (PyBytes_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
463 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
464 retval.assign (PyBytes_AsString (obj), PyBytes_Size (obj));
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
465 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
466 else if (PyUnicode_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
467 {
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
468 python_object enc = PyUnicode_AsUTF8String (obj);
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
469 if (enc && PyBytes_Check (enc))
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
470 retval.assign (PyBytes_AsString (enc), PyBytes_Size (enc));
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
471 else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
472 throw object_convert_exception
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
473 ("failed to extract string: UTF-8 error");
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
474 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
475 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
476 throw object_convert_exception ("failed to extract string: wrong type");
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
477
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
478 return retval;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
479 }
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
480
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
481 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
482 make_py_str (const std::string& str)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
483 {
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
484 #if PY_VERSION_HEX >= 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
485 return PyUnicode_FromStringAndSize (str.data (), str.size ());
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
486 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
487 return PyString_FromStringAndSize (str.data (), str.size ());
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
488 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
489 }
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
490
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
491 PyObject *
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
492 py_implicitly_convert_argument (const octave_value& value)
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
493 {
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
494 if (value.is_object () && value.class_name () == "pyobject")
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
495 return pyobject_unwrap_object (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
496 else if (value.is_string () && value.rows () > 1)
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
497 error ("unable to convert multirow char array to a Python object");
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
498 else if (value.is_string ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
499 return make_py_str (value.string_value ());
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
500 else if (value.is_scalar_type ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
501 return make_py_numeric_value (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
502 else if (value.is_cell ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
503 return make_py_tuple (value.cell_value ());
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
504 else if (value.is_numeric_type () && value.ndims () == 2
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
505 && (value.columns () <= 1 || value.rows () <= 1))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
506 return make_py_array (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
507 else if (value.is_map () && value.numel () == 1)
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
508 return make_py_dict (value.scalar_map_value ());
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
509 else
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
510 error ("unable to convert unhandled Octave type to a Python object");
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
511
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
512 return nullptr;
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
513 }
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
514
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
515 octave_value
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
516 py_implicitly_convert_return_value (PyObject *obj)
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
517 {
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
518 if (PyBool_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
519 return octave_value {extract_py_bool (obj)};
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
520 #if PY_VERSION_HEX < 0x03000000
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
521 else if (PyInt_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
522 return octave_value {octave_int64 (extract_py_int64 (obj))};
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
523 #endif
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
524 else if (PyComplex_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
525 return octave_value {extract_py_complex (obj)};
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
526 else if (PyFloat_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
527 return octave_value {extract_py_float (obj)};
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
528 else
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
529 return pyobject_wrap_object (obj);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
530 }
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
531
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
532 }