Mercurial > pytave
annotate oct-py-types.cc @ 352:eac35d84ef0d
Convert Octave numeric vectors into Python array.array
* oct-py-types.cc, oct-py-types.h (pytave::make_py_array): New functions to
convert an Octave numeric array into Python array object of appropriate type.
* octave_to_python.cc (pytave::octvalue_to_pyobj): Add numeric vector case.
* pycall.cc: Delete %!tests that relied on conversion to numpy array.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Wed, 17 Aug 2016 21:19:58 -0700 |
parents | 9d95f087e5aa |
children | 8cdc94b83e98 |
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 |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
27 #include <octave/Cell.h> |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
28 #include <octave/oct-map.h> |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
29 #include <octave/quit.h> |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
30 |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
31 #include "exceptions.h" |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
32 #include "oct-py-eval.h" |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
33 #include "oct-py-types.h" |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
34 |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
35 // FIXME: only here to bootstrap nested conversions needed in this file |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
36 #include "octave_to_python.h" |
344
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
37 #include "python_to_octave.h" |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
38 |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
39 namespace pytave |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
40 { |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
41 |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
42 PyObject * |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
43 make_py_bool (bool value) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
44 { |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
45 if (value) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
46 Py_RETURN_TRUE; |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
47 else |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
48 Py_RETURN_FALSE; |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
49 } |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
50 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
51 PyObject * |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
52 make_py_complex (std::complex<double> value) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
53 { |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
54 Py_complex& py_complex_value = reinterpret_cast<Py_complex&> (value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
55 return PyComplex_FromCComplex (py_complex_value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
56 } |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
57 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
58 PyObject * |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
59 make_py_float (double value) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
60 { |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
61 return PyFloat_FromDouble (value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
62 } |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
63 |
340
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
64 bool |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
65 extract_py_bool (PyObject *obj) |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
66 { |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
67 if (! obj) |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
68 throw object_convert_exception ("failed to extract boolean: null object"); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
69 |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
70 if (! PyBool_Check (obj)) |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
71 throw object_convert_exception ("failed to extract boolean: wrong type"); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
72 |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
73 return (obj == Py_True); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
74 } |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
75 |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
76 std::complex<double> |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
77 extract_py_complex (PyObject *obj) |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
78 { |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
79 if (! obj) |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
80 throw object_convert_exception ("failed to extract complex: null object"); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
81 |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
82 if (! PyComplex_Check (obj)) |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
83 throw object_convert_exception ("failed to extract complex: wrong type"); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
84 |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
85 Py_complex value = PyComplex_AsCComplex (obj); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
86 return reinterpret_cast<std::complex<double>&> (value); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
87 } |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
88 |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
89 double |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
90 extract_py_float (PyObject *obj) |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
91 { |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
92 if (! obj) |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
93 throw object_convert_exception ("failed to extract float: null object"); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
94 |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
95 if (! PyFloat_Check (obj)) |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
96 throw object_convert_exception ("failed to extract float: wrong type"); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
97 |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
98 return PyFloat_AsDouble (obj); |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
99 } |
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
100 |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
101 inline PyObject * |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
102 make_py_int (int32_t value) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
103 { |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
104 #if PY_VERSION_HEX >= 0x03000000 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
105 return PyLong_FromLong (value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
106 #else |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
107 return PyInt_FromLong (value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
108 #endif |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
109 } |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
110 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
111 inline PyObject * |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
112 make_py_int (uint32_t value) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
113 { |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
114 return PyLong_FromUnsignedLong (value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
115 } |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
116 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
117 inline PyObject * |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
118 make_py_int (int64_t value) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
119 { |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
120 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG)) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
121 return PyLong_FromLongLong (value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
122 #else |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
123 return PyLong_FromLong (value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
124 #endif |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
125 } |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
126 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
127 inline PyObject * |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
128 make_py_int (uint64_t value) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
129 { |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
130 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG)) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
131 return PyLong_FromUnsignedLongLong (value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
132 #else |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
133 return PyLong_FromUnsignedLong (value); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
134 #endif |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
135 } |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
136 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
137 PyObject * |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
138 make_py_array (const void *data, size_t len, char typecode) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
139 { |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
140 if (! typecode) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
141 throw object_convert_exception ("unable to create array from Octave data"); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
142 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
143 std::string arg { typecode }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
144 PyObject *array = py_call_function ("array.array", ovl (arg)); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
145 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
146 if (len > 0) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
147 { |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
148 // create a byte buffer containing a copy of the array binary data |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
149 const char *cdata = reinterpret_cast<const char *> (data); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
150 PyObject *buf = PyBytes_FromStringAndSize (cdata, len); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
151 if (! buf) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
152 octave_throw_bad_alloc (); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
153 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
154 PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ? |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
155 PyObject_GetAttrString (array, "frombytes") : |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
156 PyObject_GetAttrString (array, "fromstring")); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
157 PyObject *args = PyTuple_Pack (1, buf); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
158 py_call_function (frombytes, args); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
159 Py_DECREF (args); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
160 Py_DECREF (buf); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
161 } |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
162 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
163 return array; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
164 } |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
165 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
166 // Prefer the 'q' and 'Q' typecodes if they are available (if Python 3 and |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
167 // built with support for long long integers) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
168 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
169 #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
|
170 # define ARRAY_INT64_TYPECODE 'q' |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
171 # define ARRAY_UINT64_TYPECODE 'Q' |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
172 #elif (SIZEOF_LONG == 8) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
173 # define ARRAY_INT64_TYPECODE 'l' |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
174 # define ARRAY_UINT64_TYPECODE 'L' |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
175 #else |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
176 # define ARRAY_INT64_TYPECODE 0 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
177 # define ARRAY_UINT64_TYPECODE 0 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
178 #endif |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
179 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
180 template <typename T> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
181 struct py_array_info { }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
182 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
183 template <> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
184 struct py_array_info<octave_int8> { static const char typecode = 'b'; }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
185 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
186 template <> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
187 struct py_array_info<octave_int16> { static const char typecode = 'h'; }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
188 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
189 template <> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
190 struct py_array_info<octave_int32> { static const char typecode = 'i'; }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
191 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
192 template <> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
193 struct py_array_info<octave_int64> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
194 { |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
195 static const char typecode = ARRAY_INT64_TYPECODE; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
196 }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
197 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
198 template <> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
199 struct py_array_info<octave_uint8> { static const char typecode = 'B'; }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
200 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
201 template <> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
202 struct py_array_info<octave_uint16> { static const char typecode = 'H'; }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
203 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
204 template <> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
205 struct py_array_info<octave_uint32> { static const char typecode = 'I'; }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
206 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
207 template <> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
208 struct py_array_info<octave_uint64> { |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
209 static const char typecode = ARRAY_UINT64_TYPECODE; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
210 }; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
211 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
212 PyObject * |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
213 make_py_array (const NDArray& nda) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
214 { |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
215 return make_py_array (nda.data (), nda.numel () * sizeof (double), 'd'); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
216 } |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
217 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
218 PyObject * |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
219 make_py_array (const FloatNDArray& nda) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
220 { |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
221 return make_py_array (nda.data (), nda.numel () * sizeof (float), 'f'); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
222 } |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
223 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
224 template <typename T> |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
225 PyObject * |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
226 make_py_array (const intNDArray<T>& nda) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
227 { |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
228 return make_py_array (nda.data (), nda.numel () * sizeof (T), |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
229 py_array_info<T>::typecode); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
230 } |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
231 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
232 // Instantiate all possible integer array template functions needed |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
233 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
234 template PyObject * make_py_array<octave_int8> (const int8NDArray&); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
235 template PyObject * make_py_array<octave_int16> (const int16NDArray&); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
236 template PyObject * make_py_array<octave_int32> (const int32NDArray&); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
237 template PyObject * make_py_array<octave_int64> (const int64NDArray&); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
238 template PyObject * make_py_array<octave_uint8> (const uint8NDArray&); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
239 template PyObject * make_py_array<octave_uint16> (const uint16NDArray&); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
240 template PyObject * make_py_array<octave_uint32> (const uint32NDArray&); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
241 template PyObject * make_py_array<octave_uint64> (const uint64NDArray&); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
242 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
243 PyObject * |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
244 make_py_numeric_value (const octave_value& value) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
245 { |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
246 if (value.is_scalar_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
247 { |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
248 if (value.is_bool_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
249 return make_py_bool (value.bool_value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
250 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
251 else if (value.is_int8_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
252 return make_py_int (value.int8_scalar_value ().value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
253 else if (value.is_int16_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
254 return make_py_int (value.int16_scalar_value ().value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
255 else if (value.is_int32_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
256 return make_py_int (value.int32_scalar_value ().value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
257 else if (value.is_int64_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
258 return make_py_int (value.int64_scalar_value ().value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
259 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
260 else if (value.is_uint8_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
261 return make_py_int (value.uint8_scalar_value ().value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
262 else if (value.is_uint16_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
263 return make_py_int (value.uint16_scalar_value ().value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
264 else if (value.is_uint32_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
265 return make_py_int (value.uint32_scalar_value ().value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
266 else if (value.is_uint64_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
267 return make_py_int (value.uint64_scalar_value ().value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
268 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
269 else if (value.is_complex_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
270 return make_py_complex (value.complex_value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
271 else if (value.is_float_type ()) |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
272 return make_py_float (value.double_value ()); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
273 } |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
274 |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
275 throw value_convert_exception ("unhandled scalar type"); |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
276 return 0; |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
277 } |
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
278 |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
279 PyObject * |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
280 make_py_array (const octave_value& value) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
281 { |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
282 if (value.is_numeric_type () && ! value.is_complex_type () |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
283 && value.ndims () == 2 && (value.columns () == 1 || value.rows () == 1)) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
284 { |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
285 if (value.is_double_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
286 return make_py_array (value.array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
287 else if (value.is_single_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
288 return make_py_array (value.float_array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
289 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
290 else if (value.is_int8_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
291 return make_py_array (value.int8_array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
292 else if (value.is_int16_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
293 return make_py_array (value.int16_array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
294 else if (value.is_int32_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
295 return make_py_array (value.int32_array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
296 else if (value.is_int64_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
297 return make_py_array (value.int64_array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
298 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
299 else if (value.is_uint8_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
300 return make_py_array (value.uint8_array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
301 else if (value.is_uint16_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
302 return make_py_array (value.uint16_array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
303 else if (value.is_uint32_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
304 return make_py_array (value.uint32_array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
305 else if (value.is_uint64_type ()) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
306 return make_py_array (value.uint64_array_value ()); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
307 } |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
308 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
309 throw value_convert_exception ("unhandled Octave numeric vector type"); |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
310 return 0; |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
311 } |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
312 |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
313 inline PyObject * |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
314 wrap_octvalue_to_pyobj (const octave_value& value) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
315 { |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
316 boost::python::object obj; |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
317 octvalue_to_pyobj (obj, value); |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
318 PyObject *ptr = obj.ptr (); |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
319 Py_INCREF (ptr); |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
320 return ptr; |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
321 } |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
322 |
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 inline octave_value |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
324 wrap_pyobj_to_octvalue (PyObject *obj) |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
325 { |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
326 boost::python::object objref { boost::python::handle<> (boost::python::borrowed (obj)) }; |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
327 octave_value value; |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
328 pyobj_to_octvalue (value, objref); |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
329 return value; |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
330 } |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
331 |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
332 octave_scalar_map |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
333 extract_py_scalar_map (PyObject *obj) |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
334 { |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
335 if (! obj) |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
336 throw object_convert_exception ("failed to extract map: null object"); |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
337 |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
338 if (! PyDict_Check (obj)) |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
339 throw object_convert_exception ("failed to extract map: wrong type"); |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
340 |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
341 octave_scalar_map map; |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
342 |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
343 Py_ssize_t pos = 0; |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
344 PyObject *py_key = 0; |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
345 PyObject *py_value = 0; |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
346 |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
347 while (PyDict_Next (obj, &pos, &py_key, &py_value)) |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
348 { |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
349 if (! PyBytes_Check (py_key) && ! PyUnicode_Check (py_key)) |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
350 throw object_convert_exception ("failed to extract map: bad key type"); |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
351 |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
352 std::string key = extract_py_str (py_key); |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
353 octave_value value = wrap_pyobj_to_octvalue (py_value); |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
354 map.setfield (key, value); |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
355 } |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
356 |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
357 return map; |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
358 } |
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
359 |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
360 PyObject * |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
361 make_py_dict (const octave_scalar_map& map) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
362 { |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
363 PyObject *dict = PyDict_New (); |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
364 if (! dict) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
365 octave_throw_bad_alloc (); |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
366 |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
367 for (auto p = map.begin (); p != map.end (); ++p) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
368 { |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
369 PyObject *key = make_py_str (map.key (p)); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
370 if (! key) |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
371 octave_throw_bad_alloc (); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
372 |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
373 PyObject *item = wrap_octvalue_to_pyobj (map.contents (p)); |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
374 |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
375 if (PyDict_SetItem (dict, key, item) < 0) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
376 throw boost::python::error_already_set (); |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
377 } |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
378 |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
379 return dict; |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
380 } |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
381 |
328
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
382 int64_t |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
383 extract_py_int64 (PyObject *obj) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
384 { |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
385 if (! obj) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
386 throw object_convert_exception ("failed to extract integer: null object"); |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
387 |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
388 if (PyLong_Check (obj)) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
389 return PyLong_AsLong (obj); |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
390 #if PY_VERSION_HEX < 0x03000000 |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
391 else if (PyInt_Check (obj)) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
392 return PyInt_AsLong (obj); |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
393 #endif |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
394 else |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
395 throw object_convert_exception ("failed to extract integer: wrong type"); |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
396 |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
397 return 0; |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
398 } |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
399 |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
400 PyObject * |
342
6bd8f5e3542a
Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents:
340
diff
changeset
|
401 make_py_tuple (const Cell& cell) |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
402 { |
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
403 if (! (cell.is_empty () || cell.is_vector ())) |
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
404 throw value_convert_exception ( |
342
6bd8f5e3542a
Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents:
340
diff
changeset
|
405 "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
|
406 |
342
6bd8f5e3542a
Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents:
340
diff
changeset
|
407 octave_idx_type size = cell.numel (); |
6bd8f5e3542a
Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents:
340
diff
changeset
|
408 PyObject *tuple = PyTuple_New (size); |
6bd8f5e3542a
Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents:
340
diff
changeset
|
409 if (! tuple) |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
410 octave_throw_bad_alloc (); |
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
411 |
342
6bd8f5e3542a
Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents:
340
diff
changeset
|
412 for (octave_idx_type i = 0; i < size; ++i) |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
413 { |
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
414 PyObject *item = wrap_octvalue_to_pyobj (cell.xelem (i)); |
342
6bd8f5e3542a
Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents:
340
diff
changeset
|
415 PyTuple_SET_ITEM (tuple, i, item); |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
416 } |
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
417 |
342
6bd8f5e3542a
Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents:
340
diff
changeset
|
418 return tuple; |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
419 } |
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
420 |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
421 std::string |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
422 extract_py_str (PyObject *obj) |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
423 { |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
424 std::string retval; |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
425 |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
426 if (! obj) |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
427 throw object_convert_exception ("failed to extract string: null object"); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
428 if (PyBytes_Check (obj)) |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
429 { |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
430 retval.assign (PyBytes_AsString (obj), PyBytes_Size (obj)); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
431 } |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
432 else if (PyUnicode_Check (obj)) |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
433 { |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
434 bool ok = false; |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
435 PyObject *enc = PyUnicode_AsUTF8String (obj); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
436 if (enc) |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
437 { |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
438 if (PyBytes_Check (enc)) |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
439 { |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
440 ok = true; |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
441 retval.assign (PyBytes_AsString (enc), PyBytes_Size (enc)); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
442 } |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
443 Py_DECREF (enc); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
444 } |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
445 if (! ok) |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
446 throw object_convert_exception ("failed to extract string: UTF-8 error"); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
447 } |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
448 else |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
449 throw object_convert_exception ("failed to extract string: wrong type"); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
450 |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
451 return retval; |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
452 } |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
453 |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
454 PyObject * |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
455 make_py_str (const std::string& str) |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
456 { |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
457 #if PY_VERSION_HEX >= 0x03000000 |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
458 return PyUnicode_FromStringAndSize (str.data (), str.size ()); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
459 #else |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
460 return PyString_FromStringAndSize (str.data (), str.size ()); |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
461 #endif |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
462 } |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
463 |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
464 } |