annotate pyeval.cc @ 327:15c20ab4b80a

Do not automatically convert Python integer types to Octave (see issue #56) * python_to_octave.cc (pytave::pyobj_to_octvalue): Drop automatic conversion of Python int and long types to Octave. * @pyobject/pyobject.m (pyobject.length, pyobject.size): Cast values to double. Adapt %!tests to changes. * @pyobject/cell.m, @pyobject/dummy.m, @pyobject/subsasgn.m, @pyobject/subsref.m, pycall.cc, pyeval.cc: Adapt usage examples and %!tests to changes.
author Mike Miller <mtmiller@octave.org>
date Sat, 13 Aug 2016 21:24:29 -0700
parents a140160b2319
children cee203ea6245
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
1 /*
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
2
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
3 Copyright (C) 2016 Colin B. Macdonald
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
4
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
5 This file is part of Pytave.
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
6
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
7 Pytave is free software; you can redistribute it and/or modify it
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
10 option) any later version.
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
11
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
12 Pytave is distributed in the hope that it will be useful, but WITHOUT
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
15 for more details.
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
16
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
18 along with Pytave; see the file COPYING. If not, see
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
20
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
21 */
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
22
162
df7b4a5ab542 Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents: 161
diff changeset
23 #if defined (HAVE_CONFIG_H)
df7b4a5ab542 Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents: 161
diff changeset
24 # include <config.h>
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
25 #endif
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
26
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
27 #include <dlfcn.h>
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
28 #include <boost/python.hpp>
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
29 #include <boost/python/numeric.hpp>
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
30
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
31 #include <oct.h>
204
61df785bd8b0 pyeval creates pyobj direction
Colin Macdonald <cbm@m.fsf.org>
parents: 202
diff changeset
32 #include <octave/parse.h>
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
33
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
34 #define PYTAVE_DO_DECLARE_SYMBOL
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
35 #include "arrayobjectdefs.h"
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
36 #include "exceptions.h"
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
37 #include "python_to_octave.h"
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
38 #include "pytave_utils.h"
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
39
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
40 using namespace boost::python;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
41
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
42 DEFUN_DLD (pyeval, args, nargout,
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
43 "-*- texinfo -*-\n\
173
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
44 @deftypefn {} {} pyeval (@var{expr})\n\
316
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
45 @deftypefnx {} {} pyeval (@var{expr}, @var{localns})\n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
46 @deftypefnx {} {@var{x} =} pyeval (@dots{})\n\
173
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
47 Evaluate a Python expression and return the result.\n\
316
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
48 \n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
49 When called with an optional second argument, @var{localns} is a\n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
50 @code{py.dict} that acts as the namespace for any assignments or other\n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
51 side effects of the expression.\n\
173
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
52 \n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
53 Examples:\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
54 @example\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
55 @group\n\
176
72fa08eb9681 Fix pyeval doctest
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
56 pyexec (\"import sys\")\n\
173
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
57 pyeval (\"sys.version\")\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
58 @result{} ...\n\
316
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
59 pyeval (\"dict(two=2)\")\n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
60 @result{} [pyobject ...]\n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
61 @{'two': 2@}\n\
173
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
62 @end group\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
63 @end example\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
64 @seealso{pycall, pyexec}\n\
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
65 @end deftypefn")
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
66 {
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
67 octave_value_list retval;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
68
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
69 int nargin = args.length ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
70
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
71 if (nargin < 1 || nargin > 2)
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
72 {
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
73 print_usage ();
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
74 return retval;
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
75 }
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
76
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
77 std::string code = args(0).string_value ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
78
202
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
79 std::string id;
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
80 object res;
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
81
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
82 Py_Initialize ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
83
202
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
84 object main_module = import ("__main__");
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
85 object main_namespace = main_module.attr ("__dict__");
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
86 object local_namespace;
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
87 if (nargin > 1)
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
88 {
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
89 pytave::get_object_from_python (args(1), local_namespace);
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
90 if (local_namespace.is_none ())
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
91 error ("pyeval: NAMESPACE must be a string or a Python reference");
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
92 }
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
93 else
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
94 local_namespace = main_namespace;
202
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
95
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
96 try
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
97 {
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
98 res = eval (code.c_str (), main_namespace, local_namespace);
162
df7b4a5ab542 Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents: 161
diff changeset
99
247
c3addc52dfcd pyeval: allow evaluated value of None (fixes issue #31)
Mike Miller <mtmiller@octave.org>
parents: 235
diff changeset
100 if (nargout > 0 || ! res.is_none ())
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
101 {
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
102 octave_value val;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
103 pytave::pyobj_to_octvalue (val, res);
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
104 retval(0) = val;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
105 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
106 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
107 catch (pytave::object_convert_exception const &)
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
108 {
278
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
109 error ("pyexec: error in return value type conversion");
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
110 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
111 catch (error_already_set const &)
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
112 {
231
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 190
diff changeset
113 std::string message = pytave::fetch_exception_message ();
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 190
diff changeset
114 error ("pyeval: %s", message.c_str ());
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
115 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
116
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
117 return retval;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
118 }
180
5f0d94afebe2 Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 176
diff changeset
119
5f0d94afebe2 Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 176
diff changeset
120 /*
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
121 %!assert (isnumeric (double (pyeval ("0"))))
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
122 %!assert (isreal (double (pyeval ("0"))))
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
123 %!assert (double (pyeval ("0")), 0)
180
5f0d94afebe2 Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 176
diff changeset
124
190
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
125 %!assert (isnumeric (pyeval ("10.1")))
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
126 %!assert (isreal (pyeval ("10.1")))
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
127 %!assert (pyeval ("10.1"), 10.1)
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
128
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
129 %!assert (isnumeric (pyeval ("2j")))
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
130 %!assert (iscomplex (pyeval ("2j")))
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
131 %!assert (pyeval ("2j"), 2j)
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
132
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
133 %!assert (ischar (pyeval ("\"I <3 Octave\"")))
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
134 %!assert (pyeval ("\"I <3 Octave\""), "I <3 Octave")
180
5f0d94afebe2 Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 176
diff changeset
135
183
2b03585d4ddd Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 182
diff changeset
136 %!assert (islogical (pyeval ("True")))
2b03585d4ddd Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 182
diff changeset
137 %!assert (islogical (pyeval ("False")))
190
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
138 %!assert (pyeval ("True"), true)
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
139 %!assert (pyeval ("False"), false)
183
2b03585d4ddd Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 182
diff changeset
140 %!assert (class (pyeval ("True")), "logical")
2b03585d4ddd Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 182
diff changeset
141 %!assert (class (pyeval ("False")), "logical")
2b03585d4ddd Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 182
diff changeset
142
235
bbf0b7a4e117 Return pyobject from pycall too, similar to pyeval (fixes issue #28)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 232
diff changeset
143 %!assert (isa (pyeval ("object()"), "pyobject"))
bbf0b7a4e117 Return pyobject from pycall too, similar to pyeval (fixes issue #28)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 232
diff changeset
144
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
145 %!assert (isnumeric (double (pyeval ("__import__('sys').maxsize"))))
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
146 %!assert (double (pyeval ("99999999999999")), 99999999999999)
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
147 %!assert (double (pyeval ("-99999999999999")), -99999999999999)
271
0a4f620da178 Convert numeric value to long instead of int to avoid overflow (fixes issue #40)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
148
278
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
149 %!test
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
150 %! z = pyeval ("{'x': 1., 'y': 2.}");
278
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
151 %! assert (isa (z, "pyobject"))
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
152 %! assert (z{"x"}, 1)
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
153
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
154 %!test
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
155 %! z = pyeval ("[1., 2., 3.]");
278
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
156 %! assert (isa (z, "pyobject"))
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
157 %! assert ({z{1}, z{2}, z{3}}, {1, 2, 3})
190
ac377ace2ee4 More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents: 183
diff changeset
158
180
5f0d94afebe2 Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 176
diff changeset
159 %!test
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
160 %! z = pyeval ("(4., 5., 6.)");
278
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
161 %! assert (isa (z, "pyobject"))
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
162 %! assert ({z{1}, z{2}, z{3}}, {4, 5, 6})
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
163
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
164 %!test
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
165 %! z = pyeval ("[1., [21., 22.], 3., [41., [421., 422.], 43.]]");
278
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
166 %! assert (isa (z, "pyobject"))
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
167 %! assert (isa (z{2}, "pyobject"))
182
0bf4b7cf16ee maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents: 180
diff changeset
168 %! assert (z{2}{1}, 21)
0bf4b7cf16ee maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents: 180
diff changeset
169 %! assert (z{2}{2}, 22)
278
9278a272b1c8 Drop conversion of lists, dicts and tuples (fixes issues #27, #26)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 247
diff changeset
170 %! assert (isa (z{4}{2}, "pyobject"))
182
0bf4b7cf16ee maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents: 180
diff changeset
171 %! assert (z{4}{2}{1}, 421)
0bf4b7cf16ee maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents: 180
diff changeset
172 %! assert (z{4}{2}{2}, 422)
231
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 190
diff changeset
173
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 190
diff changeset
174 %!error <NameError>
263
0fc1a58d7e89 Delete unnecessary newlines in one-line function definitions
Mike Miller <mtmiller@octave.org>
parents: 247
diff changeset
175 %! pyexec ("def raiseException(): raise NameError ('oops')")
0fc1a58d7e89 Delete unnecessary newlines in one-line function definitions
Mike Miller <mtmiller@octave.org>
parents: 247
diff changeset
176 %! pyeval ("raiseException()")
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
177
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
178 %!test
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
179 %! % Variable defined in global namespace is available locally
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
180 %! myNS = pyeval ("{}");
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
181 %! pyexec ("myvar = 1.")
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
182 %! assert (pyeval ("myvar", myNS), 1);
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
183
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
184 %!test
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
185 %! % Variables with same name can have different values in different namespaces
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
186 %! myNS1 = pyeval ("{}");
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
187 %! myNS2 = pyeval ("{}");
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
188 %! pyexec ("myvar = 1.")
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
189 %! pyexec ("myvar = 2.", myNS1)
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
190 %! pyexec ("myvar = 3.", myNS2)
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
191 %! assert (pyeval ("myvar"), 1)
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
192 %! assert (pyeval ("myvar", myNS1), 2)
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
193 %! assert (pyeval ("myvar", myNS2), 3)
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
194
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
195 %!test
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
196 %! pyexec ("if 'myvar' in globals(): del myvar")
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
197 %! % Namespaces can also be passed as strings
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
198 %! pyexec ("myNS = {}");
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
199 %! pyexec ("myvar = 1.", "myNS");
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
200 %! assert (pyeval ("myvar", "myNS"), 1);
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
201
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
202 %!error <NameError>
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
203 %! pyexec ("if 'myvar' in globals(): del myvar")
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
204 %! % Variable defined in local namespace MUST not be available globally
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
205 %! myNS = pyeval ("{}");
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
206 %! pyexec ("myvar = 1.", myNS)
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
207 %! pyeval ("myvar");
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
208
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
209 %!error <NameError>
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
210 %! pyexec ("if 'myvar' in globals(): del myvar")
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
211 %! % Variable defined in one local namespace MUST not be available in another
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
212 %! myNS1 = pyeval ("{}");
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
213 %! myNS2 = pyeval ("{}");
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
214 %! pyexec ("myvar = 1.", myNS1)
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
215 %! pyeval ("myvar", myNS2);
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
216
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
217 %!error <NameError>
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
218 %! pyexec ("if 'sys' in globals(): del sys")
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
219 %! % Modules imported in local namespace MUST not be accessible globally
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
220 %! myNS = pyeval ("{}");
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
221 %! pyexec ("import sys", myNS);
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 279
diff changeset
222 %! pyeval ("sys");
180
5f0d94afebe2 Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 176
diff changeset
223 */