Mercurial > pytave
changeset 183:2b03585d4ddd
Add proper conversion of booleans between octave and python. (fixes issue #6)
* octave_to_python.cc (octvalue_to_pyobj): Allow conversion if the octvalue is
boolean.
* python_to_octave.cc (pyobj_to_octvalue): Check and convert booleans from
python to octave.
* pycall.cc, pyeval.cc: Add the respective tests for boolean conversion.
author | Abhinav Tripathi <genuinelucifer@gmail.com> |
---|---|
date | Sat, 04 Jun 2016 13:30:01 -0700 |
parents | 0bf4b7cf16ee |
children | 8b97647e48f1 |
files | octave_to_python.cc pycall.cc pyeval.cc python_to_octave.cc |
diffstat | 4 files changed, 20 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/octave_to_python.cc Sat Jun 04 12:56:57 2016 -0700 +++ b/octave_to_python.cc Sat Jun 04 13:30:01 2016 -0700 @@ -217,7 +217,7 @@ throw value_convert_exception ( "Octave value `undefined'. Can not convert to a Python object"); else if (octvalue.is_numeric_type () || octvalue.is_string () - || octvalue.is_cell ()) + || octvalue.is_cell () || octvalue.is_bool_type ()) octvalue_to_pyarr (py_object, octvalue); else if (octvalue.is_map ()) octmap_to_pyobject (py_object, octvalue.map_value ());
--- a/pycall.cc Sat Jun 04 12:56:57 2016 -0700 +++ b/pycall.cc Sat Jun 04 13:30:01 2016 -0700 @@ -186,4 +186,12 @@ %! pyexec ("def pyfunc(x):\n return 2*x"); %! z = pycall ("pyfunc", [20 20]); %! assert (z, [40 40]) + +%!test +%! pyexec (["def pyfunc(x):\n if x.item((0,0)) is True:\n return 30", ... +%! "\n elif x.item((0,0)) is False:\n return 20\n else:", ... +%! "\n return 10"]); +%! assert (pycall ("pyfunc", true), 30) +%! assert (pycall ("pyfunc", false), 20) +%! assert (pycall ("pyfunc", 10), 10) */
--- a/pyeval.cc Sat Jun 04 12:56:57 2016 -0700 +++ b/pyeval.cc Sat Jun 04 13:30:01 2016 -0700 @@ -116,6 +116,13 @@ %! assert (ischar (q)) %! assert (! strcmp (q, "1 <3 Octave")) +%!assert (islogical (pyeval ("True"))) +%!assert (islogical (pyeval ("False"))) +%!assert (pyeval ("True")) +%!assert (! pyeval ("False")) +%!assert (class (pyeval ("True")), "logical") +%!assert (class (pyeval ("False")), "logical") + %!test %! % FIXME: this will change when we stop converting lists %! z = pyeval ("[1, [21, 22], 3, [41, [421, 422], 43]]");
--- a/python_to_octave.cc Sat Jun 04 12:56:57 2016 -0700 +++ b/python_to_octave.cc Sat Jun 04 13:30:01 2016 -0700 @@ -478,6 +478,7 @@ const boost::python::object& py_object) { extract<int> intx (py_object); + extract<bool> boolx (py_object); extract<double> doublex (py_object); extract<Complex> complexx (py_object); extract<std::string> stringx (py_object); @@ -487,7 +488,9 @@ extract<boost::python::dict> dictx (py_object); extract<boost::python::tuple> tuplex (py_object); - if (intx.check ()) + if (boolx.check() && PyBool_Check ((PyArrayObject*)py_object.ptr ())) + oct_value = boolx (); + else if (intx.check ()) oct_value = intx (); else if (doublex.check ()) oct_value = doublex ();