Skip to content

Commit e0a8a3e

Browse files
Camille-KCamille Krewcunhugtalbot
authored
[DataHelper] Change default access of vector data buffer (#318)
* [DataHelper] Change behaviour of toBufferInfo for empty vectors Until now, no difference was made in the toBufferInfo method depending of the size of the handled data, in particular if this data was an empty vector (with size = 0). Accessing such a vector through Python code would called a trigger to the toBufferInfo method, resulting in a call to the getValuePtr method and an access to the first element (data[0]), which in this case doesn't exist. We change this default behaviour by calling the getValuePtr method only if the vector is not empty, and by returning a null pointer otherwise. * Update Plugin/src/SofaPython3/DataHelper.cpp Co-authored-by: Camille Krewcun <camille.krewcun@inria.fr> Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
1 parent 2d62f4e commit e0a8a3e

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

Plugin/src/SofaPython3/DataHelper.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,10 @@ py::buffer_info toBufferInfo(BaseData& m)
334334

335335
std::tuple<int,int> shape = getShape(&m);
336336

337-
void* ptr = const_cast<void*>(nfo.getValuePtr(m.getValueVoidPtr()));
337+
// By default, ptr is set to nullptr. It is updated by a call to getValuePtr only if the data is not empty. Otherwise the ptr passed to the py::buffer_info remains nullptr.
338+
void* ptr = nullptr;
339+
if (std::get<0>(shape) != 0)
340+
ptr = const_cast<void*>(nfo.getValuePtr(m.getValueVoidPtr()));
338341

339342
if( !itemNfo->Container() ){
340343
return py::buffer_info(

0 commit comments

Comments
 (0)