annotate octave_to_python.cc @ 399:692cecebc71f

Error on attempt to convert a char array to Python str (fixes issue #75) * octave_to_python.cc (pytave::octvalue_to_pyobj): Throw an exception on attempt to convert a char array with more than one row. * pycall.cc: Add %!tests.
author Mike Miller <mtmiller@octave.org>
date Fri, 28 Apr 2017 10:13:04 -0700
parents 09a1acb81d8b
children 6c316b5f30f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
1 /*
139
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
2
150
a3dc9d24ae38 maint: Add myself to copyright notices
Mike Miller <mtmiller@octave.org>
parents: 146
diff changeset
3 Copyright (C) 2015-2016 Mike Miller
139
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
4 Copyright (C) 2008 David Grundberg, HÃ¥kan Fors Nilsson
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
5 Copyright (C) 2009 VZLU Prague
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
6
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
7 This file is part of Pytave.
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
8
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
9 Pytave is free software: you can redistribute it and/or modify it
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
10 under the terms of the GNU General Public License as published by the
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
11 Free Software Foundation, either version 3 of the License, or (at your
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
12 option) any later version.
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
13
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
14 Pytave is distributed in the hope that it will be useful, but WITHOUT
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
17 for more details.
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
18
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
19 You should have received a copy of the GNU General Public License
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
20 along with Pytave; see the file COPYING. If not, see
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
21 <http://www.gnu.org/licenses/>.
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
22
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 134
diff changeset
23 */
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
24
132
386772f4e12d Clean up includes thanks to Octave header changes
Mike Miller <mtmiller@octave.org>
parents: 108
diff changeset
25 #if defined (HAVE_CONFIG_H)
386772f4e12d Clean up includes thanks to Octave header changes
Mike Miller <mtmiller@octave.org>
parents: 108
diff changeset
26 # include <config.h>
386772f4e12d Clean up includes thanks to Octave header changes
Mike Miller <mtmiller@octave.org>
parents: 108
diff changeset
27 #endif
386772f4e12d Clean up includes thanks to Octave header changes
Mike Miller <mtmiller@octave.org>
parents: 108
diff changeset
28
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
29 #include <boost/python.hpp>
100
ff70627aa203 Undef HAVE_STAT and HAVE_FSTAT consistently through-out all files
David Grundberg <individ@acc.umu.se>
parents: 76
diff changeset
30
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
31 #include <octave/oct.h>
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
32 #include <octave/ov.h>
9
9a773d71a973 celler & structar ^_^
Håkan Fors nilsson <c04hfn@cs.umu.se>
parents: 6
diff changeset
33 #include <octave/oct-map.h>
217
69abda471c67 Attempt at moving pyobject from octave to python
Colin Macdonald <cbm@m.fsf.org>
parents: 167
diff changeset
34 #include <octave/parse.h>
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
35
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
36 #include <iostream>
75
b0991511a16d a few fixes to numpy support
Jaroslav Hajek <highegg@gmail.com>
parents: 70
diff changeset
37 #include "arrayobjectdefs.h"
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
38 #include "exceptions.h"
56
43a413b7c151 improve conversions, handle POobject and char arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 50
diff changeset
39 #include "octave_to_python.h"
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 304
diff changeset
40 #include "oct-py-types.h"
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
41 #include "oct-py-util.h"
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
42
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
43 using namespace boost::python;
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
44
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
45 namespace pytave
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
46 {
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
47
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
48 template <class PythonPrimitive, class OctaveBase>
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
49 static void
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
50 copy_octarray_to_pyarrobj (PyArrayObject *pyarr, const OctaveBase& matrix,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
51 const unsigned int matindex,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
52 const unsigned int matstride,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
53 const int dimension, const unsigned int offset)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
54 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
55 unsigned char *ptr = (unsigned char*) PyArray_DATA (pyarr);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
56 if (dimension == PyArray_NDIM (pyarr) - 1)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
57 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
58 // Last dimension, base case
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
59 for (int i = 0; i < PyArray_DIM (pyarr, dimension); i++)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
60 {
108
74f17b2a5d45 Use the latest NumPy API
Mike Miller <mtmiller@octave.org>
parents: 107
diff changeset
61 *(PythonPrimitive *)&ptr[offset + i*PyArray_STRIDE (pyarr, dimension)]
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
62 = matrix.elem (matindex + i*matstride);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
63 }
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
64 }
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
65 else
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
66 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
67 for (int i = 0; i < PyArray_DIM (pyarr, dimension); i++)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
68 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
69 copy_octarray_to_pyarrobj<PythonPrimitive, OctaveBase> (
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
70 pyarr,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
71 matrix,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
72 matindex + i*matstride,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
73 matstride * PyArray_DIM (pyarr, dimension),
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
74 dimension + 1,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
75 offset + i*PyArray_STRIDE (pyarr, dimension));
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
76 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
77 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
78 }
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
79
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
80 static PyArrayObject *
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
81 createPyArr (const dim_vector& dims, int pyarrtype)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
82 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
83 int len = dims.length ();
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
84 npy_intp dimensions[len];
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
85 for (int i = 0; i < dims.length (); i++)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
86 dimensions[i] = dims(i);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
87
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
88 return (PyArrayObject *)PyArray_SimpleNew (len, dimensions, pyarrtype);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
89 }
56
43a413b7c151 improve conversions, handle POobject and char arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 50
diff changeset
90
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
91 template <class PythonPrimitive, class OctaveBase>
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
92 static PyArrayObject *
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
93 create_array (const OctaveBase& octarr, int pyarraytype)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
94 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
95 PyArrayObject *pyarr = createPyArr (octarr.dims (), pyarraytype);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
96 try
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
97 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
98 copy_octarray_to_pyarrobj<PythonPrimitive, OctaveBase> (pyarr, octarr, 0, 1, 0, 0);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
99 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
100 catch (const value_convert_exception&)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
101 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
102 Py_DECREF (pyarr);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
103 throw;
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
104 }
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
105 return pyarr;
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
106 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
107
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
108 static PyArrayObject *
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
109 octvalue_to_pyarrobj (const octave_value& matrix)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
110 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
111 if (matrix.is_double_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
112 if (matrix.is_complex_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
113 return create_array<Complex, ComplexNDArray> (matrix.complex_array_value (), NPY_CDOUBLE);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
114 else if (matrix.is_real_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
115 return create_array<double, NDArray> (matrix.array_value (), NPY_DOUBLE);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
116 else
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
117 throw value_convert_exception ("Unknown double matrix type");
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
118
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
119 if (matrix.is_single_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
120 if (matrix.is_complex_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
121 return create_array<FloatComplex, FloatComplexNDArray> (matrix.float_complex_array_value (), NPY_CFLOAT);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
122 else if (matrix.is_real_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
123 return create_array<float, FloatNDArray> (matrix.float_array_value (), NPY_FLOAT);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
124 else
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
125 throw value_convert_exception ("Unknown float matrix type");
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
126
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
127 if (matrix.is_int8_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
128 return create_array<int8_t, int8NDArray> (matrix.int8_array_value (), NPY_INT8);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
129 if (matrix.is_int16_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
130 return create_array<int16_t, int16NDArray> (matrix.int16_array_value (), NPY_INT16);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
131 if (matrix.is_int32_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
132 return create_array<int32_t, int32NDArray> (matrix.int32_array_value (), NPY_INT32);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
133 if (matrix.is_int64_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
134 return create_array<int64_t, int64NDArray> (matrix.int64_array_value (), NPY_INT64);
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
135
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
136 if (matrix.is_uint8_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
137 return create_array<uint8_t, uint8NDArray> (matrix.uint8_array_value (), NPY_UINT8);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
138 if (matrix.is_uint16_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
139 return create_array<uint16_t, uint16NDArray> (matrix.uint16_array_value (), NPY_UINT16);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
140 if (matrix.is_uint32_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
141 return create_array<uint32_t, uint32NDArray> (matrix.uint32_array_value (), NPY_UINT32);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
142 if (matrix.is_uint64_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
143 return create_array<uint64_t, uint64NDArray> (matrix.uint64_array_value (), NPY_UINT64);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
144
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
145 if (matrix.is_bool_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
146 return create_array<bool, boolNDArray> (matrix.bool_array_value (), NPY_BOOL);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
147 if (matrix.is_string ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
148 return create_array<char, charNDArray> (matrix.char_array_value (), NPY_CHAR);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
149
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
150 throw value_convert_exception ("Octave matrix type not known, conversion not implemented");
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
151 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
152
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
153 static void
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
154 octvalue_to_pyarr (boost::python::object& py_object,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
155 const octave_value& octvalue)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
156 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
157 PyArrayObject *pyarr = octvalue_to_pyarrobj (octvalue);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
158 py_object = object (handle<PyObject> ((PyObject *)pyarr));
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
159 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
160
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
161 void octvalue_to_pyobj (boost::python::object& py_object,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
162 const octave_value& octvalue)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
163 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
164 if (octvalue.is_undefined ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
165 throw value_convert_exception (
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
166 "Octave value `undefined'. Can not convert to a Python object");
399
692cecebc71f Error on attempt to convert a char array to Python str (fixes issue #75)
Mike Miller <mtmiller@octave.org>
parents: 392
diff changeset
167 else if (octvalue.is_string () && octvalue.rows () > 1)
692cecebc71f Error on attempt to convert a char array to Python str (fixes issue #75)
Mike Miller <mtmiller@octave.org>
parents: 392
diff changeset
168 throw value_convert_exception (
692cecebc71f Error on attempt to convert a char array to Python str (fixes issue #75)
Mike Miller <mtmiller@octave.org>
parents: 392
diff changeset
169 "Octave multirow char array cannot be converted to a Python object");
185
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
170 else if (octvalue.is_string ())
325
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
171 {
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
172 PyObject *obj = make_py_str (octvalue.string_value ());
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
173 py_object = object (handle<PyObject> (obj));
fd5881d48238 Overhaul Python string creation and extraction
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
174 }
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
175 else if (octvalue.is_scalar_type ())
337
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
176 {
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
177 PyObject *obj = make_py_numeric_value (octvalue);
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
178 py_object = object (handle<PyObject> (obj));
d3d355dc44ad Overhaul Python numeric type creation from Octave scalar types
Mike Miller <mtmiller@octave.org>
parents: 336
diff changeset
179 }
303
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
180 else if (octvalue.is_cell ())
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 326
diff changeset
181 {
342
6bd8f5e3542a Convert Octave cell array into Python tuple instead of list
Mike Miller <mtmiller@octave.org>
parents: 337
diff changeset
182 PyObject *obj = make_py_tuple (octvalue.cell_value ());
336
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 326
diff changeset
183 py_object = object (handle<PyObject> (obj));
c081e30c2f64 Overhaul Python list creation from cell array
Mike Miller <mtmiller@octave.org>
parents: 326
diff changeset
184 }
352
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
185 else if (octvalue.is_numeric_type () && octvalue.ndims () == 2
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
186 && (octvalue.columns () == 1 || octvalue.rows () == 1))
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
187 {
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
188 PyObject *obj = make_py_array (octvalue);
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
189 py_object = object (handle<PyObject> (obj));
eac35d84ef0d Convert Octave numeric vectors into Python array.array
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
190 }
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
191 else if (octvalue.is_numeric_type () || octvalue.is_string ()
303
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
192 || octvalue.is_bool_type ())
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
193 octvalue_to_pyarr (py_object, octvalue);
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 304
diff changeset
194 else if (octvalue.is_map () && octvalue.numel () == 1)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 304
diff changeset
195 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 304
diff changeset
196 PyObject *obj = make_py_dict (octvalue.scalar_map_value ());
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 304
diff changeset
197 py_object = object (handle<PyObject> (obj));
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 304
diff changeset
198 }
229
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
199 else if (octvalue.is_object () && octvalue.class_name () == "pyobject")
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
200 {
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
201 PyObject *obj = pyobject_unwrap_object (octvalue);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 352
diff changeset
202 py_object = object (handle<PyObject> (obj));
229
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
203 }
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
204 else
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
205 throw value_convert_exception (
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
206 "Conversion from Octave value not implemented");
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
207 }
25
113428ba033c Whitespace fixes
David Grundberg <individ@acc.umu.se>
parents: 21
diff changeset
208 }