Mercurial > pytave
annotate pycall.cc @ 399:692cecebc71f
Error on attempt to convert a char array to Python str (fixes issue #75)
* octave_to_python.cc (pytave::octvalue_to_pyobj): Throw an exception on
attempt to convert a char array with more than one row.
* pycall.cc: Add %!tests.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Fri, 28 Apr 2017 10:13:04 -0700 |
parents | 55cd61fe519a |
children | 3644df6564bc |
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" |
351
040aff46e4db
Refactor calling a Python function into a set of wrapper functions
Mike Miller <mtmiller@octave.org>
parents:
334
diff
changeset
|
36 #include "oct-py-eval.h" |
331
cee203ea6245
Rename oct-py-util.cc from pytave_utils.cc
Mike Miller <mtmiller@octave.org>
parents:
328
diff
changeset
|
37 #include "oct-py-util.h" |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
38 #include "octave_to_python.h" |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
39 #include "python_to_octave.h" |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
40 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
41 using namespace boost::python; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
42 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
43 DEFUN_DLD (pycall, args, nargout, |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
44 "-*- texinfo -*-\n\ |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
45 @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
|
46 @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
|
47 @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
|
48 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
|
49 \n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
50 Examples:\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
51 @example\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
52 @group\n\ |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
309
diff
changeset
|
53 pycall (\"float\", 6)\n\ |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
54 @result{} 6\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
55 pycall (\"os.getuid\")\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
56 @result{} ...\n\ |
228
2a9956be24aa
maint: update pycall doc string examples to reflect recent changes
Mike Miller <mtmiller@octave.org>
parents:
226
diff
changeset
|
57 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
|
58 @result{} 1.4142\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
59 @end group\n\ |
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
60 @end example\n\ |
241
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
61 \n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
62 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
|
63 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
|
64 be set. For example:\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
65 @example\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
66 @group\n\ |
242 | 67 s = pyeval (\"set([1, 2])\");\n\ |
241
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
68 pycall (s.add, 3)\n\ |
242 | 69 \n\ |
241
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
70 r = pycall (s.add, 4)\n\ |
242 | 71 @result{} r = [pyobject ...]\n\ |
241
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 None\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
74 \n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
75 @end group\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
76 @end example\n\ |
73c0b59a0ee9
Add documentation for pycall returning None
Colin Macdonald <cbm@m.fsf.org>
parents:
239
diff
changeset
|
77 \n\ |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
172
diff
changeset
|
78 @seealso{pyeval, pyexec}\n\ |
160
eab5c6026303
Add some examples to the documentation
Colin Macdonald <cbm@m.fsf.org>
parents:
150
diff
changeset
|
79 @end deftypefn") |
115
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 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
|
82 std::string id; |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
83 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
84 int nargin = args.length (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
85 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
86 if (nargin < 1) |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
87 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
88 print_usage (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
89 return retval; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
90 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
91 |
359
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
92 if (! (args(0).is_string () || (args(0).is_object () |
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
93 && args(0).class_name () == "pyobject"))) |
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
94 error ("pycall: FUNC must be a string or a Python reference"); |
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
95 |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
96 Py_Initialize (); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
97 |
172
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
98 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
|
99 _import_array (); |
29d9da90afcf
pycall: New function to pass Octave values to a Python function
Mike Miller <mtmiller@octave.org>
parents:
160
diff
changeset
|
100 |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
101 try |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
102 { |
359
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
103 PyObject *callable = 0; |
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
104 if (args(0).is_string ()) |
364
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
359
diff
changeset
|
105 { |
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
359
diff
changeset
|
106 callable = pytave::py_find_function (args(0).string_value ()); |
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
359
diff
changeset
|
107 if (! callable) |
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
359
diff
changeset
|
108 error ("pycall: no such Python function or callable: %s", |
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
359
diff
changeset
|
109 args(0).string_value ().c_str ()); |
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
359
diff
changeset
|
110 } |
359
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
111 else |
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
112 { |
364
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
359
diff
changeset
|
113 callable = pytave::pyobject_unwrap_object (args(0)); |
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
359
diff
changeset
|
114 if (! callable) |
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
359
diff
changeset
|
115 error("pycall: FUNC must be a valid Python reference"); |
359
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
116 } |
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
117 |
351
040aff46e4db
Refactor calling a Python function into a set of wrapper functions
Mike Miller <mtmiller@octave.org>
parents:
334
diff
changeset
|
118 octave_value_list arglist = args.slice (1, nargin - 1); |
359
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
119 PyObject *result = pytave::py_call_function (callable, arglist); |
305
616ec5f18d95
pycall: refactor to call function with arbitrary number of positional args
Mike Miller <mtmiller@octave.org>
parents:
304
diff
changeset
|
120 object res = object (handle<PyObject> (result)); |
359
4c2b748eaea0
Use py_find_function where specifically searching for a function by name
Mike Miller <mtmiller@octave.org>
parents:
352
diff
changeset
|
121 Py_DECREF (callable); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
122 |
239
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
123 // Ensure reasonable "ans" behaviour, consistent with Python's "_". |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
124 if (nargout > 0 || ! res.is_none ()) |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
125 { |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
126 octave_value val; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
127 pytave::pyobj_to_octvalue (val, res); |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
128 retval(0) = val; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
129 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
130 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
131 catch (pytave::object_convert_exception const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
132 { |
278
9278a272b1c8
Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
248
diff
changeset
|
133 error ("pyexec: error in return value type conversion"); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
134 } |
193
28173dc00d51
Add catch for value_convert_exception and a test. (fixes issue #16)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
190
diff
changeset
|
135 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
|
136 { |
195
459b6f5c56ed
maint: prefer term "argument" over "parameter" for values passed into a function
Mike Miller <mtmiller@octave.org>
parents:
194
diff
changeset
|
137 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
|
138 } |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
139 catch (error_already_set const &) |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
140 { |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
141 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
|
142 error ("pycall: %s", message.c_str ()); |
115
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
143 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
144 |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
145 return retval; |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
146 } |
00d19f71c9ca
Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
147 |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
148 /* |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
149 %!assert (isreal (pycall ("random.random"))) |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
309
diff
changeset
|
150 %!assert (double (pycall ("math.exp", 3)), exp (3)) |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
309
diff
changeset
|
151 %!assert (double (pycall ("math.trunc", pi)), fix (pi)) |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
309
diff
changeset
|
152 %!assert (double (pycall ("math.sqrt", 2)), sqrt (2)) |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
309
diff
changeset
|
153 %!assert (double (pycall ("cmath.sqrt", 2j)), sqrt (2j)) |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
309
diff
changeset
|
154 %!assert (double (pycall ("int", 10.2)), 10) |
365
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
155 %!assert (isa (pycall ("os.getcwd"), "pyobject")) |
235
bbf0b7a4e117
Return pyobject from pycall too, similar to pyeval (fixes issue #28)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
234
diff
changeset
|
156 %!assert (isa (pycall ("object"), "pyobject")) |
281
b06df69f4b37
Remove unused code and add tests to pycall
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
279
diff
changeset
|
157 %!assert (isa (pycall ("dict"), "pyobject")) |
b06df69f4b37
Remove unused code and add tests to pycall
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
279
diff
changeset
|
158 %!assert (isa (pycall ("list"), "pyobject")) |
b06df69f4b37
Remove unused code and add tests to pycall
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
279
diff
changeset
|
159 %!assert (isa (pycall ("tuple"), "pyobject")) |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
160 |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
161 ## 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
|
162 %!test |
230
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
163 %! pyexec (["def typename(x):\n" ... |
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
164 %! " s = type(x).__name__\n" ... |
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
165 %! " if s == 'long':\n" ... |
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
166 %! " return 'int'\n" ... |
262832ae684f
pycall: fix test failure with Python 2
Mike Miller <mtmiller@octave.org>
parents:
228
diff
changeset
|
167 %! " return s"]); |
365
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
168 %!assert (char (pycall ("typename", 0)), "float") |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
169 %!assert (char (pycall ("typename", pi)), "float") |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
170 %!assert (char (pycall ("typename", 2j)), "complex") |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
171 %!assert (char (pycall ("typename", int32 (0))), "int") |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
172 %!assert (char (pycall ("typename", false)), "bool") |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
173 %!assert (char (pycall ("typename", true)), "bool") |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
174 %!assert (char (pycall ("typename", "Hello world")), "str") |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
175 %!assert (char (pycall ("typename", char ([1, 2, 3]))), "str") |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
184
diff
changeset
|
176 |
303
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
177 ## 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
|
178 %!assert (char (pycall ("list")), "[]") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
179 %!assert (char (pycall ("list", {})), "[]") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
180 %!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
|
181 %!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
|
182 %!assert (char (pycall ("tuple")), "()") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
183 %!assert (char (pycall ("tuple", {})), "()") |
0d94e42bacf6
Convert Octave cell array to Python list (fixes issue #41)
Mike Miller <mtmiller@octave.org>
parents:
281
diff
changeset
|
184 %!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
|
185 %!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
|
186 %!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
|
187 %!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
|
188 |
399
692cecebc71f
Error on attempt to convert a char array to Python str (fixes issue #75)
Mike Miller <mtmiller@octave.org>
parents:
371
diff
changeset
|
189 ## Test failure to convert char arrays to strings |
692cecebc71f
Error on attempt to convert a char array to Python str (fixes issue #75)
Mike Miller <mtmiller@octave.org>
parents:
371
diff
changeset
|
190 %!error (pycall ("str", ("hello")')) |
692cecebc71f
Error on attempt to convert a char array to Python str (fixes issue #75)
Mike Miller <mtmiller@octave.org>
parents:
371
diff
changeset
|
191 %!error (pycall ("str", ["hello"; "world"])) |
692cecebc71f
Error on attempt to convert a char array to Python str (fixes issue #75)
Mike Miller <mtmiller@octave.org>
parents:
371
diff
changeset
|
192 |
334
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
193 ## Test construction of dict from pyargs |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
194 %!test |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
195 %! a = pycall ("dict", pyargs ("a", 1, "b", 2, "c", 3)); |
365
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
196 %! assert (sort (cellfun (@char, cell (pycall ("list", a.keys ())), "uniformoutput", false)), {"a", "b", "c"}) |
334
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
197 %! assert (sort (double (pycall ("array.array", "d", a.values ()))), [1, 2, 3]) |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
198 |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
199 ## Test copy construction of dict from dict |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
200 %!test |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
201 %! a = pycall ("dict", pyargs ("a", 1, "b", 2, "c", 3)); |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
202 %! b = pycall ("dict", a); |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
203 %! assert (isequal (a, b)) |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
204 |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
205 ## Test construction of dict from sequence of key value pairs |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
206 %!test |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
207 %! a = pycall ("dict", pyargs ("a", 1, "b", 2, "c", 3)); |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
208 %! b = pycall ("dict", pycall ("zip", {"a", "b", "c"}, {1, 2, 3})); |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
209 %! assert (isequal (a, b)) |
252b458bd904
Add %!tests of dict creation and equality
Mike Miller <mtmiller@octave.org>
parents:
333
diff
changeset
|
210 |
198
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
211 ## 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
|
212 %!test |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
213 %! pyexec ("def roundtrip(x): return x"); |
365
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
214 %! values = { 0, pi, 2j, eps, false, true }; |
198
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
215 %! 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
|
216 %! 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
|
217 %! endfor |
377f2dc057ea
pycall: add more tests for preservation of values to and from Python
Mike Miller <mtmiller@octave.org>
parents:
197
diff
changeset
|
218 |
304
a133564f4af7
Convert integer types to appropriate Python int or long types (fixes issue #55)
Mike Miller <mtmiller@octave.org>
parents:
303
diff
changeset
|
219 ## 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
|
220 %!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
|
221 %!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
|
222 %!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
|
223 %!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
|
224 %!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
|
225 %!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
|
226 %!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
|
227 %!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
|
228 %!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
|
229 %!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
|
230 %!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
|
231 %!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
|
232 %!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
|
233 %!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
|
234 %!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
|
235 %!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
|
236 %!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
|
237 %!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
|
238 %!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
|
239 %!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
|
240 |
195
459b6f5c56ed
maint: prefer term "argument" over "parameter" for values passed into a function
Mike Miller <mtmiller@octave.org>
parents:
194
diff
changeset
|
241 %!error <argument type conversion> |
263
0fc1a58d7e89
Delete unnecessary newlines in one-line function definitions
Mike Miller <mtmiller@octave.org>
parents:
248
diff
changeset
|
242 %! 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
|
243 %! 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
|
244 |
328
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
245 ## Test conversion of integer types from Python |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
246 %!test |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
247 %! if (pyeval ("__import__('sys').hexversion >= 0x03000000")) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
248 %! assert (isa (pycall ("int", 0), "pyobject")) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
249 %! assert (isa (pycall ("int", 2^31-1), "pyobject")) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
250 %! assert (isa (pycall ("int", -2^31), "pyobject")) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
251 %! assert (double (pycall ("int", 0)), 0) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
252 %! assert (double (pycall ("int", 2^31-1)), 2^31-1) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
253 %! assert (double (pycall ("int", -2^31)), -2^31) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
254 %! else |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
255 %! assert (pycall ("int", 0), int64 (0)) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
256 %! assert (pycall ("int", 2^31-1), int64 (2^31-1)) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
257 %! assert (pycall ("int", -2^31), int64 (-2^31)) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
258 %! assert (isa (pycall ("long", 0), "pyobject")) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
259 %! assert (isa (pycall ("long", 2^31-1), "pyobject")) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
260 %! assert (isa (pycall ("long", -2^31), "pyobject")) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
261 %! assert (double (pycall ("long", 0)), 0) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
262 %! assert (double (pycall ("long", 2^31-1)), 2^31-1) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
263 %! assert (double (pycall ("long", -2^31)), -2^31) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
264 %! endif |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
265 %!assert (isa (pycall ("int", 2^100), "pyobject")) |
5782d7932529
Automatically convert Python 2 'int' type to Octave int64 (fixes issue #56)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
266 |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
267 %!test |
184
8b97647e48f1
maint: whitespace fixes for cset 2b03585d4ddd
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
268 %! 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
|
269 %! " 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
|
270 %! " 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
|
271 %! " else:\n return 10"]); |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
309
diff
changeset
|
272 %! assert (double (pycall ("pyfunc", true)), 30) |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
309
diff
changeset
|
273 %! assert (double (pycall ("pyfunc", false)), 20) |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
309
diff
changeset
|
274 %! assert (double (pycall ("pyfunc", 10)), 10) |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
275 |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
198
diff
changeset
|
276 %!error <NameError> |
263
0fc1a58d7e89
Delete unnecessary newlines in one-line function definitions
Mike Miller <mtmiller@octave.org>
parents:
248
diff
changeset
|
277 %! 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
|
278 %! pycall ("raiseException") |
239
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
279 |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
280 ## None as a return value |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
281 %!test |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
282 %! f = pyeval ("lambda: None"); |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
283 %! r = pycall (f); |
368
4d54fb68de71
__py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents:
365
diff
changeset
|
284 %! assert (__py_is_none__ (r)) |
239
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
285 |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
286 ## But returning None will not set "ans" |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
287 %!test |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
288 %! f = pyeval ("lambda: None"); |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
289 %! clear ans |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
290 %! pycall (f); |
0401489ea58c
Allow pycall to return None
Colin Macdonald <cbm@m.fsf.org>
parents:
235
diff
changeset
|
291 %! assert (! exist ("ans", "var")) |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
178
diff
changeset
|
292 */ |