changeset 409:b9b8790d1082

Add overload of py_isinstance taking a string decribing a type * oct-py-util.h (pytave::py_isinstance): New function overload. * __py_struct_from_dict__.cc (F__py_isinstance__): Use it. * python_to_octave.cc (pytave::pyobj_to_octvalue): Use it.
author Mike Miller <mtmiller@octave.org>
date Tue, 02 May 2017 17:44:02 -0700
parents f833e29b2c12
children 95c6ad0be828
files __py_struct_from_dict__.cc oct-py-util.h python_to_octave.cc
diffstat 3 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/__py_struct_from_dict__.cc	Tue May 02 10:06:14 2017 -0700
+++ b/__py_struct_from_dict__.cc	Tue May 02 17:44:02 2017 -0700
@@ -238,8 +238,7 @@
   pytave::py_init ();
 
   pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
-  pytave::python_object type = pytave::py_find_type (typestr);
-  retval(0) = pytave::py_isinstance (obj, type);
+  retval(0) = pytave::py_isinstance (obj, typestr);
 
   return retval;
 }
--- a/oct-py-util.h	Tue May 02 10:06:14 2017 -0700
+++ b/oct-py-util.h	Tue May 02 17:44:02 2017 -0700
@@ -83,6 +83,18 @@
   bool
   py_isinstance (PyObject *obj, PyObject *type);
 
+  //! Check whether an object is an instance of a type.
+  //!
+  //! @param obj Python object
+  //! @param typestr name of a Python type
+  //! @return @c true if @a obj is an instance of the type named by @a typestr,
+  //!         @c false otherwise
+  inline bool
+  py_isinstance (PyObject *obj, const std::string& typestr)
+  {
+    return py_isinstance (obj, py_find_type (typestr));
+  }
+
   std::string
   py_object_class_name (PyObject *obj);
 
--- a/python_to_octave.cc	Tue May 02 10:06:14 2017 -0700
+++ b/python_to_octave.cc	Tue May 02 17:44:02 2017 -0700
@@ -311,7 +311,6 @@
   void pyobj_to_octvalue (octave_value& oct_value,
                           const boost::python::object& py_object)
   {
-    boost::python::extract<boost::python::numeric::array> arrayx (py_object);
 
     if (PyBool_Check (py_object.ptr ()))
       oct_value = extract_py_bool (py_object.ptr ());
@@ -323,7 +322,7 @@
       oct_value = extract_py_float (py_object.ptr ());
     else if (PyComplex_Check (py_object.ptr ()))
       oct_value = extract_py_complex (py_object.ptr ());
-    else if (arrayx.check ())
+    else if (py_isinstance (py_object.ptr (), "numpy.ndarray"))
       pyarr_to_octvalue (oct_value, (PyArrayObject*)py_object.ptr ());
     else
       oct_value = pyobject_wrap_object (py_object.ptr ());