Mercurial > pytave
annotate pycall.cc @ 304:a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
* octave_to_python.cc (pytave::python_integer_value): New functions to construct
appropriate Python int or long types based on integer size and Python version.
(pytave::octscalar_to_pyobject): Use them.
* pycall.cc: Add %!tests for conversion of integer types into Python.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Mon, 08 Aug 2016 16:17:21 -0700 |
parents | 0d94e42bacf6 |
children | 616ec5f18d95 |
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 |
234
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
30 #include <octave/oct.h> |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
31 #include <octave/parse.h> |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
32 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
33 #define PYTAVE_DO_DECLARE_SYMBOL |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
34 #include "arrayobjectdefs.h" |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
35 #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
|
36 #include "octave_to_python.h" |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
37 #include "python_to_octave.h" |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
38 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
39 using namespace boost::python; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
40 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
41 DEFUN_DLD (pycall, args, nargout, |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
42 "-*- texinfo -*-\n\ |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
43 @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
|
44 @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
|
45 @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
|
46 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
|
47 \n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
48 Examples:\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
49 @example\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
50 @group\n\ |
228
2a9956be24aa
maint: update pycall doc string examples to reflect recent changes
Mike Miller <mtmiller@octave.org>
parents:
226
diff
changeset
|
51 pycall (\"int\", 6)\n\ |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
52 @result{} 6\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
53 pycall (\"os.getuid\")\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
54 @result{} ...\n\ |
228
2a9956be24aa
maint: update pycall doc string examples to reflect recent changes
Mike Miller <mtmiller@octave.org>
parents:
226
diff
changeset
|
55 pycall (\"math.sqrt\", 2)\n\ |
2a9956be24aa
maint: update pycall doc string examples to reflect recent changes
Mike Miller <mtmiller@octave.org>
parents:
226
diff
changeset
|
56 @result{} 1.4142\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
57 @end group\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
58 @end example\n\ |
241
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
59 \n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
60 If the callable has no return, and an lvalue is specified, it will be set\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
61 to @code{None}. However, if no lvalue was specified, @code{ans} will not\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
62 be set. For example:\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
63 @example\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
64 @group\n\ |
242 | 65 s = pyeval (\"set([1, 2])\");\n\ |
241
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
66 pycall (s.add, 3)\n\ |
242 | 67 \n\ |
241
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
68 r = pycall (s.add, 4)\n\ |
242 | 69 @result{} r = [pyobject ...]\n\ |
241
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
70 \n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
71 None\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
72 \n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
73 @end group\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
74 @end example\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
75 \n\ |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
76 @seealso{pyeval, pyexec}\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
77 @end deftypefn") |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
78 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
79 octave_value_list retval; |
235
bbf0b7a4e117
Return pyobject from pycall too, similar to pyeval (fixes issue #28)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
234
diff
changeset
|
80 object res; |
bbf0b7a4e117
Return pyobject from pycall too, similar to pyeval (fixes issue #28)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
234
diff
changeset
|
81 std::string id; |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
82 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
83 int nargin = args.length (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
84 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
85 if (nargin < 1) |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
86 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
87 print_usage (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
88 return retval; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
89 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
90 |
234
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
91 bool func_by_name = false; |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
92 |
234
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
93 if (args(0).is_string ()) |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
94 func_by_name = true; |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
95 else if (args(0).is_object () && args(0).class_name () == "pyobject") |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
96 func_by_name = false; |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
97 else |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
98 error ("pycall: FUNC must be a string or a Python reference"); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
99 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
100 Py_Initialize (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
101 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
102 pytave::init_exceptions (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
103 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
|
104 _import_array (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
105 |
226
382bb1d91239
maint: import standard Python names consistently
Mike Miller <mtmiller@octave.org>
parents:
198
diff
changeset
|
106 object main_module = import ("__main__"); |
235
bbf0b7a4e117
Return pyobject from pycall too, similar to pyeval (fixes issue #28)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
234
diff
changeset
|
107 object main_namespace = main_module.attr ("__dict__"); |
226
382bb1d91239
maint: import standard Python names consistently
Mike Miller <mtmiller@octave.org>
parents:
198
diff
changeset
|
108 #if PY_VERSION_HEX >= 0x03000000 |
382bb1d91239
maint: import standard Python names consistently
Mike Miller <mtmiller@octave.org>
parents:
198
diff
changeset
|
109 object builtins_module = import ("builtins"); |
382bb1d91239
maint: import standard Python names consistently
Mike Miller <mtmiller@octave.org>
parents:
198
diff
changeset
|
110 #else |
382bb1d91239
maint: import standard Python names consistently
Mike Miller <mtmiller@octave.org>
parents:
198
diff
changeset
|
111 object builtins_module = import ("__builtin__"); |
382bb1d91239
maint: import standard Python names consistently
Mike Miller <mtmiller@octave.org>
parents:
198
diff
changeset
|
112 #endif |
382bb1d91239
maint: import standard Python names consistently
Mike Miller <mtmiller@octave.org>
parents:
198
diff
changeset
|
113 |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
114 try |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
115 { |
234
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
116 object callable; |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
117 |
234
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
118 if (func_by_name) |
191
0500459a739b
Enforce checking of attributes in builtins for pycall (fixes issue #20)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
119 { |
234
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
120 std::string module; |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
121 std::string func = args(0).string_value (); |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
122 |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
123 size_t idx = func.rfind ("."); |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
124 if (idx != std::string::npos) |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
125 { |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
126 module = func.substr (0, idx); |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
127 func = func.substr (idx + 1); |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
128 } |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
129 |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
130 object mod; |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
131 if (module.empty ()) |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
132 { |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
133 if (PyObject_HasAttrString (main_module.ptr (), func.c_str ())) |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
134 mod = main_module; |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
135 else |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
136 mod = builtins_module; |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
137 } |
191
0500459a739b
Enforce checking of attributes in builtins for pycall (fixes issue #20)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
138 else |
234
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
139 mod = import (module.c_str ()); |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
140 |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
141 callable = mod.attr (func.c_str ()); |
191
0500459a739b
Enforce checking of attributes in builtins for pycall (fixes issue #20)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
142 } |
192
9077907490d8
pycall: clean up determination of which module to import from
Mike Miller <mtmiller@octave.org>
parents:
191
diff
changeset
|
143 else |
234
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
144 { |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
145 octave_value_list tmp = feval ("getid", ovl (args(0)), 1); |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
146 std::string hexid = tmp(0).string_value (); |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
147 callable = main_module.attr ("__InOct__")[hexid]; |
237b71aaf6e7
pycall: support Python reference to a callable object
Mike Miller <mtmiller@octave.org>
parents:
232
diff
changeset
|
148 } |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
149 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
150 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
|
151 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
|
152 { |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
153 object arg; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
154 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
|
155 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
|
156 } |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
157 |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
158 switch (nargin - 1) |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
159 { |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
160 case 0: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
161 res = callable (); |
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 case 1: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
164 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
|
165 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
166 case 2: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
167 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
|
168 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
169 case 3: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
170 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
|
171 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
172 case 4: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
173 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
|
174 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
175 case 5: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
176 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
|
177 pyargs[4]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
178 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
179 case 6: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
180 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
|
181 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
|
182 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
183 case 7: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
184 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
|
185 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
|
186 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
187 case 8: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
188 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
|
189 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
|
190 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
191 case 9: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
192 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
|
193 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
|
194 pyargs[8]); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
195 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
196 case 10: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
197 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
|
198 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
|
199 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
|
200 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
201 default: |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
202 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
|
203 break; |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
204 } |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
205 |
239
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
206 // Ensure reasonable "ans" behaviour, consistent with Python's "_". |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
207 if (nargout > 0 || ! res.is_none ()) |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
208 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
209 octave_value val; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
210 pytave::pyobj_to_octvalue (val, res); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
211 retval(0) = val; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
212 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
213 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
214 catch (pytave::object_convert_exception const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
215 { |
278
9278a272b1c8
Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
248
diff
changeset
|
216 error ("pyexec: error in return value type conversion"); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
217 } |
193
28173dc00d51
Add catch for value_convert_exception and a test. (fixes issue #16)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
218 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
|
219 { |
195
459b6f5c56ed
maint: prefer term "argument" over "parameter" for values passed into a function
Mike Miller <mtmiller@octave.org>
parents:
194
diff
changeset
|
220 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
|
221 } |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
222 catch (error_already_set const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
223 { |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
224 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
|
225 error ("pycall: %s", message.c_str ()); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
226 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
227 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
228 return retval; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
229 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
230 |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
231 /* |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
232 %!assert (ischar (pycall ("os.getcwd"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
233 %!assert (isreal (pycall ("random.random"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
234 %!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
|
235 %!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
|
236 %!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
|
237 %!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
|
238 %!assert (pycall ("int", 10.2), 10) |
235
bbf0b7a4e117
Return pyobject from pycall too, similar to pyeval (fixes issue #28)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
234
diff
changeset
|
239 %!assert (isa (pycall ("object"), "pyobject")) |
281
b06df69f4b37
Remove unused code and add tests to pycall
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
279
diff
changeset
|
240 %!assert (isa (pycall ("dict"), "pyobject")) |
b06df69f4b37
Remove unused code and add tests to pycall
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
279
diff
changeset
|
241 %!assert (isa (pycall ("list"), "pyobject")) |
b06df69f4b37
Remove unused code and add tests to pycall
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
279
diff
changeset
|
242 %!assert (isa (pycall ("tuple"), "pyobject")) |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
243 |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
244 ## 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
|
245 %!test |
230
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
246 %! pyexec (["def typename(x):\n" ... |
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
247 %! " s = type(x).__name__\n" ... |
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
248 %! " if s == 'long':\n" ... |
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
249 %! " return 'int'\n" ... |
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
250 %! " return s"]); |
197
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
251 %!assert (pycall ("typename", 0), "float") |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
252 %!assert (pycall ("typename", pi), "float") |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
253 %!assert (pycall ("typename", 2j), "complex") |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
254 %!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
|
255 %!assert (pycall ("typename", false), "bool") |
d6c44d680482
Convert Octave scalars into Python scalars (fixes issue #14)
Mike Miller <mtmiller@octave.org>
parents:
195
diff
changeset
|
256 %!assert (pycall ("typename", true), "bool") |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
257 %!assert (pycall ("typename", "Hello world"), "str") |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
258 %!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
|
259 |
303
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
260 ## Test construction of sequence types from cell arrays |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
261 %!assert (char (pycall ("list")), "[]") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
262 %!assert (char (pycall ("list", {})), "[]") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
263 %!assert (char (pycall ("list", {1, 2, 3})), "[1.0, 2.0, 3.0]") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
264 %!assert (char (pycall ("list", {int8(1), int8(2), int8(3)})), "[1, 2, 3]") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
265 %!assert (char (pycall ("tuple")), "()") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
266 %!assert (char (pycall ("tuple", {})), "()") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
267 %!assert (char (pycall ("tuple", {1, 2, 3})), "(1.0, 2.0, 3.0)") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
268 %!assert (char (pycall ("tuple", {int8(1), int8(2), int8(3)})), "(1, 2, 3)") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
269 %!error (pycall ("list", {1, 2, 3; 4, 5, 6})) |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
270 %!error (pycall ("dict", {1, 2, 3})) |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
271 |
198
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
272 ## 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
|
273 %!test |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
274 %! 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
|
275 %! 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
|
276 %! [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
|
277 %! 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
|
278 %! 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
|
279 %! endfor |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
280 |
304
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
281 ## Test conversion of integer types into Python |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
282 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 0"), int8 (0))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
283 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == -2**7 "), intmin ("int8"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
284 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 2**7 -1"), intmax ("int8"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
285 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 0"), intmin ("uint8"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
286 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 2**8 -1"), intmax ("uint8"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
287 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 0"), int16 (0))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
288 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == -2**15 "), intmin ("int16"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
289 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 2**15-1"), intmax ("int16"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
290 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 0"), intmin ("uint16"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
291 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 2**16-1"), intmax ("uint16"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
292 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 0"), int32 (0))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
293 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == -2**31 "), intmin ("int32"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
294 %!assert (pycall (pyeval ("lambda x: type(x) == type(0) and x == 2**31-1"), intmax ("int32"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
295 %!assert (pycall (pyeval ("lambda x: type(x) == type(2**64) and x == 0"), intmin ("uint32"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
296 %!assert (pycall (pyeval ("lambda x: type(x) == type(2**64) and x == 2**32-1"), intmax ("uint32"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
297 %!assert (pycall (pyeval ("lambda x: type(x) == type(2**64) and x == 0"), int64 (0))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
298 %!assert (pycall (pyeval ("lambda x: type(x) == type(2**64) and x == -2**63 "), intmin ("int64"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
299 %!assert (pycall (pyeval ("lambda x: type(x) == type(2**64) and x == 2**63-1"), intmax ("int64"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
300 %!assert (pycall (pyeval ("lambda x: type(x) == type(2**64) and x == 0"), intmin ("uint64"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
301 %!assert (pycall (pyeval ("lambda x: type(x) == type(2**64) and x == 2**64-1"), intmax ("uint64"))) |
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
302 |
195
459b6f5c56ed
maint: prefer term "argument" over "parameter" for values passed into a function
Mike Miller <mtmiller@octave.org>
parents:
194
diff
changeset
|
303 %!error <argument type conversion> |
263
0fc1a58d7e89
Delete unnecessary newlines in one-line function definitions
Mike Miller <mtmiller@octave.org>
parents:
248
diff
changeset
|
304 %! pyexec ("def intwrapper(x): return int(x)"); |
193
28173dc00d51
Add catch for value_convert_exception and a test. (fixes issue #16)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
305 %! 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
|
306 |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
307 %!test |
263
0fc1a58d7e89
Delete unnecessary newlines in one-line function definitions
Mike Miller <mtmiller@octave.org>
parents:
248
diff
changeset
|
308 %! pyexec ("def pyfunc(x): return 2*x"); |
182
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
309 %! z = pycall ("pyfunc", [20 20]); |
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
310 %! 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
|
311 |
2b03585d4ddd
Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
182
diff
changeset
|
312 %!test |
184
8b97647e48f1
maint: whitespace fixes for cset 2b03585d4ddd
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
313 %! 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
|
314 %! " 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
|
315 %! " 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
|
316 %! " 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
|
317 %! 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
|
318 %! 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
|
319 %! 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
|
320 |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
321 %!error <NameError> |
263
0fc1a58d7e89
Delete unnecessary newlines in one-line function definitions
Mike Miller <mtmiller@octave.org>
parents:
248
diff
changeset
|
322 %! pyexec ("def raiseException(): raise NameError('oops')") |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
323 %! pycall ("raiseException") |
239
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
324 |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
325 ## None as a return value |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
326 %!test |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
327 %! f = pyeval ("lambda: None"); |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
328 %! r = pycall (f); |
248
2e4c9ce0c83c
maint: prefer Octave lowercase naming convention to camelCase
Mike Miller <mtmiller@octave.org>
parents:
242
diff
changeset
|
329 %! is_none = pyeval ("lambda x: x is None"); |
2e4c9ce0c83c
maint: prefer Octave lowercase naming convention to camelCase
Mike Miller <mtmiller@octave.org>
parents:
242
diff
changeset
|
330 %! assert (is_none (r)) |
239
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
331 |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
332 ## But returning None will not set "ans" |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
333 %!test |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
334 %! f = pyeval ("lambda: None"); |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
335 %! clear ans |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
336 %! pycall (f); |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
337 %! assert (! exist ("ans", "var")) |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
338 */ |