Mercurial > pytave
annotate pycall.cc @ 184:8b97647e48f1
maint: whitespace fixes for cset 2b03585d4ddd
* pycall.cc: Improve readability of Python code block.
* python_to_octave.cc (pyobj_to_octvalue): Use space between function name
and opening parenthesis.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Wed, 08 Jun 2016 12:24:59 -0700 |
parents | 2b03585d4ddd |
children | ac377ace2ee4 |
rev | line source |
---|---|
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
1 /* |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
2 |
150
a3dc9d24ae38
maint: Add myself to copyright notices
Mike Miller <mtmiller@octave.org>
parents:
139
diff
changeset
|
3 Copyright (C) 2015-2016 Mike Miller |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
4 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
5 This file is part of Pytave. |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
6 |
139
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
132
diff
changeset
|
7 Pytave is free software: you can redistribute it and/or modify it |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
8 under the terms of the GNU General Public License as published by the |
139
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
132
diff
changeset
|
9 Free Software Foundation, either version 3 of the License, or (at your |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
10 option) any later version. |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
11 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
12 Pytave is distributed in the hope that it will be useful, but WITHOUT |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
15 for more details. |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
16 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
18 along with Pytave; see the file COPYING. If not, see |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
20 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
21 */ |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
22 |
132
386772f4e12d
Clean up includes thanks to Octave header changes
Mike Miller <mtmiller@octave.org>
parents:
116
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
386772f4e12d
Clean up includes thanks to Octave header changes
Mike Miller <mtmiller@octave.org>
parents:
116
diff
changeset
|
24 # include <config.h> |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
25 #endif |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
26 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
27 #include <boost/python.hpp> |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
28 #include <boost/python/numeric.hpp> |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
29 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
30 #include <oct.h> |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
31 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
32 #define PYTAVE_DO_DECLARE_SYMBOL |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
33 #include "arrayobjectdefs.h" |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
34 #include "exceptions.h" |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
35 #include "octave_to_python.h" |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
36 #include "python_to_octave.h" |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
37 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
38 using namespace boost::python; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
39 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
40 DEFUN_DLD (pycall, args, nargout, |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
41 "-*- texinfo -*-\n\ |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
42 @deftypefn {} {} pycall (@var{func})\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
43 @deftypefnx {} {@var{x} =} pycall (@var{func})\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
44 @deftypefnx {} {@var{x} =} pycall (@var{func}, @var{arg1}, @var{arg2}, @dots{})\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
45 Call a Python function or callable, passing Octave values as arguments.\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
46 \n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
47 Examples:\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
48 @example\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
49 @group\n\ |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
50 pycall (\"__builtin__.int\", 6)\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
51 @result{} 6\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
52 pycall (\"os.getuid\")\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
53 @result{} ...\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
54 pycall (\"__builtin__.eval\", \"4+5\")\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
55 @result{} 9\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
56 @end group\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
57 @end example\n\ |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
58 @seealso{pyeval, pyexec}\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
59 @end deftypefn") |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
60 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
61 octave_value_list retval; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
62 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
63 int nargin = args.length (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
64 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
65 if (nargin < 1) |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
66 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
67 print_usage (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
68 return retval; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
69 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
70 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
71 std::string module; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
72 std::string func = args(0).string_value (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
73 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
74 size_t idx = func.rfind ("."); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
75 if (idx != std::string::npos) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
76 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
77 module = func.substr (0, idx); |
116
3a35bb85ce52
py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents:
115
diff
changeset
|
78 func = func.substr (idx + 1); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
79 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
80 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
81 Py_Initialize (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
82 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
83 pytave::init_exceptions (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
84 numeric::array::set_module_and_type ("numpy", "ndarray"); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
85 _import_array (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
86 |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
87 try |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
88 { |
178
9aeb3cd4a288
maint: Back out accidental change
Mike Miller <mtmiller@octave.org>
parents:
177
diff
changeset
|
89 object main_module = import ("__main__"); |
9aeb3cd4a288
maint: Back out accidental change
Mike Miller <mtmiller@octave.org>
parents:
177
diff
changeset
|
90 object main_namespace = main_module.attr ("__dict__"); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
91 |
116
3a35bb85ce52
py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents:
115
diff
changeset
|
92 object mod = (module.empty ()) ? main_module : import (module.c_str ()); |
3a35bb85ce52
py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents:
115
diff
changeset
|
93 object callable = mod.attr (func.c_str ()); |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
94 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
95 std::vector<object> pyargs; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
96 for (int i = 1; i < nargin; i++) |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
97 { |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
98 object arg; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
99 pytave::octvalue_to_pyobj (arg, args(i)); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
100 pyargs.push_back (arg); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
101 } |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
102 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
103 object res; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
104 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
105 switch (nargin - 1) |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
106 { |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
107 case 0: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
108 res = callable (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
109 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
110 case 1: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
111 res = callable (pyargs[0]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
112 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
113 case 2: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
114 res = callable (pyargs[0], pyargs[1]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
115 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
116 case 3: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
117 res = callable (pyargs[0], pyargs[1], pyargs[2]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
118 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
119 case 4: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
120 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
121 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
122 case 5: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
123 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
124 pyargs[4]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
125 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
126 case 6: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
127 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
128 pyargs[4], pyargs[5]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
129 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
130 case 7: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
131 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
132 pyargs[4], pyargs[5], pyargs[6]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
133 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
134 case 8: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
135 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
136 pyargs[4], pyargs[5], pyargs[6], pyargs[7]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
137 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
138 case 9: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
139 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
140 pyargs[4], pyargs[5], pyargs[6], pyargs[7], |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
141 pyargs[8]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
142 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
143 case 10: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
144 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
145 pyargs[4], pyargs[5], pyargs[6], pyargs[7], |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
146 pyargs[8], pyargs[9]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
147 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
148 default: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
149 error ("pycall: more than 10 arguments are not yet supported"); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
150 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
151 } |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
152 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
153 if (! res.is_none ()) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
154 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
155 octave_value val; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
156 pytave::pyobj_to_octvalue (val, res); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
157 retval(0) = val; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
158 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
159 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
160 catch (pytave::object_convert_exception const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
161 { |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
162 error ("pycall: error in return value type conversion"); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
163 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
164 catch (error_already_set const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
165 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
166 PyObject *ptype, *pvalue, *ptraceback; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
167 PyErr_Fetch (&ptype, &pvalue, &ptraceback); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
168 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
169 try |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
170 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
171 std::string message = extract<std::string> (pvalue); |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
172 error ("pycall: %s", message.c_str ()); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
173 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
174 catch (error_already_set const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
175 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
176 PyErr_Restore (ptype, pvalue, ptraceback); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
177 PyErr_Print (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
178 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
179 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
180 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
181 return retval; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
182 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
183 |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
184 /* |
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
185 %!test |
182
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
186 %! pyexec ("def pyfunc(x):\n return 2*x"); |
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
187 %! z = pycall ("pyfunc", [20 20]); |
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
188 %! assert (z, [40 40]) |
183
2b03585d4ddd
Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
182
diff
changeset
|
189 |
2b03585d4ddd
Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
182
diff
changeset
|
190 %!test |
184
8b97647e48f1
maint: whitespace fixes for cset 2b03585d4ddd
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
191 %! pyexec (["def pyfunc(x):\n" ... |
8b97647e48f1
maint: whitespace fixes for cset 2b03585d4ddd
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
192 %! " if x.item((0,0)) is True:\n return 30\n" ... |
8b97647e48f1
maint: whitespace fixes for cset 2b03585d4ddd
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
193 %! " elif x.item((0,0)) is False:\n return 20\n" ... |
8b97647e48f1
maint: whitespace fixes for cset 2b03585d4ddd
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
194 %! " else:\n return 10"]); |
183
2b03585d4ddd
Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
182
diff
changeset
|
195 %! assert (pycall ("pyfunc", true), 30) |
2b03585d4ddd
Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
182
diff
changeset
|
196 %! assert (pycall ("pyfunc", false), 20) |
2b03585d4ddd
Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
182
diff
changeset
|
197 %! assert (pycall ("pyfunc", 10), 10) |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
198 */ |