annotate oct-py-types.cc @ 421:24555fba9964

Include <Python.h> first to suppress possible redefinition warnings * oct-py-eval.cc, oct-py-types.cc, oct-py-util.cc, pycall.cc, pyeval.cc, pyexec.cc: Include <Python.h> before other system and project header files to suppress possible macro redefinition warnings. Normalize and reduce use of Octave header files.
author Mike Miller <mtmiller@octave.org>
date Thu, 04 May 2017 13:22:20 -0700
parents 7864849e84c2
children 8247f298fd16
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
421
24555fba9964 Include <Python.h> first to suppress possible redefinition warnings
Mike Miller <mtmiller@octave.org>
parents: 420
diff changeset
27 #include <Python.h>
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
28 #include <limits>
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
29 #include <octave/Cell.h>
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
30 #include <octave/oct-map.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
31 #include <octave/quit.h>
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
32 #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
33
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
34 #include "exceptions.h"
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
35 #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
36 #include "oct-py-object.h"
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
37 #include "oct-py-types.h"
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
38 #include "oct-py-util.h"
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
39
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
40 namespace pytave
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
41 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
42
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
43 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
44 make_py_bool (bool value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
45 {
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
46 python_object retval = value ? Py_True : Py_False;
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
47 Py_INCREF (retval);
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
48 return retval.release ();
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
49 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
50
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
51 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
52 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
53 {
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
54 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
55 return PyComplex_FromCComplex (py_complex_value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
56 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
57
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
58 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
59 make_py_float (double value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
60 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
61 return PyFloat_FromDouble (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
62 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
63
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
64 bool
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
65 extract_py_bool (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
66 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
67 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
68 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
69
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
70 if (! PyBool_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
71 throw object_convert_exception ("failed to extract boolean: 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
72
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
73 return (obj == Py_True);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
74 }
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
75
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
76 std::complex<double>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
77 extract_py_complex (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
78 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
79 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
80 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
81
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
82 if (! PyComplex_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
83 throw object_convert_exception ("failed to extract complex: 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
84
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
85 Py_complex value = PyComplex_AsCComplex (obj);
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
86 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
87 }
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
88
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
89 double
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
90 extract_py_float (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
91 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
92 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
93 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
94
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
95 if (! PyFloat_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
96 throw object_convert_exception ("failed to extract float: 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
97
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
98 return PyFloat_AsDouble (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
99 }
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
100
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
101 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
102 make_py_int (int32_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
103 {
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
104 #if PY_VERSION_HEX >= 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
105 return PyLong_FromLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
106 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
107 return PyInt_FromLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
108 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
109 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
110
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
111 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
112 make_py_int (uint32_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
113 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
114 return PyLong_FromUnsignedLong (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
115 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
116
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
117 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
118 make_py_int (int64_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
119 {
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
120 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG))
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
121 return PyLong_FromLongLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
122 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
123 return PyLong_FromLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
124 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
125 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
126
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
127 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
128 make_py_int (uint64_t value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
129 {
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
130 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG))
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
131 return PyLong_FromUnsignedLongLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
132 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
133 return PyLong_FromUnsignedLong (value);
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
134 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
135 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
136
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
137 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
138 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
139 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
140 if (! typecode)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
141 throw object_convert_exception
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
142 ("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
143
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
144 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
145 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
146
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
147 if (len > 0)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
148 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
149 // 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
150 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
151 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
152 if (! buf)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
153 octave_throw_bad_alloc ();
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
154
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
155 PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ?
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
156 PyObject_GetAttrString (array, "frombytes") :
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
157 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
158 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
159 py_call_function (frombytes, args);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
160 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
161
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
162 return array.release ();
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
163 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
164
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
165 // 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
166 // 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
167
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
168 #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
169 # define ARRAY_INT64_TYPECODE 'q'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
170 # define ARRAY_UINT64_TYPECODE 'Q'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
171 #elif (SIZEOF_LONG == 8)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
172 # define ARRAY_INT64_TYPECODE 'l'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
173 # define ARRAY_UINT64_TYPECODE 'L'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
174 #else
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
175 # define ARRAY_INT64_TYPECODE 0
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
176 # define ARRAY_UINT64_TYPECODE 0
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
177 #endif
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
178
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
179 template <typename T>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
180 struct py_array_info { };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
181
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
182 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
183 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
184
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
185 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
186 struct py_array_info<octave_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
187
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
188 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
189 struct py_array_info<octave_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
190
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
191 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
192 struct py_array_info<octave_int64>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
193 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
194 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
195 };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
196
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
197 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
198 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
199
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
200 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
201 struct py_array_info<octave_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
202
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
203 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
204 struct py_array_info<octave_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
205
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
206 template <>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
207 struct py_array_info<octave_uint64> {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
208 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
209 };
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
210
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
211 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
212 make_py_array (const NDArray& nda)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
213 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
214 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
215 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
216
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
217 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
218 make_py_array (const FloatNDArray& nda)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
219 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
220 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
221 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
222
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
223 template <typename T>
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
224 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
225 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
226 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
227 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
228 py_array_info<T>::typecode);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
229 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
230
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
231 // 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
232
402
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_int8> (const int8NDArray&);
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_int16> (const int16NDArray&);
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_int32> (const int32NDArray&);
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_int64> (const int64NDArray&);
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_uint8> (const uint8NDArray&);
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_uint16> (const uint16NDArray&);
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_uint32> (const uint32NDArray&);
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_uint64> (const uint64NDArray&);
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
241
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
242 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
243 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
244 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
245 if (value.is_scalar_type ())
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_bool_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
248 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
249
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
250 else if (value.is_int8_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
251 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
252 else if (value.is_int16_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.int16_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_int32_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.int32_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_int64_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.int64_scalar_value ().value ());
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
258
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
259 else if (value.is_uint8_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
260 return make_py_int (value.uint8_scalar_value ().value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
261 else if (value.is_uint16_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.uint16_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_uint32_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.uint32_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_uint64_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.uint64_scalar_value ().value ());
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
267
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
268 else if (value.is_complex_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
269 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
270 else if (value.is_float_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
271 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
272 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
273
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
274 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
275 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
276 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
277
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
278 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
279 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
280 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
281 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
282 && 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
283 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
284 if (value.is_double_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
285 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
286 else if (value.is_single_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.float_array_value ());
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
288
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
289 else if (value.is_int8_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
290 return make_py_array (value.int8_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
291 else if (value.is_int16_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.int16_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_int32_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.int32_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_int64_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.int64_array_value ());
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
297
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
298 else if (value.is_uint8_type ())
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
299 return make_py_array (value.uint8_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
300 else if (value.is_uint16_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.uint16_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_uint32_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.uint32_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_uint64_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.uint64_array_value ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
306 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
307
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
308 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
309 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
310 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
311
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
312 octave_scalar_map
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
313 extract_py_scalar_map (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
314 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
315 if (! obj)
408
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
316 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
317
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
318 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
319 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
320
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
321 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
322
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
323 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
324 PyObject *py_key = nullptr;
3644df6564bc maint: use C++11 nullptr rather than 0 or NULL
Mike Miller <mtmiller@octave.org>
parents: 402
diff changeset
325 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
326
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
327 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
328 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
329 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
330 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
331 "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
332
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
333 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
334 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
335 map.setfield (key, value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
336 }
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
337
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
338 return map;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
339 }
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
340
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
341 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
342 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
343 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
344 PyObject *dict = PyDict_New ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
345 if (! dict)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
346 octave_throw_bad_alloc ();
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
347
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
348 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
349 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
350 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
351 if (! key)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
352 octave_throw_bad_alloc ();
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
353
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
354 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
355
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
356 if (PyDict_SetItem (dict, key, item) < 0)
415
9bf8ba050122 Replace all remaining Boost.Python references
Mike Miller <mtmiller@octave.org>
parents: 411
diff changeset
357 throw pytave::error_already_set ();
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
358 }
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
359
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
360 return dict;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
361 }
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
362
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
363 int64_t
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
364 extract_py_int64 (PyObject *obj)
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 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
367 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
368
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
369 if (PyLong_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
370 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
371 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
372 #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
373 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
374 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
375 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
376 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
377 if (overflow)
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
378 {
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
379 if (overflow > 0)
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
380 value = std::numeric_limits<int64_t>::max ();
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
381 else
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
382 value = std::numeric_limits<int64_t>::min ();
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
383 }
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
384 return static_cast<int64_t> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
385 }
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
386 #if PY_VERSION_HEX < 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
387 else if (PyInt_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
388 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
389 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
390 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
391 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
392
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
393 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
394 }
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
395
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
396 uint64_t
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
397 extract_py_uint64 (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
398 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
399 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
400 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
401
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
402 if (PyLong_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
403 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
404 // 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
405 if (Py_SIZE (obj) < 0)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
406 return 0;
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
407
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
408 #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
409 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
410 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
411 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
412 unsigned long value = PyLong_AsUnsignedLong (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
413 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
414 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
415 if (overflow)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
416 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
417 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
418 PyErr_Clear ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
419 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
420
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
421 return static_cast<uint64_t> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
422 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
423 #if PY_VERSION_HEX < 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
424 else if (PyInt_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
425 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
426 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
427 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
428 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
429
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
430 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
431 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
432
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
433 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
434 make_py_tuple (const Cell& cell)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
435 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
436 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
437 throw value_convert_exception (
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
438 "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
439
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
440 octave_idx_type size = cell.numel ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
441 PyObject *tuple = PyTuple_New (size);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
442 if (! tuple)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
443 octave_throw_bad_alloc ();
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
444
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
445 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
446 {
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
447 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
448 PyTuple_SET_ITEM (tuple, i, item);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
449 }
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
450
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
451 return tuple;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
452 }
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
453
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
454 std::string
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
455 extract_py_str (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
456 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
457 std::string retval;
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
458
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
459 if (! obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
460 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
461 if (PyBytes_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
462 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
463 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
464 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
465 else if (PyUnicode_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
466 {
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
467 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
468 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
469 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
470 else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
471 throw object_convert_exception
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
472 ("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
473 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
474 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
475 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
476
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
477 return retval;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
478 }
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
479
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
480 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
481 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
482 {
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
483 #if PY_VERSION_HEX >= 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
484 return PyUnicode_FromStringAndSize (str.data (), str.size ());
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
485 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
486 return PyString_FromStringAndSize (str.data (), str.size ());
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
487 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
488 }
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
489
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
490 PyObject *
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
491 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
492 {
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
493 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
494 return pyobject_unwrap_object (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
495 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
496 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
497 else if (value.is_string ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
498 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
499 else if (value.is_scalar_type ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
500 return make_py_numeric_value (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
501 else if (value.is_cell ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
502 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
503 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
504 && (value.columns () <= 1 || value.rows () <= 1))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
505 return make_py_array (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
506 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
507 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
508 else
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
509 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
510
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
511 return nullptr;
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
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
514 octave_value
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
515 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
516 {
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
517 if (PyBool_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
518 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
519 #if PY_VERSION_HEX < 0x03000000
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
520 else if (PyInt_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
521 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
522 #endif
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
523 else if (PyComplex_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
524 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
525 else if (PyFloat_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
526 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
527 else
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
528 return pyobject_wrap_object (obj);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
529 }
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
530
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
531 }