Mercurial > pytave
annotate pyeval.cc @ 190:ac377ace2ee4
More unit tests of pycall and pyeval
* pycall.cc, pyeval.cc: More unit tests of type translation, including
some conversions not yet working correctly.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 09 Jun 2016 14:52:06 -0700 |
parents | 2b03585d4ddd |
children | 0c6f7ae8a95b 6fffa6219b2c |
rev | line source |
---|---|
161 | 1 /* |
2 | |
3 Copyright (C) 2016 Colin B. Macdonald | |
4 | |
5 This file is part of Pytave. | |
6 | |
7 Pytave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 3 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Pytave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Pytave; see the file COPYING. If not, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
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 | 25 #endif |
26 | |
27 #include <dlfcn.h> | |
28 #include <boost/python.hpp> | |
29 #include <boost/python/numeric.hpp> | |
30 | |
31 #include <oct.h> | |
32 | |
33 #define PYTAVE_DO_DECLARE_SYMBOL | |
34 #include "arrayobjectdefs.h" | |
35 #include "exceptions.h" | |
36 #include "python_to_octave.h" | |
37 | |
38 using namespace boost::python; | |
39 | |
40 DEFUN_DLD (pyeval, args, nargout, | |
41 "-*- texinfo -*-\n\ | |
173
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
42 @deftypefn {} {} pyeval (@var{expr})\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
43 @deftypefnx {} {@var{x} =} pyeval (@var{expr})\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
44 Evaluate a Python expression and return the result.\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
45 \n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
46 Examples:\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
47 @example\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
48 @group\n\ |
176 | 49 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
|
50 pyeval (\"sys.version\")\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
51 @result{} ...\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
52 pyeval (\"dict(one=1, two=2)\")\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
53 @result{} scalar structure containing the fields:\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
54 two = 2\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
55 one = 1\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
56 @end group\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
57 @end example\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
58 @seealso{pycall, pyexec}\n\ |
161 | 59 @end deftypefn") |
60 { | |
61 octave_value_list retval; | |
62 | |
63 int nargin = args.length (); | |
64 | |
65 std::string code = args(0).string_value (); | |
66 | |
67 Py_Initialize (); | |
68 | |
69 try | |
70 { | |
71 object main_module = import ("__main__"); | |
72 object main_namespace = main_module.attr ("__dict__"); | |
73 object res = eval (code.c_str (), main_namespace, main_namespace); | |
162
df7b4a5ab542
Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents:
161
diff
changeset
|
74 |
df7b4a5ab542
Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents:
161
diff
changeset
|
75 // FIXME: currently, we cannot return the raw object to octave... |
161 | 76 if (! res.is_none ()) |
77 { | |
78 octave_value val; | |
79 pytave::pyobj_to_octvalue (val, res); | |
80 retval(0) = val; | |
81 } | |
82 } | |
83 catch (pytave::object_convert_exception const &) | |
84 { | |
85 error ("pyeval: error in return value type conversion"); | |
86 } | |
87 catch (error_already_set const &) | |
88 { | |
89 PyObject *ptype, *pvalue, *ptraceback; | |
90 PyErr_Fetch (&ptype, &pvalue, &ptraceback); | |
91 | |
92 try | |
93 { | |
94 std::string message = extract<std::string> (pvalue); | |
95 error ("pyeval: %s", message.c_str ()); | |
96 } | |
97 catch (error_already_set const &) | |
98 { | |
99 PyErr_Restore (ptype, pvalue, ptraceback); | |
100 PyErr_Print (); | |
101 } | |
102 } | |
103 | |
104 return retval; | |
105 } | |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
176
diff
changeset
|
106 |
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
176
diff
changeset
|
107 /* |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
108 %!assert (isnumeric (pyeval ("0"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
109 %!assert (isreal (pyeval ("0"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
110 %!assert (pyeval ("0"), 0) |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
176
diff
changeset
|
111 |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
112 %!assert (isnumeric (pyeval ("10.1"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
113 %!assert (isreal (pyeval ("10.1"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
114 %!assert (pyeval ("10.1"), 10.1) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
115 |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
116 %!assert (isnumeric (pyeval ("2j"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
117 %!assert (iscomplex (pyeval ("2j"))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
118 %!assert (pyeval ("2j"), 2j) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
119 |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
120 %!assert (ischar (pyeval ("\"I <3 Octave\""))) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
121 %!assert (pyeval ("\"I <3 Octave\""), "I <3 Octave") |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
176
diff
changeset
|
122 |
183
2b03585d4ddd
Add proper conversion of booleans between octave and python. (fixes issue #6)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
182
diff
changeset
|
123 %!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
|
124 %!assert (islogical (pyeval ("False"))) |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
125 %!assert (pyeval ("True"), true) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
126 %!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
|
127 %!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
|
128 %!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
|
129 |
190
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
130 ## FIXME: these will change when dict, list, and tuple are not converted |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
131 %!assert (pyeval ("{'x': 1, 'y': 2}"), struct ("x", 1, "y", 2)) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
132 %!assert (pyeval ("[1, 2, 3]"), {1, 2, 3}) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
133 %!assert (pyeval ("(4, 5, 6)"), {4, 5, 6}) |
ac377ace2ee4
More unit tests of pycall and pyeval
Mike Miller <mtmiller@octave.org>
parents:
183
diff
changeset
|
134 |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
176
diff
changeset
|
135 %!test |
182
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
136 %! % FIXME: this will change when we stop converting lists |
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
137 %! z = pyeval ("[1, [21, 22], 3, [41, [421, 422], 43]]"); |
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
138 %! assert (z{2}{1}, 21) |
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
139 %! assert (z{2}{2}, 22) |
0bf4b7cf16ee
maint: Use Octave coding style conventions for unit tests
Mike Miller <mtmiller@octave.org>
parents:
180
diff
changeset
|
140 %! 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
|
141 %! assert (z{4}{2}{2}, 422) |
180
5f0d94afebe2
Added some basic tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
176
diff
changeset
|
142 */ |