Mercurial > pytave
comparison 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 |
comparison
equal
deleted
inserted
replaced
326:37df9bd607ed | 327:15c20ab4b80a |
---|---|
116 | 116 |
117 return retval; | 117 return retval; |
118 } | 118 } |
119 | 119 |
120 /* | 120 /* |
121 %!assert (isnumeric (pyeval ("0"))) | 121 %!assert (isnumeric (double (pyeval ("0")))) |
122 %!assert (isreal (pyeval ("0"))) | 122 %!assert (isreal (double (pyeval ("0")))) |
123 %!assert (pyeval ("0"), 0) | 123 %!assert (double (pyeval ("0")), 0) |
124 | 124 |
125 %!assert (isnumeric (pyeval ("10.1"))) | 125 %!assert (isnumeric (pyeval ("10.1"))) |
126 %!assert (isreal (pyeval ("10.1"))) | 126 %!assert (isreal (pyeval ("10.1"))) |
127 %!assert (pyeval ("10.1"), 10.1) | 127 %!assert (pyeval ("10.1"), 10.1) |
128 | 128 |
140 %!assert (class (pyeval ("True")), "logical") | 140 %!assert (class (pyeval ("True")), "logical") |
141 %!assert (class (pyeval ("False")), "logical") | 141 %!assert (class (pyeval ("False")), "logical") |
142 | 142 |
143 %!assert (isa (pyeval ("object()"), "pyobject")) | 143 %!assert (isa (pyeval ("object()"), "pyobject")) |
144 | 144 |
145 %!assert (isnumeric (pyeval ("__import__('sys').maxsize"))) | 145 %!assert (isnumeric (double (pyeval ("__import__('sys').maxsize")))) |
146 %!assert (pyeval ("99999999999999"), 99999999999999) | 146 %!assert (double (pyeval ("99999999999999")), 99999999999999) |
147 %!assert (pyeval ("-99999999999999"), -99999999999999) | 147 %!assert (double (pyeval ("-99999999999999")), -99999999999999) |
148 | 148 |
149 %!test | 149 %!test |
150 %! z = pyeval ("{'x': 1, 'y': 2}"); | 150 %! z = pyeval ("{'x': 1., 'y': 2.}"); |
151 %! assert (isa (z, "pyobject")) | 151 %! assert (isa (z, "pyobject")) |
152 %! assert (z{"x"}, 1) | 152 %! assert (z{"x"}, 1) |
153 | 153 |
154 %!test | 154 %!test |
155 %! z = pyeval ("[1, 2, 3]"); | 155 %! z = pyeval ("[1., 2., 3.]"); |
156 %! assert (isa (z, "pyobject")) | 156 %! assert (isa (z, "pyobject")) |
157 %! assert ({z{1}, z{2}, z{3}}, {1, 2, 3}) | 157 %! assert ({z{1}, z{2}, z{3}}, {1, 2, 3}) |
158 | 158 |
159 %!test | 159 %!test |
160 %! z = pyeval ("(4, 5, 6)"); | 160 %! z = pyeval ("(4., 5., 6.)"); |
161 %! assert (isa (z, "pyobject")) | 161 %! assert (isa (z, "pyobject")) |
162 %! assert ({z{1}, z{2}, z{3}}, {4, 5, 6}) | 162 %! assert ({z{1}, z{2}, z{3}}, {4, 5, 6}) |
163 | 163 |
164 %!test | 164 %!test |
165 %! z = pyeval ("[1, [21, 22], 3, [41, [421, 422], 43]]"); | 165 %! z = pyeval ("[1., [21., 22.], 3., [41., [421., 422.], 43.]]"); |
166 %! assert (isa (z, "pyobject")) | 166 %! assert (isa (z, "pyobject")) |
167 %! assert (isa (z{2}, "pyobject")) | 167 %! assert (isa (z{2}, "pyobject")) |
168 %! assert (z{2}{1}, 21) | 168 %! assert (z{2}{1}, 21) |
169 %! assert (z{2}{2}, 22) | 169 %! assert (z{2}{2}, 22) |
170 %! assert (isa (z{4}{2}, "pyobject")) | 170 %! assert (isa (z{4}{2}, "pyobject")) |
176 %! pyeval ("raiseException()") | 176 %! pyeval ("raiseException()") |
177 | 177 |
178 %!test | 178 %!test |
179 %! % Variable defined in global namespace is available locally | 179 %! % Variable defined in global namespace is available locally |
180 %! myNS = pyeval ("{}"); | 180 %! myNS = pyeval ("{}"); |
181 %! pyexec ("myvar = 1") | 181 %! pyexec ("myvar = 1.") |
182 %! assert (pyeval ("myvar", myNS), 1); | 182 %! assert (pyeval ("myvar", myNS), 1); |
183 | 183 |
184 %!test | 184 %!test |
185 %! % Variables with same name can have different values in different namespaces | 185 %! % Variables with same name can have different values in different namespaces |
186 %! myNS1 = pyeval ("{}"); | 186 %! myNS1 = pyeval ("{}"); |
187 %! myNS2 = pyeval ("{}"); | 187 %! myNS2 = pyeval ("{}"); |
188 %! pyexec ("myvar = 1") | 188 %! pyexec ("myvar = 1.") |
189 %! pyexec ("myvar = 2", myNS1) | 189 %! pyexec ("myvar = 2.", myNS1) |
190 %! pyexec ("myvar = 3", myNS2) | 190 %! pyexec ("myvar = 3.", myNS2) |
191 %! assert (pyeval ("myvar"), 1) | 191 %! assert (pyeval ("myvar"), 1) |
192 %! assert (pyeval ("myvar", myNS1), 2) | 192 %! assert (pyeval ("myvar", myNS1), 2) |
193 %! assert (pyeval ("myvar", myNS2), 3) | 193 %! assert (pyeval ("myvar", myNS2), 3) |
194 | 194 |
195 %!test | 195 %!test |
196 %! pyexec ("if 'myvar' in globals(): del myvar") | 196 %! pyexec ("if 'myvar' in globals(): del myvar") |
197 %! % Namespaces can also be passed as strings | 197 %! % Namespaces can also be passed as strings |
198 %! pyexec ("myNS = {}"); | 198 %! pyexec ("myNS = {}"); |
199 %! pyexec ("myvar = 1", "myNS"); | 199 %! pyexec ("myvar = 1.", "myNS"); |
200 %! assert (pyeval ("myvar", "myNS"), 1); | 200 %! assert (pyeval ("myvar", "myNS"), 1); |
201 | 201 |
202 %!error <NameError> | 202 %!error <NameError> |
203 %! pyexec ("if 'myvar' in globals(): del myvar") | 203 %! pyexec ("if 'myvar' in globals(): del myvar") |
204 %! % Variable defined in local namespace MUST not be available globally | 204 %! % Variable defined in local namespace MUST not be available globally |
205 %! myNS = pyeval ("{}"); | 205 %! myNS = pyeval ("{}"); |
206 %! pyexec ("myvar = 1", myNS) | 206 %! pyexec ("myvar = 1.", myNS) |
207 %! pyeval ("myvar"); | 207 %! pyeval ("myvar"); |
208 | 208 |
209 %!error <NameError> | 209 %!error <NameError> |
210 %! pyexec ("if 'myvar' in globals(): del myvar") | 210 %! pyexec ("if 'myvar' in globals(): del myvar") |
211 %! % Variable defined in one local namespace MUST not be available in another | 211 %! % Variable defined in one local namespace MUST not be available in another |
212 %! myNS1 = pyeval ("{}"); | 212 %! myNS1 = pyeval ("{}"); |
213 %! myNS2 = pyeval ("{}"); | 213 %! myNS2 = pyeval ("{}"); |
214 %! pyexec ("myvar = 1", myNS1) | 214 %! pyexec ("myvar = 1.", myNS1) |
215 %! pyeval ("myvar", myNS2); | 215 %! pyeval ("myvar", myNS2); |
216 | 216 |
217 %!error <NameError> | 217 %!error <NameError> |
218 %! pyexec ("if 'sys' in globals(): del sys") | 218 %! pyexec ("if 'sys' in globals(): del sys") |
219 %! % Modules imported in local namespace MUST not be accessible globally | 219 %! % Modules imported in local namespace MUST not be accessible globally |