Mercurial > pytave
annotate pycall.cc @ 231:6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
* exceptions.{h, cc}: Add a new method to properly extract string for a python
exception. Also add a new error message if we can't extract the error string.
* pycall.cc, pyeval.cc, pyexec.cc: Use the new method to extract string when an
exception occurs.
author | Abhinav Tripathi <genuinelucifer@gmail.com> |
---|---|
date | Sat, 16 Jul 2016 05:50:36 -0700 |
parents | 377f2dc057ea |
children | c02f0a4c92e7 |
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__"); |
192
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
90 #if PY_VERSION_HEX >= 0x03000000 |
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
91 object builtins_module = import ("builtins"); |
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
92 #else |
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
93 object builtins_module = import ("__builtin__"); |
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
94 #endif |
191
0500459a739b
Enforce checking of attributes in builtins for pycall (fixes issue #20)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
95 |
192
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
96 object mod; |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
97 |
192
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
98 if (module.empty ()) |
191
0500459a739b
Enforce checking of attributes in builtins for pycall (fixes issue #20)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
99 { |
192
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
100 if (PyObject_HasAttrString (main_module.ptr (), func.c_str ())) |
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
101 mod = main_module; |
191
0500459a739b
Enforce checking of attributes in builtins for pycall (fixes issue #20)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
102 else |
192
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
103 mod = builtins_module; |
191
0500459a739b
Enforce checking of attributes in builtins for pycall (fixes issue #20)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
104 } |
192
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
105 else |
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
106 mod = import (module.c_str ()); |
191
0500459a739b
Enforce checking of attributes in builtins for pycall (fixes issue #20)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
107 |
116
3a35bb85ce52
py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents:
115
diff
changeset
|
108 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
|
109 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
110 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
|
111 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
|
112 { |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
113 object arg; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
114 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
|
115 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
|
116 } |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
117 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
118 object res; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
119 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
120 switch (nargin - 1) |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
121 { |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
122 case 0: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
123 res = callable (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
124 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
125 case 1: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
126 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
|
127 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
128 case 2: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
129 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
|
130 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
131 case 3: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
132 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
|
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 4: |
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 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
137 case 5: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
138 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
|
139 pyargs[4]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
140 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
141 case 6: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
142 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
|
143 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
|
144 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
145 case 7: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
146 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
|
147 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
|
148 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
149 case 8: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
150 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
|
151 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
|
152 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
153 case 9: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
154 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
|
155 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
|
156 pyargs[8]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
157 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
158 case 10: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
163 default: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
164 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
|
165 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
166 } |
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 if (! res.is_none ()) |
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 octave_value val; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
171 pytave::pyobj_to_octvalue (val, res); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
172 retval(0) = val; |
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 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
175 catch (pytave::object_convert_exception const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
176 { |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
177 error ("pycall: error in return value type conversion"); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
178 } |
193
28173dc00d51
Add catch for value_convert_exception and a test. (fixes issue #16)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
179 catch (pytave::value_convert_exception const &) |
28173dc00d51
Add catch for value_convert_exception and a test. (fixes issue #16)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
180 { |
195
459b6f5c56ed
maint: prefer term "argument" over "parameter" for values passed into a function
Mike Miller <mtmiller@octave.org>
parents:
194
diff
changeset
|
181 error ("pycall: error in argument type conversion"); |
193
28173dc00d51
Add catch for value_convert_exception and a test. (fixes issue #16)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
182 } |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
183 catch (error_already_set const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
184 { |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
185 std::string message = pytave::fetch_exception_message (); |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
186 error ("pycall: %s", message.c_str ()); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
187 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
188 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
189 return retval; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
190 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
191 |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
192 /* |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
193 %!assert (ischar (pycall ("os.getcwd"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
194 %!assert (isreal (pycall ("random.random"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
195 %!assert (pycall ("math.exp", 3), exp (3)) |
197
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
196 %!assert (pycall ("math.trunc", pi), fix (pi)) |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
197 %!assert (pycall ("math.sqrt", 2), sqrt (2)) |
197
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
198 %!assert (pycall ("cmath.sqrt", 2j), sqrt (2j)) |
191
0500459a739b
Enforce checking of attributes in builtins for pycall (fixes issue #20)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
199 %!assert (pycall ("int", 10.2), 10) |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
200 |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
201 ## Test argument type conversion of values into Python |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
202 %!test |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
203 %! pyexec ("def typename(x): return type(x).__name__"); |
197
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
204 %!assert (pycall ("typename", 0), "float") |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
205 %!assert (pycall ("typename", pi), "float") |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
206 %!assert (pycall ("typename", 2j), "complex") |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
207 %!assert (pycall ("typename", int32 (0)), "int") |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
208 %!assert (pycall ("typename", false), "bool") |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
209 %!assert (pycall ("typename", true), "bool") |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
210 %!assert (pycall ("typename", "Hello world"), "str") |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
211 %!assert (pycall ("typename", char ([1, 2, 3])), "str") |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
212 |
198
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
213 ## Test round trip type preservation / conversion |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
214 %!test |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
215 %! pyexec ("def roundtrip(x): return x"); |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
216 %! values = { 0, pi, 2j, eps, false, true, version, "Hello world", ... |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
217 %! [1, 2, 3], eye (4) }; |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
218 %! for i = 1:numel (values) |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
219 %! assert (pycall ("roundtrip", values{i}), values{i}); |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
220 %! endfor |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
221 |
195
459b6f5c56ed
maint: prefer term "argument" over "parameter" for values passed into a function
Mike Miller <mtmiller@octave.org>
parents:
194
diff
changeset
|
222 %!error <argument type conversion> |
193
28173dc00d51
Add catch for value_convert_exception and a test. (fixes issue #16)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
223 %! pyexec ("def intwrapper(x):\n return int(x)\n"); |
28173dc00d51
Add catch for value_convert_exception and a test. (fixes issue #16)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
224 %! pycall ("intwrapper", ftp ()); |
28173dc00d51
Add catch for value_convert_exception and a test. (fixes issue #16)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
225 |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
226 %!test |
182
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
227 %! 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
|
228 %! z = pycall ("pyfunc", [20 20]); |
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
229 %! 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
|
230 |
2b03585d4ddd
Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
182
diff
changeset
|
231 %!test |
184
8b97647e48f1
maint: whitespace fixes for cset 2b03585d4ddd
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
232 %! pyexec (["def pyfunc(x):\n" ... |
197
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
233 %! " if x is True:\n return 30\n" ... |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
234 %! " elif x is False:\n return 20\n" ... |
184
8b97647e48f1
maint: whitespace fixes for cset 2b03585d4ddd
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
235 %! " 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
|
236 %! 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
|
237 %! 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
|
238 %! assert (pycall ("pyfunc", 10), 10) |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
239 |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
240 %!error <NameError> |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
241 %! pyexec ("def raiseException ():\n raise NameError ('oops')") |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
242 %! pycall ("raiseException") |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
243 */ |