annotate oct-py-types.cc @ 374:d362cdd1ddeb

pyobject: add conversion methods for single, intX, and uintX types * oct-py-types.cc, oct-py-types.h (pytave::extract_py_uint64): New function. * __py_struct_from_dict__.cc (F__py_uint64_scalar_value__): New function. (F__py_int64_scalar_value__): Simplify and clean up style. Add %!tests. * @pyobject/pyobject.m (pyobject.single, pyobject.int8, pyobject.int16, pyobject.int32, pyobject.uint8, pyobject.uint16, pyobject.uint32, pyobject.uint64): New conversion methods.
author Mike Miller <mtmiller@octave.org>
date Fri, 26 Aug 2016 18:51:42 -0700
parents 07c1b457cb6b
children 6c316b5f30f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
1 /*
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
2
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
3 Copyright (C) 2016 Mike Miller
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
4
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
5 This file is part of Pytave.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
6
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
7 Pytave is free software; you can redistribute it and/or modify it
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
10 option) any later version.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
11
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
12 Pytave is distributed in the hope that it will be useful, but WITHOUT
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
15 for more details.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
16
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
18 along with Pytave; see the file COPYING. If not, see
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
20
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
21 */
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
22
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
23 #if defined (HAVE_CONFIG_H)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
24 # include <config.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
25 #endif
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
26
355
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
27 #include <limits>
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
28 #include <octave/Cell.h>
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
29 #include <octave/oct-map.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
30 #include <octave/quit.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
31
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
32 #include "exceptions.h"
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
33 #include "oct-py-eval.h"
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
34 #include "oct-py-types.h"
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
35
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
36 // FIXME: only here to bootstrap nested conversions needed in this file
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
37 #include "octave_to_python.h"
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
38 #include "python_to_octave.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
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
43 PyObject *
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
44 make_py_bool (bool value)
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
45 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
46 if (value)
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
47 Py_RETURN_TRUE;
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
48 else
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
49 Py_RETURN_FALSE;
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
50 }
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
51
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
52 PyObject *
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
53 make_py_complex (std::complex<double> value)
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
54 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
55 Py_complex& py_complex_value = reinterpret_cast<Py_complex&> (value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
56 return PyComplex_FromCComplex (py_complex_value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
57 }
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
58
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
59 PyObject *
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
60 make_py_float (double value)
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
61 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
62 return PyFloat_FromDouble (value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
63 }
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
64
340
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
65 bool
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
66 extract_py_bool (PyObject *obj)
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
67 {
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
68 if (! obj)
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
69 throw object_convert_exception ("failed to extract boolean: null object");
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
70
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
71 if (! PyBool_Check (obj))
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
72 throw object_convert_exception ("failed to extract boolean: wrong type");
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
73
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
74 return (obj == Py_True);
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
75 }
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
76
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
77 std::complex<double>
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
78 extract_py_complex (PyObject *obj)
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
79 {
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
80 if (! obj)
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
81 throw object_convert_exception ("failed to extract complex: null object");
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
82
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
83 if (! PyComplex_Check (obj))
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
84 throw object_convert_exception ("failed to extract complex: wrong type");
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
85
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
86 Py_complex value = PyComplex_AsCComplex (obj);
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
87 return reinterpret_cast<std::complex<double>&> (value);
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
88 }
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
89
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
90 double
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
91 extract_py_float (PyObject *obj)
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
92 {
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
93 if (! obj)
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
94 throw object_convert_exception ("failed to extract float: null object");
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
95
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
96 if (! PyFloat_Check (obj))
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
97 throw object_convert_exception ("failed to extract float: wrong type");
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
98
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
99 return PyFloat_AsDouble (obj);
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
100 }
06b8aeea456f Add functions to conversion API to extract bool, double, and complex values
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
101
361
07c1b457cb6b Make pytave::make_py_int functions public
Mike Miller <mtmiller@octave.org>
parents: 355
diff changeset
102 PyObject *
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
103 make_py_int (int32_t value)
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
104 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
105 #if PY_VERSION_HEX >= 0x03000000
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
106 return PyLong_FromLong (value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
107 #else
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
108 return PyInt_FromLong (value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
109 #endif
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
110 }
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
111
361
07c1b457cb6b Make pytave::make_py_int functions public
Mike Miller <mtmiller@octave.org>
parents: 355
diff changeset
112 PyObject *
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
113 make_py_int (uint32_t value)
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
114 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
115 return PyLong_FromUnsignedLong (value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
116 }
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
117
361
07c1b457cb6b Make pytave::make_py_int functions public
Mike Miller <mtmiller@octave.org>
parents: 355
diff changeset
118 PyObject *
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
119 make_py_int (int64_t value)
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
120 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
121 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG))
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
122 return PyLong_FromLongLong (value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
123 #else
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
124 return PyLong_FromLong (value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
125 #endif
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
126 }
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
127
361
07c1b457cb6b Make pytave::make_py_int functions public
Mike Miller <mtmiller@octave.org>
parents: 355
diff changeset
128 PyObject *
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
129 make_py_int (uint64_t value)
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
130 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
131 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG))
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
132 return PyLong_FromUnsignedLongLong (value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
133 #else
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
134 return PyLong_FromUnsignedLong (value);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
135 #endif
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
136 }
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
137
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
138 PyObject *
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
139 make_py_array (const void *data, size_t len, char typecode)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
140 {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
141 if (! typecode)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
142 throw object_convert_exception ("unable to create array from Octave data");
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
143
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
144 std::string arg { typecode };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
145 PyObject *array = py_call_function ("array.array", ovl (arg));
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
146
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
147 if (len > 0)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
148 {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
149 // create a byte buffer containing a copy of the array binary data
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
150 const char *cdata = reinterpret_cast<const char *> (data);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
151 PyObject *buf = PyBytes_FromStringAndSize (cdata, len);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
152 if (! buf)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
153 octave_throw_bad_alloc ();
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
154
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
155 PyObject *frombytes = (PyObject_HasAttrString (array, "frombytes") ?
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
156 PyObject_GetAttrString (array, "frombytes") :
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
157 PyObject_GetAttrString (array, "fromstring"));
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
158 PyObject *args = PyTuple_Pack (1, buf);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
159 py_call_function (frombytes, args);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
160 Py_DECREF (args);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
161 Py_DECREF (buf);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
162 }
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
163
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
164 return array;
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
165 }
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
166
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
167 // Prefer the 'q' and 'Q' typecodes if they are available (if Python 3 and
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
168 // built with support for long long integers)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
169
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
170 #if (PY_VERSION_HEX >= 0x03000000) && defined (HAVE_LONG_LONG)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
171 # define ARRAY_INT64_TYPECODE 'q'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
172 # define ARRAY_UINT64_TYPECODE 'Q'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
173 #elif (SIZEOF_LONG == 8)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
174 # define ARRAY_INT64_TYPECODE 'l'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
175 # define ARRAY_UINT64_TYPECODE 'L'
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
176 #else
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
177 # define ARRAY_INT64_TYPECODE 0
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
178 # define ARRAY_UINT64_TYPECODE 0
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
179 #endif
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
180
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
181 template <typename T>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
182 struct py_array_info { };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
183
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
184 template <>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
185 struct py_array_info<octave_int8> { static const char typecode = 'b'; };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
186
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
187 template <>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
188 struct py_array_info<octave_int16> { static const char typecode = 'h'; };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
189
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
190 template <>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
191 struct py_array_info<octave_int32> { static const char typecode = 'i'; };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
192
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
193 template <>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
194 struct py_array_info<octave_int64>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
195 {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
196 static const char typecode = ARRAY_INT64_TYPECODE;
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
197 };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
198
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
199 template <>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
200 struct py_array_info<octave_uint8> { static const char typecode = 'B'; };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
201
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
202 template <>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
203 struct py_array_info<octave_uint16> { static const char typecode = 'H'; };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
204
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
205 template <>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
206 struct py_array_info<octave_uint32> { static const char typecode = 'I'; };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
207
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
208 template <>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
209 struct py_array_info<octave_uint64> {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
210 static const char typecode = ARRAY_UINT64_TYPECODE;
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
211 };
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
212
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
213 PyObject *
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
214 make_py_array (const NDArray& nda)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
215 {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
216 return make_py_array (nda.data (), nda.numel () * sizeof (double), 'd');
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
217 }
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
218
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
219 PyObject *
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
220 make_py_array (const FloatNDArray& nda)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
221 {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
222 return make_py_array (nda.data (), nda.numel () * sizeof (float), 'f');
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
223 }
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
224
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
225 template <typename T>
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
226 PyObject *
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
227 make_py_array (const intNDArray<T>& nda)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
228 {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
229 return make_py_array (nda.data (), nda.numel () * sizeof (T),
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
230 py_array_info<T>::typecode);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
231 }
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
232
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
233 // Instantiate all possible integer array template functions needed
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
234
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
235 template PyObject * make_py_array<octave_int8> (const int8NDArray&);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
236 template PyObject * make_py_array<octave_int16> (const int16NDArray&);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
237 template PyObject * make_py_array<octave_int32> (const int32NDArray&);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
238 template PyObject * make_py_array<octave_int64> (const int64NDArray&);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
239 template PyObject * make_py_array<octave_uint8> (const uint8NDArray&);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
240 template PyObject * make_py_array<octave_uint16> (const uint16NDArray&);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
241 template PyObject * make_py_array<octave_uint32> (const uint32NDArray&);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
242 template PyObject * make_py_array<octave_uint64> (const uint64NDArray&);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
243
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
244 PyObject *
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
245 make_py_numeric_value (const octave_value& value)
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
246 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
247 if (value.is_scalar_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
248 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
249 if (value.is_bool_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
250 return make_py_bool (value.bool_value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
251
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
252 else if (value.is_int8_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
253 return make_py_int (value.int8_scalar_value ().value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
254 else if (value.is_int16_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
255 return make_py_int (value.int16_scalar_value ().value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
256 else if (value.is_int32_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
257 return make_py_int (value.int32_scalar_value ().value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
258 else if (value.is_int64_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
259 return make_py_int (value.int64_scalar_value ().value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
260
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
261 else if (value.is_uint8_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
262 return make_py_int (value.uint8_scalar_value ().value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
263 else if (value.is_uint16_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
264 return make_py_int (value.uint16_scalar_value ().value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
265 else if (value.is_uint32_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
266 return make_py_int (value.uint32_scalar_value ().value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
267 else if (value.is_uint64_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
268 return make_py_int (value.uint64_scalar_value ().value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
269
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
270 else if (value.is_complex_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
271 return make_py_complex (value.complex_value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
272 else if (value.is_float_type ())
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
273 return make_py_float (value.double_value ());
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
274 }
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
275
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
276 throw value_convert_exception ("unhandled scalar type");
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
277 return 0;
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
278 }
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
279
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
280 PyObject *
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
281 make_py_array (const octave_value& value)
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
282 {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
283 if (value.is_numeric_type () && ! value.is_complex_type ()
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
284 && value.ndims () == 2 && (value.columns () == 1 || value.rows () == 1))
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
285 {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
286 if (value.is_double_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
287 return make_py_array (value.array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
288 else if (value.is_single_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
289 return make_py_array (value.float_array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
290
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
291 else if (value.is_int8_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
292 return make_py_array (value.int8_array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
293 else if (value.is_int16_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
294 return make_py_array (value.int16_array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
295 else if (value.is_int32_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
296 return make_py_array (value.int32_array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
297 else if (value.is_int64_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
298 return make_py_array (value.int64_array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
299
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
300 else if (value.is_uint8_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
301 return make_py_array (value.uint8_array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
302 else if (value.is_uint16_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
303 return make_py_array (value.uint16_array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
304 else if (value.is_uint32_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
305 return make_py_array (value.uint32_array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
306 else if (value.is_uint64_type ())
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
307 return make_py_array (value.uint64_array_value ());
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
308 }
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
309
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
310 throw value_convert_exception ("unhandled Octave numeric vector type");
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
311 return 0;
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
312 }
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 344
diff changeset
313
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
314 inline PyObject *
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
315 wrap_octvalue_to_pyobj (const octave_value& value)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
316 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
317 boost::python::object obj;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
318 octvalue_to_pyobj (obj, value);
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
319 PyObject *ptr = obj.ptr ();
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
320 Py_INCREF (ptr);
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
321 return ptr;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
322 }
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
323
344
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
324 inline octave_value
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
325 wrap_pyobj_to_octvalue (PyObject *obj)
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
326 {
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
327 boost::python::object objref { boost::python::handle<> (boost::python::borrowed (obj)) };
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
328 octave_value value;
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
329 pyobj_to_octvalue (value, objref);
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
330 return value;
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
331 }
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
332
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
333 octave_scalar_map
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
334 extract_py_scalar_map (PyObject *obj)
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
335 {
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
336 if (! obj)
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
337 throw object_convert_exception ("failed to extract map: null object");
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
338
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
339 if (! PyDict_Check (obj))
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
340 throw object_convert_exception ("failed to extract map: wrong type");
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
341
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
342 octave_scalar_map map;
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
343
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
344 Py_ssize_t pos = 0;
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
345 PyObject *py_key = 0;
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
346 PyObject *py_value = 0;
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
347
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
348 while (PyDict_Next (obj, &pos, &py_key, &py_value))
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
349 {
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
350 if (! PyBytes_Check (py_key) && ! PyUnicode_Check (py_key))
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
351 throw object_convert_exception ("failed to extract map: bad key type");
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
352
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
353 std::string key = extract_py_str (py_key);
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
354 octave_value value = wrap_pyobj_to_octvalue (py_value);
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
355 map.setfield (key, value);
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
356 }
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
357
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
358 return map;
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
359 }
9d95f087e5aa Add a function to extract a scalar map value from a Python dict
Mike Miller <mtmiller@octave.org>
parents: 342
diff changeset
360
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
361 PyObject *
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
362 make_py_dict (const octave_scalar_map& map)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
363 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
364 PyObject *dict = PyDict_New ();
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
365 if (! dict)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
366 octave_throw_bad_alloc ();
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
367
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
368 for (auto p = map.begin (); p != map.end (); ++p)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
369 {
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
370 PyObject *key = make_py_str (map.key (p));
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
371 if (! key)
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
372 octave_throw_bad_alloc ();
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
373
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
374 PyObject *item = wrap_octvalue_to_pyobj (map.contents (p));
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
375
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
376 if (PyDict_SetItem (dict, key, item) < 0)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
377 throw boost::python::error_already_set ();
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
378 }
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
379
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
380 return dict;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
381 }
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
382
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
383 int64_t
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
384 extract_py_int64 (PyObject *obj)
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
385 {
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
386 if (! obj)
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
387 throw object_convert_exception ("failed to extract integer: null object");
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
388
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
389 if (PyLong_Check (obj))
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
390 {
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
391 int overflow = 0;
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
392 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG == 8))
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
393 PY_LONG_LONG value = PyLong_AsLongLongAndOverflow (obj, &overflow);
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
394 #else
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
395 long value = PyLong_AsLongAndOverflow (obj, &overflow);
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
396 #endif
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
397 if (overflow)
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
398 if (overflow > 0)
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
399 value = std::numeric_limits<int64_t>::max ();
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
400 else
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
401 value = std::numeric_limits<int64_t>::min ();
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
402 return static_cast<int64_t> (value);
8cdc94b83e98 Fix int64 value extraction from long to work on 32-bit, use Octave saturation
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
403 }
328
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
404 #if PY_VERSION_HEX < 0x03000000
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
405 else if (PyInt_Check (obj))
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
406 return PyInt_AsLong (obj);
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
407 #endif
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
408 else
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
409 throw object_convert_exception ("failed to extract integer: wrong type");
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
410
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
411 return 0;
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
412 }
5782d7932529 Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents: 325
diff changeset
413
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
414 uint64_t
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
415 extract_py_uint64 (PyObject *obj)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
416 {
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
417 if (! obj)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
418 throw object_convert_exception ("failed to extract integer: null object");
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
419
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
420 if (PyLong_Check (obj))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
421 {
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
422 // FIXME: if (value < 0), may be very implementation dependent
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
423 if (Py_SIZE (obj) < 0)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
424 return 0;
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
425
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
426 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG == 8))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
427 unsigned PY_LONG_LONG value = PyLong_AsUnsignedLongLong (obj);
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
428 bool overflow = (value == static_cast<unsigned PY_LONG_LONG> (-1));
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
429 #else
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
430 unsigned long value = PyLong_AsUnsignedLong (obj);
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
431 bool overflow = (value == static_cast<unsigned long> (-1));
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
432 #endif
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
433 if (overflow)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
434 {
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
435 value = std::numeric_limits<uint64_t>::max ();
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
436 PyErr_Clear ();
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
437 }
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
438
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
439 return static_cast<uint64_t> (value);
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
440 }
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
441 #if PY_VERSION_HEX < 0x03000000
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
442 else if (PyInt_Check (obj))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
443 return static_cast<uint64_t> (PyInt_AsLong (obj));
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
444 #endif
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
445 else
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
446 throw object_convert_exception ("failed to extract integer: wrong type");
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
447
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
448 return 0;
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
449 }
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 361
diff changeset
450
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
451 PyObject *
342
6bd8f5e3542a Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents: 340
diff changeset
452 make_py_tuple (const Cell& cell)
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
453 {
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
454 if (! (cell.is_empty () || cell.is_vector ()))
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
455 throw value_convert_exception (
342
6bd8f5e3542a Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents: 340
diff changeset
456 "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
457
342
6bd8f5e3542a Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents: 340
diff changeset
458 octave_idx_type size = cell.numel ();
6bd8f5e3542a Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents: 340
diff changeset
459 PyObject *tuple = PyTuple_New (size);
6bd8f5e3542a Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents: 340
diff changeset
460 if (! tuple)
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
461 octave_throw_bad_alloc ();
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
462
342
6bd8f5e3542a Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents: 340
diff changeset
463 for (octave_idx_type i = 0; i < size; ++i)
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
464 {
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
465 PyObject *item = wrap_octvalue_to_pyobj (cell.xelem (i));
342
6bd8f5e3542a Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents: 340
diff changeset
466 PyTuple_SET_ITEM (tuple, i, item);
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
467 }
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
468
342
6bd8f5e3542a Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents: 340
diff changeset
469 return tuple;
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
470 }
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 328
diff changeset
471
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
472 std::string
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
473 extract_py_str (PyObject *obj)
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
474 {
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
475 std::string retval;
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
476
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
477 if (! obj)
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
478 throw object_convert_exception ("failed to extract string: null object");
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
479 if (PyBytes_Check (obj))
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
480 {
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
481 retval.assign (PyBytes_AsString (obj), PyBytes_Size (obj));
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
482 }
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
483 else if (PyUnicode_Check (obj))
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
484 {
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
485 bool ok = false;
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
486 PyObject *enc = PyUnicode_AsUTF8String (obj);
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
487 if (enc)
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
488 {
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
489 if (PyBytes_Check (enc))
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
490 {
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
491 ok = true;
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
492 retval.assign (PyBytes_AsString (enc), PyBytes_Size (enc));
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
493 }
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
494 Py_DECREF (enc);
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
495 }
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
496 if (! ok)
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
497 throw object_convert_exception ("failed to extract string: UTF-8 error");
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
498 }
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
499 else
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
500 throw object_convert_exception ("failed to extract string: wrong type");
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
501
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
502 return retval;
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
503 }
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
504
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
505 PyObject *
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
506 make_py_str (const std::string& str)
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
507 {
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
508 #if PY_VERSION_HEX >= 0x03000000
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
509 return PyUnicode_FromStringAndSize (str.data (), str.size ());
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
510 #else
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
511 return PyString_FromStringAndSize (str.data (), str.size ());
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
512 #endif
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
513 }
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
514
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
515 }