annotate oct-py-types.cc @ 423:6b9de18b4bdd

Eliminate remaining custom exception handling in favor of Octave errors * oct-py-error.cc, oct-py-error.h (pytave::error_python_exception): New function to generate an Octave error from an active Python error condition, based on pytave::fetch_exception_message. * oct-py-eval.cc (pytave::py_call_function, pytave::py_run_string_safe): Use it. * oct-py-types.cc (pytave::make_py_dict): Likewise. * oct-py-util.cc (pytave::py_objstore): Likewise. * pycall.cc (Fpycall): Remove exception handling logic. * pyeval.cc (Fpyeval): Likewise. * pyexec.cc (Fpyexec): Likewise. * exceptions.cc, exceptions.h: Delete. * Makefile.am (COMMON_SOURCE_FILES, PYTAVE_HEADER_FILES): Remove them.
author Mike Miller <mtmiller@octave.org>
date Thu, 04 May 2017 21:15:07 -0700
parents 8247f298fd16
children
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
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
34 #include "oct-py-error.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)
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
68 error_conversion_invalid_python_object ("a boolean value");
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))
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
71 error_conversion_mismatch_python_type ("a boolean value", "bool");
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)
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
80 error_conversion_invalid_python_object ("a complex value");
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))
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
83 error_conversion_mismatch_python_type ("a complex value", "complex");
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)
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
93 error_conversion_invalid_python_object ("a floating point value");
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))
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
96 error_conversion_mismatch_python_type ("a floating point value", "float");
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)
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
141 error ("unable to create array, invalid array type code");
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 {
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
244 if (! value.is_scalar_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
245 error ("unable to convert non-scalar type \"%s\" to a Python number",
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
246 value.type_name ().c_str ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
247
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
248 if (value.is_bool_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
249 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
250
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
251 else if (value.is_int8_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
252 return make_py_int (value.int8_scalar_value ().value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
253 else if (value.is_int16_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
254 return make_py_int (value.int16_scalar_value ().value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
255 else if (value.is_int32_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
256 return make_py_int (value.int32_scalar_value ().value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
257 else if (value.is_int64_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
258 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
259
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
260 else if (value.is_uint8_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
261 return make_py_int (value.uint8_scalar_value ().value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
262 else if (value.is_uint16_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
263 return make_py_int (value.uint16_scalar_value ().value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
264 else if (value.is_uint32_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
265 return make_py_int (value.uint32_scalar_value ().value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
266 else if (value.is_uint64_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
267 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
268
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
269 else if (value.is_complex_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
270 return make_py_complex (value.complex_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
271 else if (value.is_float_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
272 return make_py_float (value.double_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
273 else
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
274 error ("unable to convert unhandled scalar type \"%s\" to a "
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
275 "Python number", value.type_name ().c_str ());
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
276
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
277 return nullptr;
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
278 }
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
279
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
280 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
281 make_py_array (const octave_value& value)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
282 {
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
283 if (! (value.is_numeric_type () && ! value.is_complex_type ()
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
284 && value.ndims () == 2
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
285 && (value.columns () <= 1 || value.rows () <= 1)))
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
286 error ("unable to convert non-vector type \"%s\" to a Python array",
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
287 value.type_name ().c_str ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
288
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
289 if (value.is_double_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
290 return make_py_array (value.array_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
291 else if (value.is_single_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
292 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
293
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
294 else if (value.is_int8_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
295 return make_py_array (value.int8_array_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
296 else if (value.is_int16_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
297 return make_py_array (value.int16_array_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
298 else if (value.is_int32_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
299 return make_py_array (value.int32_array_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
300 else if (value.is_int64_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
301 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
302
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
303 else if (value.is_uint8_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
304 return make_py_array (value.uint8_array_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
305 else if (value.is_uint16_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
306 return make_py_array (value.uint16_array_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
307 else if (value.is_uint32_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
308 return make_py_array (value.uint32_array_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
309 else if (value.is_uint64_type ())
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
310 return make_py_array (value.uint64_array_value ());
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
311 else
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
312 error ("unable to convert unhandled vector type \"%s\" to a "
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
313 "Python array", value.type_name ().c_str ());
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
314
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
315 return nullptr;
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
316 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
317
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
318 octave_scalar_map
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
319 extract_py_scalar_map (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
320 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
321 if (! obj)
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
322 error_conversion_invalid_python_object ("an Octave struct");
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
323
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
324 if (! PyDict_Check (obj))
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
325 error_conversion_mismatch_python_type ("an Octave struct", "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
326
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
327 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
328
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
329 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
330 PyObject *py_key = nullptr;
3644df6564bc maint: use C++11 nullptr rather than 0 or NULL
Mike Miller <mtmiller@octave.org>
parents: 402
diff changeset
331 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
332
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
333 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
334 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
335 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
336 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
337 "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
338
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
339 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
340 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
341 map.setfield (key, value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
342 }
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
343
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
344 return map;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
345 }
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
346
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
347 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
348 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
349 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
350 PyObject *dict = PyDict_New ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
351 if (! dict)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
352 octave_throw_bad_alloc ();
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
353
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
354 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
355 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
356 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
357 if (! key)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
358 octave_throw_bad_alloc ();
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
359
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
360 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
361
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
362 if (PyDict_SetItem (dict, key, item) < 0)
423
6b9de18b4bdd Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents: 422
diff changeset
363 error_python_exception ();
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
364 }
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
365
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
366 return dict;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
367 }
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
368
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
369 int64_t
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
370 extract_py_int64 (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
371 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
372 if (! obj)
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
373 error_conversion_invalid_python_object ("a signed integer value");
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
374
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
375 if (PyLong_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
376 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
377 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
378 #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
379 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
380 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
381 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
382 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
383 if (overflow)
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
384 {
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
385 if (overflow > 0)
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
386 value = std::numeric_limits<int64_t>::max ();
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
387 else
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
388 value = std::numeric_limits<int64_t>::min ();
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
389 }
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
390 return static_cast<int64_t> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
391 }
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
392 #if PY_VERSION_HEX < 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
393 else if (PyInt_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
394 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
395 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
396 else
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
397 error_conversion_mismatch_python_type ("a signed integer value",
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
398 "int or long");
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
399
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
400 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
401 }
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
402
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
403 uint64_t
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
404 extract_py_uint64 (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
405 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
406 if (! obj)
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
407 error_conversion_invalid_python_object ("an unsigned integer value");
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
408
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
409 if (PyLong_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
410 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
411 // 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
412 if (Py_SIZE (obj) < 0)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
413 return 0;
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
414
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
415 #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
416 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
417 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
418 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
419 unsigned long value = PyLong_AsUnsignedLong (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
420 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
421 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
422 if (overflow)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
423 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
424 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
425 PyErr_Clear ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
426 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
427
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
428 return static_cast<uint64_t> (value);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
429 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
430 #if PY_VERSION_HEX < 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
431 else if (PyInt_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
432 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
433 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
434 else
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
435 error_conversion_mismatch_python_type ("an unsigned integer value",
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
436 "int or long");
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
437
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
438 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
439 }
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
440
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
441 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
442 make_py_tuple (const Cell& cell)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
443 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
444 if (! (cell.is_empty () || cell.is_vector ()))
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
445 error ("unable to convert multidimensional cell array to a Python tuple");
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
446
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
447 octave_idx_type size = cell.numel ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
448 PyObject *tuple = PyTuple_New (size);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
449 if (! tuple)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
450 octave_throw_bad_alloc ();
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
451
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
452 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
453 {
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
454 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
455 PyTuple_SET_ITEM (tuple, i, item);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
456 }
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
457
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
458 return tuple;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
459 }
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
460
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
461 std::string
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
462 extract_py_str (PyObject *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 std::string retval;
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
465
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
466 if (! obj)
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
467 error_conversion_invalid_python_object ("a string value");
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
468
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
469 if (PyBytes_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
470 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
471 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
472 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
473 else if (PyUnicode_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
474 {
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
475 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
476 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
477 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
478 else
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
479 octave_throw_bad_alloc ();
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
480 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
481 else
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
482 error_conversion_mismatch_python_type ("a string value", "str");
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
483
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
484 return retval;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
485 }
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
486
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
487 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
488 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
489 {
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
490 #if PY_VERSION_HEX >= 0x03000000
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
491 return PyUnicode_FromStringAndSize (str.data (), str.size ());
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
492 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
493 return PyString_FromStringAndSize (str.data (), str.size ());
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
494 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 400
diff changeset
495 }
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
496
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
497 PyObject *
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
498 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
499 {
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
500 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
501 return pyobject_unwrap_object (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
502 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
503 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
504 else if (value.is_string ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
505 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
506 else if (value.is_scalar_type ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
507 return make_py_numeric_value (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
508 else if (value.is_cell ())
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
509 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
510 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
511 && (value.columns () <= 1 || value.rows () <= 1))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
512 return make_py_array (value);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
513 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
514 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
515 else
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
516 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
517
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
518 return nullptr;
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
519 }
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
520
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
521 octave_value
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
522 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
523 {
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
524 if (PyBool_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_bool (obj)};
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
526 #if PY_VERSION_HEX < 0x03000000
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
527 else if (PyInt_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
528 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
529 #endif
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
530 else if (PyComplex_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
531 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
532 else if (PyFloat_Check (obj))
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
533 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
534 else
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
535 return pyobject_wrap_object (obj);
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
536 }
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
537
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
538 }