annotate oct-py-types.cc @ 420:7864849e84c2

Eliminate some compiler warnings * Makefile.am (AM_CXXFLAGS): Build with -W -Wall by default. * __py_struct_from_dict__.cc (F__py_int64_scalar_value__, F__py_uint64_scalar_value__, F__py_is_none__, F__py_isinstance__, F__py_objstore_del__, F__py_objstore_get__, F__py_objstore_put__, F__py_string_value__, F__py_struct_from_dict__): Remove unused parameter to eliminate -Wunused-parameter warning. * pyexec.cc (Fpyexec): Likewise. * oct-py-init.cc (pytave::py_init): Declare argv array correctly to eliminate -Wwrite-strings warning. * oct-py-types.cc (pytave::make_py_bool): Explicitly increment and return value to eliminate -Wstrict-aliasing warning. (pytave::make_py_complex, pytave::extract_py_complex): Copy complex values between C++ and Python types to eliminate -Wstrict-aliasing warning. (pytave::extract_py_int64): Add braces to nested if block to eliminate -Wparentheses warning.
author Mike Miller <mtmiller@octave.org>
date Thu, 04 May 2017 12:31:07 -0700
parents 9bf8ba050122
children 24555fba9964
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
1 /*
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
2
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
3 Copyright (C) 2016 Mike Miller
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
4
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
5 This file is part of Pytave.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
6
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
7 Pytave is free software; you can redistribute it and/or modify it
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
10 option) any later version.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
11
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
12 Pytave is distributed in the hope that it will be useful, but WITHOUT
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
15 for more details.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
16
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
18 along with Pytave; see the file COPYING. If not, see
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
20
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
21 */
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
22
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
23 #if defined (HAVE_CONFIG_H)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
24 # include <config.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
25 #endif
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
26
355
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
27 #include <limits>
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
28 #include <octave/Cell.h>
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
29 #include <octave/oct-map.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
30 #include <octave/quit.h>
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
31 #include <octave/ov-null-mat.h>
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
32
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
33 #include "exceptions.h"
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
34 #include "oct-py-eval.h"
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
35 #include "oct-py-object.h"
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
36 #include "oct-py-types.h"
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
37 #include "oct-py-util.h"
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
38
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
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
42 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
43 make_py_bool (bool value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
44 {
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
45 python_object retval = value ? Py_True : Py_False;
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
46 Py_INCREF (retval);
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
47 return retval.release ();
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
48 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
49
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
50 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
51 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
52 {
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
53 Py_complex py_complex_value {value.real (), value.imag ()};
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
54 return PyComplex_FromCComplex (py_complex_value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
55 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
56
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
57 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
58 make_py_float (double value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
59 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
60 return PyFloat_FromDouble (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
61 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
62
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
63 bool
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
64 extract_py_bool (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
65 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
66 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
67 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
68
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
69 if (! PyBool_Check (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: 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
71
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
72 return (obj == Py_True);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
73 }
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 std::complex<double>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
76 extract_py_complex (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
77 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
78 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
79 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
80
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
81 if (! PyComplex_Check (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: 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
83
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
84 Py_complex value = PyComplex_AsCComplex (obj);
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
85 return std::complex<double> {value.real, value.imag};
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
86 }
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
87
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
88 double
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
89 extract_py_float (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
90 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
91 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
92 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
93
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
94 if (! PyFloat_Check (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: 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
96
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
97 return PyFloat_AsDouble (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
98 }
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 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
101 make_py_int (int32_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
102 {
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
103 #if PY_VERSION_HEX >= 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
104 return PyLong_FromLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
105 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
106 return PyInt_FromLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
107 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
108 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
109
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
110 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
111 make_py_int (uint32_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
112 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
113 return PyLong_FromUnsignedLong (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
114 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
115
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
116 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
117 make_py_int (int64_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
118 {
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
119 #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
120 return PyLong_FromLongLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
121 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
122 return PyLong_FromLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
123 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
124 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
125
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
126 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
127 make_py_int (uint64_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
128 {
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
129 #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
130 return PyLong_FromUnsignedLongLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
131 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
132 return PyLong_FromUnsignedLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
133 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
134 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
135
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
136 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
137 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
138 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
139 if (! typecode)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
140 throw object_convert_exception
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
141 ("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
142
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
143 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
144 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
145
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
146 if (len > 0)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
147 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
148 // 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
149 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
150 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
151 if (! buf)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
152 octave_throw_bad_alloc ();
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
153
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
154 PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ?
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
155 PyObject_GetAttrString (array, "frombytes") :
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
156 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
157 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
158 py_call_function (frombytes, args);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
159 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
160
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
161 return array.release ();
402
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
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
164 // 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
165 // 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
166
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
167 #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
168 # define ARRAY_INT64_TYPECODE 'q'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
169 # define ARRAY_UINT64_TYPECODE 'Q'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
170 #elif (SIZEOF_LONG == 8)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
171 # define ARRAY_INT64_TYPECODE 'l'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
172 # define ARRAY_UINT64_TYPECODE 'L'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
173 #else
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
174 # define ARRAY_INT64_TYPECODE 0
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
175 # define ARRAY_UINT64_TYPECODE 0
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
176 #endif
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
177
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
178 template <typename T>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
179 struct py_array_info { };
352
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 <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
182 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
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_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
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_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
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_int64>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
192 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
193 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
194 };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
195
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
196 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
197 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
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_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
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_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
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_uint64> {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
207 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
208 };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
209
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
210 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
211 make_py_array (const NDArray& nda)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
212 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
213 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
214 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
215
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
216 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
217 make_py_array (const FloatNDArray& nda)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
218 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
219 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
220 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
221
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
222 template <typename T>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
223 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
224 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
225 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
226 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
227 py_array_info<T>::typecode);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
228 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
229
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
230 // 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
231
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
232 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
233 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
234 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
235 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
236 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
237 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
238 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
239 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
240
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
241 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
242 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
243 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
244 if (value.is_scalar_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
245 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
246 if (value.is_bool_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
247 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
248
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
249 else if (value.is_int8_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
250 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
251 else if (value.is_int16_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
252 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
253 else if (value.is_int32_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
254 return make_py_int (value.int32_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
255 else if (value.is_int64_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
256 return make_py_int (value.int64_scalar_value ().value ());
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
257
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
258 else if (value.is_uint8_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.uint8_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
260 else if (value.is_uint16_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
261 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
262 else if (value.is_uint32_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
263 return make_py_int (value.uint32_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
264 else if (value.is_uint64_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
265 return make_py_int (value.uint64_scalar_value ().value ());
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
266
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
267 else if (value.is_complex_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
268 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
269 else if (value.is_float_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
270 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
271 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
272
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
273 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
274 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
275 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
276
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
277 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
278 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
279 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
280 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
281 && 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
282 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
283 if (value.is_double_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
284 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
285 else if (value.is_single_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
286 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
287
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
288 else if (value.is_int8_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.int8_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
290 else if (value.is_int16_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
291 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
292 else if (value.is_int32_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
293 return make_py_array (value.int32_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
294 else if (value.is_int64_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
295 return make_py_array (value.int64_array_value ());
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
296
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
297 else if (value.is_uint8_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.uint8_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
299 else if (value.is_uint16_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
300 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
301 else if (value.is_uint32_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
302 return make_py_array (value.uint32_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
303 else if (value.is_uint64_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
304 return make_py_array (value.uint64_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
305 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
306
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
307 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
308 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
309 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
310
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
311 octave_scalar_map
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
312 extract_py_scalar_map (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
313 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
314 if (! obj)
408
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
315 error ("unable to convert to an Octave struct, invalid Python object");
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
316
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
317 if (! PyDict_Check (obj))
408
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
318 error ("unable to convert to an Octave struct, must be a Python dict");
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
319
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
320 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
321
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
322 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
323 PyObject *py_key = nullptr;
3644df6564bc maint: use C++11 nullptr rather than 0 or NULL
Mike Miller <mtmiller@octave.org>
parents: 402
diff changeset
324 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
325
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
326 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
327 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
328 if (! PyBytes_Check (py_key) && ! PyUnicode_Check (py_key))
408
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
329 error ("unable to convert Python dict to Octave struct, "
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
330 "all keys in the dict must be strings");
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
331
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
332 std::string key = extract_py_str (py_key);
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
333 octave_value value = py_implicitly_convert_return_value (py_value);
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
334 map.setfield (key, value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
335 }
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
336
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
337 return map;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
338 }
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 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
341 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
342 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
343 PyObject *dict = PyDict_New ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
344 if (! dict)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
345 octave_throw_bad_alloc ();
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
346
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
347 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
348 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
349 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
350 if (! key)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
351 octave_throw_bad_alloc ();
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
352
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
353 PyObject *item = py_implicitly_convert_argument (map.contents (p));
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
354
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
355 if (PyDict_SetItem (dict, key, item) < 0)
415
9bf8ba050122 Replace all remaining Boost.Python references
Mike Miller <mtmiller@octave.org>
parents: 411
diff changeset
356 throw pytave::error_already_set ();
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
357 }
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
358
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
359 return dict;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
360 }
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
361
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
362 int64_t
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
363 extract_py_int64 (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
364 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
365 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
366 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
367
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
368 if (PyLong_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
369 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
370 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
371 #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
372 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
373 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
374 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
375 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
376 if (overflow)
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
377 {
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
378 if (overflow > 0)
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
379 value = std::numeric_limits<int64_t>::max ();
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
380 else
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
381 value = std::numeric_limits<int64_t>::min ();
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
382 }
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
383 return static_cast<int64_t> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
384 }
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
385 #if PY_VERSION_HEX < 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
386 else if (PyInt_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
387 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
388 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
389 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
390 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
391
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
392 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
393 }
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
394
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
395 uint64_t
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
396 extract_py_uint64 (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
397 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
398 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
399 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
400
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
401 if (PyLong_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
402 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
403 // 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
404 if (Py_SIZE (obj) < 0)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
405 return 0;
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
406
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
407 #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
408 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
409 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
410 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
411 unsigned long value = PyLong_AsUnsignedLong (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
412 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
413 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
414 if (overflow)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
415 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
416 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
417 PyErr_Clear ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
418 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
419
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
420 return static_cast<uint64_t> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
421 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
422 #if PY_VERSION_HEX < 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
423 else if (PyInt_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
424 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
425 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
426 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
427 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
428
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
429 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
430 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
431
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
432 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
433 make_py_tuple (const Cell& cell)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
434 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
435 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
436 throw value_convert_exception (
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
437 "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
438
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
439 octave_idx_type size = cell.numel ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
440 PyObject *tuple = PyTuple_New (size);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
441 if (! tuple)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
442 octave_throw_bad_alloc ();
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
443
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
444 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
445 {
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
446 PyObject *item = py_implicitly_convert_argument (cell.xelem (i));
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
447 PyTuple_SET_ITEM (tuple, i, item);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
448 }
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
449
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
450 return tuple;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
451 }
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
452
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
453 std::string
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
454 extract_py_str (PyObject *obj)
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 std::string retval;
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
457
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
458 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
459 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
460 if (PyBytes_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
461 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
462 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
463 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
464 else if (PyUnicode_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
465 {
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
466 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
467 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
468 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
469 else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
470 throw object_convert_exception
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
471 ("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
472 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
473 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
474 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
475
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
476 return retval;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
477 }
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 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
480 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
481 {
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
482 #if PY_VERSION_HEX >= 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
483 return PyUnicode_FromStringAndSize (str.data (), str.size ());
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
484 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
485 return PyString_FromStringAndSize (str.data (), str.size ());
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
486 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
487 }
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
488
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
489 PyObject *
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
490 py_implicitly_convert_argument (const octave_value& value)
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
491 {
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
492 if (value.is_object () && value.class_name () == "pyobject")
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
493 return pyobject_unwrap_object (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
494 else if (value.is_string () && value.rows () > 1)
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
495 error ("unable to convert multirow char array to a Python object");
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
496 else if (value.is_string ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
497 return make_py_str (value.string_value ());
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
498 else if (value.is_scalar_type ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
499 return make_py_numeric_value (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
500 else if (value.is_cell ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
501 return make_py_tuple (value.cell_value ());
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
502 else if (value.is_numeric_type () && value.ndims () == 2
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
503 && (value.columns () <= 1 || value.rows () <= 1))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
504 return make_py_array (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
505 else if (value.is_map () && value.numel () == 1)
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
506 return make_py_dict (value.scalar_map_value ());
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
507 else
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
508 error ("unable to convert unhandled Octave type to a Python object");
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
509
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
510 return nullptr;
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
511 }
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
512
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
513 octave_value
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
514 py_implicitly_convert_return_value (PyObject *obj)
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
515 {
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
516 if (PyBool_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
517 return octave_value {extract_py_bool (obj)};
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
518 #if PY_VERSION_HEX < 0x03000000
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
519 else if (PyInt_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
520 return octave_value {octave_int64 (extract_py_int64 (obj))};
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
521 #endif
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
522 else if (PyComplex_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
523 return octave_value {extract_py_complex (obj)};
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
524 else if (PyFloat_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
525 return octave_value {extract_py_float (obj)};
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
526 else
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
527 return pyobject_wrap_object (obj);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
528 }
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
529
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
530 }