Skip to content

Commit 6e0979a

Browse files
committed
core: fix setitem function for user types
We should authorize the cast from long objects
1 parent 20ec426 commit 6e0979a

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

include/eigenpy/user-type.hpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,34 +132,46 @@ struct SpecialMethods<T, NPY_USERDEF> {
132132
eigenpy::Exception("Cannot retrieve the type stored in the array.");
133133
return -1;
134134
}
135+
135136
PyArrayObject* py_array = static_cast<PyArrayObject*>(array);
136137
PyArray_Descr* descr = PyArray_DTYPE(py_array);
137138
PyTypeObject* array_scalar_type = descr->typeobj;
138139
PyTypeObject* src_obj_type = Py_TYPE(src_obj);
139140

141+
T& dest = *static_cast<T*>(dest_ptr);
140142
if (array_scalar_type != src_obj_type) {
141-
std::stringstream ss;
142-
ss << "The input type is of wrong type. ";
143-
ss << "The expected type is " << bp::type_info(typeid(T)).name()
144-
<< std::endl;
145-
eigenpy::Exception(ss.str());
146-
return -1;
147-
}
143+
long long src_value = PyLong_AsLongLong(src_obj);
144+
if (src_value == -1 && PyErr_Occurred()) {
145+
std::stringstream ss;
146+
ss << "The input type is of wrong type. ";
147+
ss << "The expected type is " << bp::type_info(typeid(T)).name()
148+
<< std::endl;
149+
eigenpy::Exception(ss.str());
150+
return -1;
151+
}
152+
153+
dest = T(src_value);
148154

149-
bp::extract<T&> extract_src_obj(src_obj);
150-
if (!extract_src_obj.check()) {
151-
std::stringstream ss;
152-
ss << "The input type is of wrong type. ";
153-
ss << "The expected type is " << bp::type_info(typeid(T)).name()
154-
<< std::endl;
155-
eigenpy::Exception(ss.str());
156-
return -1;
155+
} else {
156+
bp::extract<T&> extract_src_obj(src_obj);
157+
if (!extract_src_obj.check()) {
158+
std::cout << "if (!extract_src_obj.check())" << std::endl;
159+
std::stringstream ss;
160+
ss << "The input type is of wrong type. ";
161+
ss << "The expected type is " << bp::type_info(typeid(T)).name()
162+
<< std::endl;
163+
eigenpy::Exception(ss.str());
164+
return -1;
165+
}
166+
167+
const T& src = extract_src_obj();
168+
const T& src = extract_src_obj();
169+
T& dest = *static_cast<T*>(dest_ptr);
170+
const T& src = extract_src_obj();
171+
T& dest = *static_cast<T*>(dest_ptr);
172+
dest = src;
157173
}
158174

159-
const T& src = extract_src_obj();
160-
T& dest = *static_cast<T*>(dest_ptr);
161-
dest = src;
162-
163175
return 0;
164176
}
165177

0 commit comments

Comments
 (0)