annotate octave_to_python.cc @ 320:c2aa34730dc9

Convert scalar struct into Python dict (fixes issue #57) * oct-py-types.cc, oct-py-types.h: New files for conversion of objects between Octave and Python APIs. * Makefile.am (COMMON_SOURCE_FILES, PYTAVE_HEADER_FILES): Include new files. * octave_to_python.cc (pytave::octvalue_to_pyobj): Add case to handle scalar struct instead of struct array. * @pyobject/pyobject.m: Add %!tests for pyobject from scalar struct and %!error tests for failure with struct array.
author Mike Miller <mtmiller@octave.org>
date Thu, 11 Aug 2016 16:12:35 -0700
parents a133564f4af7
children fd5881d48238
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"
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
41
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
42 using namespace boost::python;
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
43
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
44 namespace pytave
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
45 {
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
46
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
47 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
48 static void
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
49 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
50 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
51 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
52 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
53 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
54 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
55 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
56 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
57 // 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
58 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
59 {
108
74f17b2a5d45 Use the latest NumPy API
Mike Miller <mtmiller@octave.org>
parents: 107
diff changeset
60 *(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
61 = 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
62 }
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
63 }
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
64 else
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
65 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
66 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
67 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
68 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
69 pyarr,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
70 matrix,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
71 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
72 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
73 dimension + 1,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
74 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
75 }
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 }
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
78
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
79 template <>
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
80 void
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
81 copy_octarray_to_pyarrobj<PyObject *, Cell> (PyArrayObject *pyarr,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
82 const Cell& matrix,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
83 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
84 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
85 const int dimension,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
86 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
87 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
88 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
89 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
90 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
91 // 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
92 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
93 {
56
43a413b7c151 improve conversions, handle POobject and char arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 50
diff changeset
94 object pyobj;
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
95 octvalue_to_pyobj (pyobj, 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
96 Py_INCREF (pyobj.ptr ());
108
74f17b2a5d45 Use the latest NumPy API
Mike Miller <mtmiller@octave.org>
parents: 107
diff changeset
97 *(PyObject **)&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
98 = pyobj.ptr ();
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 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
101 else
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
102 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
103 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
104 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
105 copy_octarray_to_pyarrobj<PyObject *, Cell> (
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
106 pyarr,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
107 matrix,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
108 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
109 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
110 dimension + 1,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
111 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
112 }
56
43a413b7c151 improve conversions, handle POobject and char arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 50
diff changeset
113 }
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
114 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
115
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
116 static PyArrayObject *
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
117 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
118 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
119 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
120 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
121 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
122 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
123
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
124 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
125 }
56
43a413b7c151 improve conversions, handle POobject and char arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 50
diff changeset
126
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
127 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
128 static PyArrayObject *
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
129 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
130 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
131 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
132 try
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
133 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
134 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
135 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
136 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
137 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
138 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
139 throw;
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
140 }
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
141 return pyarr;
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
142 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
143
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
144 static PyArrayObject *
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
145 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
146 {
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_double_type ())
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
148 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
149 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
150 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
151 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
152 else
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
153 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
154
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
155 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
156 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
157 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
158 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
159 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
160 else
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
161 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
162
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
163 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
164 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
165 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
166 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
167 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
168 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
169 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
170 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
171
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
172 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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
181 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
182 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
183 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
184 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
185
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
186 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
187 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
188
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
189 static void
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
190 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
191 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
192 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
193 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
194 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
195 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
196
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
197 static void
303
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
198 octcell_to_pyobject (boost::python::object& py_object,
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
199 const Cell& cell)
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
200 {
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
201 if (! (cell.is_empty () || cell.is_vector ()))
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
202 throw value_convert_exception (
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
203 "unable to convert multidimensional cell array into Python sequence");
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
204
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
205 boost::python::list sequence;
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
206
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
207 for (octave_idx_type i = 0; i < cell.numel (); i++)
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
208 {
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
209 boost::python::object py_val;
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
210 octvalue_to_pyobj (py_val, cell(i));
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
211 sequence.append (py_val);
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
212 }
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
213
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
214 py_object = sequence;
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
215 }
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
216
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
217 static void
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
218 octmap_to_pyobject (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
219 const octave_map& map)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
220 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
221 py_object = boost::python::dict ();
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
222 string_vector keys = map.keys ();
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
223
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
224 for (octave_idx_type i = 0 ; i < keys.numel (); i++)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
225 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
226 boost::python::object py_val;
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
227 const Cell c = map.contents (keys[i]);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
228 octvalue_to_pyarr (py_val, c);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
229 py_object[keys[i]] = py_val;
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
230 }
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
231 }
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
232
304
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
233 inline PyObject *
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
234 python_integer_value (int32_t value)
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
235 {
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
236 #if PY_VERSION_HEX >= 0x03000000
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
237 return PyLong_FromLong (value);
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
238 #else
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
239 return PyInt_FromLong (value);
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
240 #endif
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
241 }
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
242
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
243 inline PyObject *
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
244 python_integer_value (uint32_t value)
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
245 {
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
246 return PyLong_FromUnsignedLong (value);
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
247 }
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
248
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
249 inline PyObject *
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
250 python_integer_value (int64_t value)
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
251 {
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
252 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG))
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
253 return PyLong_FromLongLong (value);
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
254 #else
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
255 return PyLong_FromLong (value);
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
256 #endif
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
257 }
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
258
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
259 inline PyObject *
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
260 python_integer_value (uint64_t value)
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
261 {
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
262 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG > SIZEOF_LONG))
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
263 return PyLong_FromUnsignedLongLong (value);
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
264 #else
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
265 return PyLong_FromUnsignedLong (value);
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
266 #endif
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
267 }
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
268
185
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
269 static void
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
270 octscalar_to_pyobject (boost::python::object& py_object,
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
271 const octave_value& arg)
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
272 {
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
273 PyObject *obj = 0;
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
274
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
275 if (arg.is_complex_type ())
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
276 obj = PyComplex_FromDoubles (arg.real ().double_value (),
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
277 arg.imag ().double_value ());
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
278 else if (arg.is_float_type ())
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
279 obj = PyFloat_FromDouble (arg.double_value ());
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
280
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
281 else if (arg.is_int8_type ())
304
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
282 obj = python_integer_value (arg.int8_scalar_value ().value ());
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
283 else if (arg.is_int16_type ())
304
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
284 obj = python_integer_value (arg.int16_scalar_value ().value ());
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
285 else if (arg.is_int32_type ())
304
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
286 obj = python_integer_value (arg.int32_scalar_value ().value ());
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
287 else if (arg.is_int64_type ())
304
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
288 obj = python_integer_value (arg.int64_scalar_value ().value ());
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
289
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
290 else if (arg.is_uint8_type ())
304
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
291 obj = python_integer_value (arg.uint8_scalar_value ().value ());
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
292 else if (arg.is_uint16_type ())
304
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
293 obj = python_integer_value (arg.uint16_scalar_value ().value ());
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
294 else if (arg.is_uint32_type ())
304
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
295 obj = python_integer_value (arg.uint32_scalar_value ().value ());
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
296 else if (arg.is_uint64_type ())
304
a133564f4af7 Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents: 303
diff changeset
297 obj = python_integer_value (arg.uint64_scalar_value ().value ());
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
298
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
299 else if (arg.is_bool_type ())
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
300 obj = PyBool_FromLong (arg.bool_value ());
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
301
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
302 if (obj)
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
303 py_object = object (handle<PyObject> (obj));
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
304 else
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
305 throw value_convert_exception ("unhandled scalar type");
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
306 }
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
307
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
308 static void
185
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
309 octstring_to_pyobject (boost::python::object& py_object,
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
310 const std::string& str)
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
311 {
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
312 py_object = object (handle<PyObject> (
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
313 #if PY_VERSION_HEX >= 0x03000000
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
314 PyUnicode_FromStringAndSize (str.data (), str.size ())
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
315 #else
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
316 PyString_FromStringAndSize (str.data (), str.size ())
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
317 #endif
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
318 ));
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
319 }
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
320
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
321 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
322 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
323 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
324 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
325 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
326 "Octave value `undefined'. Can not convert 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
327 else if (octvalue.is_string ())
cede17a86ccf Convert Octave char arrays into Python strings (fixes issue #7)
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
328 octstring_to_pyobject (py_object, octvalue.string_value ());
197
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
329 else if (octvalue.is_scalar_type ())
d6c44d680482 Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents: 185
diff changeset
330 octscalar_to_pyobject (py_object, octvalue);
303
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
331 else if (octvalue.is_cell ())
0d94e42bacf6 Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents: 229
diff changeset
332 octcell_to_pyobject (py_object, octvalue.cell_value ());
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
333 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
334 || 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
335 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
336 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
337 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 304
diff changeset
338 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
339 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
340 }
229
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
341 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
342 {
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
343 octave_value_list tmp = feval ("getid", ovl (octvalue), 1);
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
344 std::string hexid = tmp(0).string_value ();
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
345 // FIXME: added a messy ref to __InOct__ in __main__, find a better way
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
346 py_object = boost::python::import ("__main__").attr ("__InOct__")[hexid];
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
347 }
d51b3d120c85 Only attempt to convert an Octave object if it is a pyobject
Mike Miller <mtmiller@octave.org>
parents: 227
diff changeset
348 else
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
349 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
350 "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
351 }
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
352
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
353 void octlist_to_pytuple (boost::python::tuple& python_tuple,
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
354 const octave_value_list& octave_list)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
355 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
356 boost::python::list seq;
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
357 int length = octave_list.length ();
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
358
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
359 for (int i = 0; i < length; i++)
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
360 {
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
361 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
362 octvalue_to_pyobj (py_object, octave_list(i));
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
363 seq.append (py_object);
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
364 }
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
365
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
366 python_tuple = tuple (seq);
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 143
diff changeset
367 }
25
113428ba033c Whitespace fixes
David Grundberg <individ@acc.umu.se>
parents: 21
diff changeset
368 }