Mercurial > pytave
annotate oct-py-types.cc @ 404:aef165ff92b0
Adopt pytave::python_object where objects are local or created and returned
* __py_struct_from_dict__.cc, exceptions.cc, oct-py-eval.cc, oct-py-types.cc,
pycall.cc: Use pytave::python_object in place of PyObject where objects are
created locally and destroyed or returned to the caller.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Fri, 28 Apr 2017 16:21:39 -0700 |
parents | 3644df6564bc |
children | f833e29b2c12 |
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> |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
31 |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
32 #include "exceptions.h" |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
33 #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
|
34 #include "oct-py-object.h" |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
35 #include "oct-py-types.h" |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
36 |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
37 // 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
|
38 #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
|
39 #include "python_to_octave.h" |
320
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 namespace pytave |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
42 { |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
43 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
44 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
45 make_py_bool (bool value) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
46 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
47 if (value) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
48 Py_RETURN_TRUE; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
49 else |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
50 Py_RETURN_FALSE; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
51 } |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
52 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
53 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
54 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
|
55 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
56 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
|
57 return PyComplex_FromCComplex (py_complex_value); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
58 } |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
59 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
60 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
61 make_py_float (double value) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
62 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
63 return PyFloat_FromDouble (value); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
64 } |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
65 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
66 bool |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
67 extract_py_bool (PyObject *obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
68 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
69 if (! obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
70 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
|
71 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
72 if (! PyBool_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
73 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
|
74 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
75 return (obj == Py_True); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
76 } |
340
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
77 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
78 std::complex<double> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
79 extract_py_complex (PyObject *obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
80 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
81 if (! obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
82 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
|
83 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
84 if (! PyComplex_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
85 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
|
86 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
87 Py_complex value = PyComplex_AsCComplex (obj); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
88 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
|
89 } |
340
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
90 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
91 double |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
92 extract_py_float (PyObject *obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
93 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
94 if (! obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
95 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
|
96 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
97 if (! PyFloat_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
98 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
|
99 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
100 return PyFloat_AsDouble (obj); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
101 } |
340
06b8aeea456f
Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents:
337
diff
changeset
|
102 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
103 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
104 make_py_int (int32_t value) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
105 { |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
106 #if PY_VERSION_HEX >= 0x03000000 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
107 return PyLong_FromLong (value); |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
108 #else |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
109 return PyInt_FromLong (value); |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
110 #endif |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
111 } |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
112 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
113 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
114 make_py_int (uint32_t value) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
115 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
116 return PyLong_FromUnsignedLong (value); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
117 } |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
118 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
119 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
120 make_py_int (int64_t value) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
121 { |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
122 #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
|
123 return PyLong_FromLongLong (value); |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
124 #else |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
125 return PyLong_FromLong (value); |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
126 #endif |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
127 } |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
128 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
129 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
130 make_py_int (uint64_t value) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
131 { |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
132 #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
|
133 return PyLong_FromUnsignedLongLong (value); |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
134 #else |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
135 return PyLong_FromUnsignedLong (value); |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
136 #endif |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
137 } |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
138 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
139 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
140 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
|
141 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
142 if (! typecode) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
143 throw object_convert_exception |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
144 ("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
|
145 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
146 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
|
147 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
|
148 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
149 if (len > 0) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
150 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
151 // 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
|
152 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
|
153 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
|
154 if (! buf) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
155 octave_throw_bad_alloc (); |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
156 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
157 PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ? |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
158 PyObject_GetAttrString (array, "frombytes") : |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
159 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
|
160 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
|
161 py_call_function (frombytes, args); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
162 } |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
163 |
404
aef165ff92b0
Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents:
403
diff
changeset
|
164 return array.release (); |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
165 } |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
166 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
167 // 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
|
168 // 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
|
169 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
170 #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
|
171 # define ARRAY_INT64_TYPECODE 'q' |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
172 # define ARRAY_UINT64_TYPECODE 'Q' |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
173 #elif (SIZEOF_LONG == 8) |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
174 # define ARRAY_INT64_TYPECODE 'l' |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
175 # define ARRAY_UINT64_TYPECODE 'L' |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
176 #else |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
177 # define ARRAY_INT64_TYPECODE 0 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
178 # define ARRAY_UINT64_TYPECODE 0 |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
179 #endif |
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
180 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
181 template <typename T> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
182 struct py_array_info { }; |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
183 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
184 template <> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
185 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
|
186 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
187 template <> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
188 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
|
189 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
190 template <> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
191 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
|
192 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
193 template <> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
194 struct py_array_info<octave_int64> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
195 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
196 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
|
197 }; |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
198 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
199 template <> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
200 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
|
201 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
202 template <> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
203 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
|
204 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
205 template <> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
206 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
|
207 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
208 template <> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
209 struct py_array_info<octave_uint64> { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
210 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
|
211 }; |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
212 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
213 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
214 make_py_array (const NDArray& nda) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
215 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
216 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
|
217 } |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
218 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
219 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
220 make_py_array (const FloatNDArray& nda) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
221 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
222 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
|
223 } |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
224 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
225 template <typename T> |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
226 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
227 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
|
228 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
229 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
|
230 py_array_info<T>::typecode); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
231 } |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
232 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
233 // 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
|
234 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
235 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 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
|
243 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
244 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
245 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
|
246 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
247 if (value.is_scalar_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
248 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
249 if (value.is_bool_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
250 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
|
251 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
252 else if (value.is_int8_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
253 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
|
254 else if (value.is_int16_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
255 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
|
256 else if (value.is_int32_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
257 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
|
258 else if (value.is_int64_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
259 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
|
260 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
261 else if (value.is_uint8_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
262 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
|
263 else if (value.is_uint16_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
264 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
|
265 else if (value.is_uint32_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
266 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
|
267 else if (value.is_uint64_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
268 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
|
269 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
270 else if (value.is_complex_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
271 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
|
272 else if (value.is_float_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
273 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
|
274 } |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
275 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
276 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
|
277 return 0; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
278 } |
337
d3d355dc44ad
Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents:
336
diff
changeset
|
279 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
280 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
281 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
|
282 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
283 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
|
284 && 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
|
285 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
286 if (value.is_double_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
287 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
|
288 else if (value.is_single_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
289 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
|
290 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
291 else if (value.is_int8_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
292 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
|
293 else if (value.is_int16_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
294 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
|
295 else if (value.is_int32_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
296 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
|
297 else if (value.is_int64_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
298 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
|
299 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
300 else if (value.is_uint8_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
301 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
|
302 else if (value.is_uint16_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
303 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
|
304 else if (value.is_uint32_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
305 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
|
306 else if (value.is_uint64_type ()) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
307 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
|
308 } |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
309 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
310 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
|
311 return 0; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
312 } |
352
eac35d84ef0d
Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents:
344
diff
changeset
|
313 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
314 inline PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
315 wrap_octvalue_to_pyobj (const octave_value& value) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
316 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
317 boost::python::object obj; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
318 octvalue_to_pyobj (obj, value); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
319 PyObject *ptr = obj.ptr (); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
320 Py_INCREF (ptr); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
321 return ptr; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
322 } |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
323 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
324 inline octave_value |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
325 wrap_pyobj_to_octvalue (PyObject *obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
326 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
327 boost::python::object objref |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
328 { boost::python::handle<> (boost::python::borrowed (obj)) }; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
329 octave_value value; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
330 pyobj_to_octvalue (value, objref); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
331 return value; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
332 } |
344
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
333 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
334 octave_scalar_map |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
335 extract_py_scalar_map (PyObject *obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
336 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
337 if (! obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
338 throw object_convert_exception ("failed to extract map: null 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
|
339 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
340 if (! PyDict_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
341 throw object_convert_exception ("failed to extract map: wrong type"); |
344
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
342 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
343 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
|
344 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
345 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
|
346 PyObject *py_key = nullptr; |
3644df6564bc
maint: use C++11 nullptr rather than 0 or NULL
Mike Miller <mtmiller@octave.org>
parents:
402
diff
changeset
|
347 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
|
348 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
349 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
|
350 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
351 if (! PyBytes_Check (py_key) && ! PyUnicode_Check (py_key)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
352 throw object_convert_exception |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
353 ("failed to extract map: bad key type"); |
344
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
354 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
355 std::string key = extract_py_str (py_key); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
356 octave_value value = wrap_pyobj_to_octvalue (py_value); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
357 map.setfield (key, value); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
358 } |
344
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
359 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
360 return map; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
361 } |
344
9d95f087e5aa
Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents:
342
diff
changeset
|
362 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
363 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
364 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
|
365 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
366 PyObject *dict = PyDict_New (); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
367 if (! dict) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
368 octave_throw_bad_alloc (); |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
369 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
370 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
|
371 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
372 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
|
373 if (! key) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
374 octave_throw_bad_alloc (); |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
375 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
376 PyObject *item = wrap_octvalue_to_pyobj (map.contents (p)); |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
377 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
378 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
|
379 throw boost::python::error_already_set (); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
380 } |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
381 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
382 return dict; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
383 } |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
384 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
385 int64_t |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
386 extract_py_int64 (PyObject *obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
387 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
388 if (! obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
389 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
|
390 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
391 if (PyLong_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
392 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
393 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
|
394 #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
|
395 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
|
396 #else |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
397 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
|
398 #endif |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
399 if (overflow) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
400 if (overflow > 0) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
401 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
|
402 else |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
403 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
|
404 return static_cast<int64_t> (value); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
405 } |
328
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
406 #if PY_VERSION_HEX < 0x03000000 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
407 else if (PyInt_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
408 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
|
409 #endif |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
410 else |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
411 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
|
412 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
413 return 0; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
414 } |
328
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
325
diff
changeset
|
415 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
416 uint64_t |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
417 extract_py_uint64 (PyObject *obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
418 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
419 if (! obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
420 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
|
421 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
422 if (PyLong_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
423 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
424 // 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
|
425 if (Py_SIZE (obj) < 0) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
426 return 0; |
374
d362cdd1ddeb
pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents:
361
diff
changeset
|
427 |
d362cdd1ddeb
pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents:
361
diff
changeset
|
428 #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
|
429 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
|
430 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
|
431 #else |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
432 unsigned long value = PyLong_AsUnsignedLong (obj); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
433 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
|
434 #endif |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
435 if (overflow) |
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 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
|
438 PyErr_Clear (); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
439 } |
374
d362cdd1ddeb
pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents:
361
diff
changeset
|
440 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
441 return static_cast<uint64_t> (value); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
442 } |
374
d362cdd1ddeb
pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents:
361
diff
changeset
|
443 #if PY_VERSION_HEX < 0x03000000 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
444 else if (PyInt_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
445 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
|
446 #endif |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
447 else |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
448 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
|
449 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
450 return 0; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
451 } |
374
d362cdd1ddeb
pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents:
361
diff
changeset
|
452 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
453 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
454 make_py_tuple (const Cell& cell) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
455 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
456 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
|
457 throw value_convert_exception ( |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
458 "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
|
459 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
460 octave_idx_type size = cell.numel (); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
461 PyObject *tuple = PyTuple_New (size); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
462 if (! tuple) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
463 octave_throw_bad_alloc (); |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
464 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
465 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
|
466 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
467 PyObject *item = wrap_octvalue_to_pyobj (cell.xelem (i)); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
468 PyTuple_SET_ITEM (tuple, i, item); |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
469 } |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
470 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
471 return tuple; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
472 } |
336
c081e30c2f64
Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
473 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
474 std::string |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
475 extract_py_str (PyObject *obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
476 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
477 std::string retval; |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
478 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
479 if (! obj) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
480 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
|
481 if (PyBytes_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
482 { |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
483 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
|
484 } |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
485 else if (PyUnicode_Check (obj)) |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
486 { |
404
aef165ff92b0
Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents:
403
diff
changeset
|
487 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
|
488 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
|
489 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
|
490 else |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
491 throw object_convert_exception |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
492 ("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
|
493 } |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
494 else |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
495 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
|
496 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
497 return retval; |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
498 } |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
499 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
500 PyObject * |
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
501 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
|
502 { |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
503 #if PY_VERSION_HEX >= 0x03000000 |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
504 return PyUnicode_FromStringAndSize (str.data (), str.size ()); |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
505 #else |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
506 return PyString_FromStringAndSize (str.data (), str.size ()); |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
507 #endif |
402
c4b78e449c62
maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents:
400
diff
changeset
|
508 } |
325
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
509 |
fd5881d48238
Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
510 } |