Mercurial > pytave
comparison pycall.cc @ 365:087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
* python_to_octave.cc (pytave::pyobj_to_octvalue): Drop conversion of bytes and
unicode objects.
* __py_struct_from_dict__.cc (F__py_string_value__): New function.
* @pyobject/char.m: Define outside class definition, use __py_string_value__.
* @pyobject/pyobject.m: Delete previous pyobject.char definition.
* @pyobject/methods.m: Apply char conversion to __name__ attribute.
* @py/py.m, @pyobject/cell.m, @pyobject/dummy.m, @pyobject/subsasgn.m,
@pyobject/subsref.m, pyargs.m, pycall.cc, pyeval.cc: Adapt examples and tests
to changes.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 25 Aug 2016 14:06:56 -0700 |
parents | 1470ed26917a |
children | 4d54fb68de71 |
comparison
equal
deleted
inserted
replaced
364:1470ed26917a | 365:087e7bc3697f |
---|---|
145 | 145 |
146 return retval; | 146 return retval; |
147 } | 147 } |
148 | 148 |
149 /* | 149 /* |
150 %!assert (ischar (pycall ("os.getcwd"))) | |
151 %!assert (isreal (pycall ("random.random"))) | 150 %!assert (isreal (pycall ("random.random"))) |
152 %!assert (double (pycall ("math.exp", 3)), exp (3)) | 151 %!assert (double (pycall ("math.exp", 3)), exp (3)) |
153 %!assert (double (pycall ("math.trunc", pi)), fix (pi)) | 152 %!assert (double (pycall ("math.trunc", pi)), fix (pi)) |
154 %!assert (double (pycall ("math.sqrt", 2)), sqrt (2)) | 153 %!assert (double (pycall ("math.sqrt", 2)), sqrt (2)) |
155 %!assert (double (pycall ("cmath.sqrt", 2j)), sqrt (2j)) | 154 %!assert (double (pycall ("cmath.sqrt", 2j)), sqrt (2j)) |
156 %!assert (double (pycall ("int", 10.2)), 10) | 155 %!assert (double (pycall ("int", 10.2)), 10) |
156 %!assert (isa (pycall ("os.getcwd"), "pyobject")) | |
157 %!assert (isa (pycall ("object"), "pyobject")) | 157 %!assert (isa (pycall ("object"), "pyobject")) |
158 %!assert (isa (pycall ("dict"), "pyobject")) | 158 %!assert (isa (pycall ("dict"), "pyobject")) |
159 %!assert (isa (pycall ("list"), "pyobject")) | 159 %!assert (isa (pycall ("list"), "pyobject")) |
160 %!assert (isa (pycall ("tuple"), "pyobject")) | 160 %!assert (isa (pycall ("tuple"), "pyobject")) |
161 | 161 |
164 %! pyexec (["def typename(x):\n" ... | 164 %! pyexec (["def typename(x):\n" ... |
165 %! " s = type(x).__name__\n" ... | 165 %! " s = type(x).__name__\n" ... |
166 %! " if s == 'long':\n" ... | 166 %! " if s == 'long':\n" ... |
167 %! " return 'int'\n" ... | 167 %! " return 'int'\n" ... |
168 %! " return s"]); | 168 %! " return s"]); |
169 %!assert (pycall ("typename", 0), "float") | 169 %!assert (char (pycall ("typename", 0)), "float") |
170 %!assert (pycall ("typename", pi), "float") | 170 %!assert (char (pycall ("typename", pi)), "float") |
171 %!assert (pycall ("typename", 2j), "complex") | 171 %!assert (char (pycall ("typename", 2j)), "complex") |
172 %!assert (pycall ("typename", int32 (0)), "int") | 172 %!assert (char (pycall ("typename", int32 (0))), "int") |
173 %!assert (pycall ("typename", false), "bool") | 173 %!assert (char (pycall ("typename", false)), "bool") |
174 %!assert (pycall ("typename", true), "bool") | 174 %!assert (char (pycall ("typename", true)), "bool") |
175 %!assert (pycall ("typename", "Hello world"), "str") | 175 %!assert (char (pycall ("typename", "Hello world")), "str") |
176 %!assert (pycall ("typename", char ([1, 2, 3])), "str") | 176 %!assert (char (pycall ("typename", char ([1, 2, 3]))), "str") |
177 | 177 |
178 ## Test construction of sequence types from cell arrays | 178 ## Test construction of sequence types from cell arrays |
179 %!assert (char (pycall ("list")), "[]") | 179 %!assert (char (pycall ("list")), "[]") |
180 %!assert (char (pycall ("list", {})), "[]") | 180 %!assert (char (pycall ("list", {})), "[]") |
181 %!assert (char (pycall ("list", {1, 2, 3})), "[1.0, 2.0, 3.0]") | 181 %!assert (char (pycall ("list", {1, 2, 3})), "[1.0, 2.0, 3.0]") |
188 %!error (pycall ("dict", {1, 2, 3})) | 188 %!error (pycall ("dict", {1, 2, 3})) |
189 | 189 |
190 ## Test construction of dict from pyargs | 190 ## Test construction of dict from pyargs |
191 %!test | 191 %!test |
192 %! a = pycall ("dict", pyargs ("a", 1, "b", 2, "c", 3)); | 192 %! a = pycall ("dict", pyargs ("a", 1, "b", 2, "c", 3)); |
193 %! assert (sort (cell (pycall ("list", a.keys ()))), {"a", "b", "c"}) | 193 %! assert (sort (cellfun (@char, cell (pycall ("list", a.keys ())), "uniformoutput", false)), {"a", "b", "c"}) |
194 %! assert (sort (double (pycall ("array.array", "d", a.values ()))), [1, 2, 3]) | 194 %! assert (sort (double (pycall ("array.array", "d", a.values ()))), [1, 2, 3]) |
195 | 195 |
196 ## Test copy construction of dict from dict | 196 ## Test copy construction of dict from dict |
197 %!test | 197 %!test |
198 %! a = pycall ("dict", pyargs ("a", 1, "b", 2, "c", 3)); | 198 %! a = pycall ("dict", pyargs ("a", 1, "b", 2, "c", 3)); |
206 %! assert (isequal (a, b)) | 206 %! assert (isequal (a, b)) |
207 | 207 |
208 ## Test round trip type preservation / conversion | 208 ## Test round trip type preservation / conversion |
209 %!test | 209 %!test |
210 %! pyexec ("def roundtrip(x): return x"); | 210 %! pyexec ("def roundtrip(x): return x"); |
211 %! values = { 0, pi, 2j, eps, false, true, version, "Hello world" }; | 211 %! values = { 0, pi, 2j, eps, false, true }; |
212 %! for i = 1:numel (values) | 212 %! for i = 1:numel (values) |
213 %! assert (pycall ("roundtrip", values{i}), values{i}); | 213 %! assert (pycall ("roundtrip", values{i}), values{i}); |
214 %! endfor | 214 %! endfor |
215 | 215 |
216 ## Test conversion of integer types into Python | 216 ## Test conversion of integer types into Python |