Mercurial > pytave
annotate pycall.cc @ 172:29d9da90afcf
pycall: New function to pass Octave values to a Python function
* pycall.cc: Rename from py.cc. Add support for dispatching variable
length Octave arguments to a Python function.
* Makefile.am (OCT_FILES): Rename py.oct to pycall.oct.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 07 Apr 2016 14:51:18 -0700 |
parents | py.cc@eab5c6026303 |
children | 28dc607532c2 |
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\ |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
42 @deftypefn {Loadable Function} pycall (@var{func})\n\ |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
43 @deftypefnx {Loadable Function} {@var{x} =} pycall (@var{func})\n\ |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
44 @deftypefnx {Loadable Function} {@var{x} =} pycall (@var{func}, @var{arg1}, @var{arg2}, @dots{})\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
45 Execute method of a Python module.\n\ |
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\ |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
50 y = pycall('__builtin__.int(6)')\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
51 @result{} y = 6\n\ |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
52 pycall('sys.version')\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
53 @result{} ans = ...\n\ |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
54 pycall('__builtin__.eval(\"4+5\")')\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
55 @result{} ans = 9\n\ |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
56 pycall('__builtin__.dict(one=1,two=2)')\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
57 @result{} ans =\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
58 scalar structure containing the fields:\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
59 two = 2\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
60 one = 1\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
61 @end group\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
62 @end example\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
63 @end deftypefn") |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
64 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
65 octave_value_list retval; |
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 int nargin = args.length (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
68 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
69 if (nargin < 1) |
115
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 print_usage (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
72 return retval; |
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 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
75 std::string module; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
76 std::string func = args(0).string_value (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
77 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
78 size_t idx = func.rfind ("."); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
79 if (idx != std::string::npos) |
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 module = func.substr (0, idx); |
116
3a35bb85ce52
py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents:
115
diff
changeset
|
82 func = func.substr (idx + 1); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
83 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
84 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
85 Py_Initialize (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
86 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
87 pytave::init_exceptions (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
88 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
|
89 _import_array (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
90 |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
91 try |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
92 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
93 object main_module = import ("__main__"); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
94 object main_namespace = main_module.attr ("__dict__"); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
95 |
116
3a35bb85ce52
py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents:
115
diff
changeset
|
96 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
|
97 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
|
98 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
99 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
|
100 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
|
101 { |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
102 object arg; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
103 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
|
104 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
|
105 } |
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 object res; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
108 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
109 switch (nargin - 1) |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
110 { |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
111 case 0: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
112 res = callable (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
113 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
114 case 1: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
115 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
|
116 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
117 case 2: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
118 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
|
119 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
120 case 3: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
121 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
|
122 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
123 case 4: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
124 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
|
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 5: |
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]); |
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 6: |
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]); |
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 7: |
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]); |
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 8: |
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 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
142 case 9: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
143 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
|
144 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
|
145 pyargs[8]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
146 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
147 case 10: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
148 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
|
149 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
|
150 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
|
151 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
152 default: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
153 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
|
154 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
155 } |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
156 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
157 if (! res.is_none ()) |
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 octave_value val; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
160 pytave::pyobj_to_octvalue (val, res); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
161 retval(0) = val; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
162 } |
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 (pytave::object_convert_exception const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
165 { |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
166 error ("pycall: error in return value type conversion"); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
167 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
168 catch (error_already_set const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
169 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
170 PyObject *ptype, *pvalue, *ptraceback; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
171 PyErr_Fetch (&ptype, &pvalue, &ptraceback); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
172 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
173 try |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
174 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
175 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
|
176 error ("pycall: %s", message.c_str ()); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
177 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
178 catch (error_already_set const &) |
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 PyErr_Restore (ptype, pvalue, ptraceback); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
181 PyErr_Print (); |
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 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
184 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
185 return retval; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
186 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
187 |