Skip to content

Commit df60770

Browse files
bakpaulhugtalbotfredroy
committed
Use built in method numpy.set_printoptions in addObject/addChild methods to enable numpy 2 (#518)
* Use set_printoptions(legacy=True) in node binfing to force usage of legacy repr for numpy objects. This is ocmpatible with numpy 1 * Patch addChild * Add RAII mechanism to make sure numpy is put back its initial state * Add [[maybe_unused]] Co-authored-by: Frederick Roy <fredroy@users.noreply.github.com> --------- Co-authored-by: Hugo <hugo.talbot@sofa-framework.org> Co-authored-by: Frederick Roy <fredroy@users.noreply.github.com>
1 parent 441731d commit df60770

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,47 @@ void setFieldsFromPythonValues(Base* self, const py::kwargs& dict)
234234
}
235235
}
236236

237+
class NumpyReprFixerRAII
238+
{
239+
public:
240+
NumpyReprFixerRAII()
241+
{
242+
using namespace pybind11::literals;
243+
244+
m_numpy = py::module_::import("numpy");
245+
const std::string version = py::cast<std::string>(m_numpy.attr("__version__"));
246+
m_majorVersion = std::stoi(version.substr(0,1));
247+
if ( m_majorVersion > 1)
248+
{
249+
m_setPO = m_numpy.attr("set_printoptions");
250+
m_initialState = m_numpy.attr("get_printoptions")();
251+
m_setPO("legacy"_a = true);
252+
}
253+
}
254+
255+
~NumpyReprFixerRAII()
256+
{
257+
if ( m_majorVersion > 1)
258+
{
259+
m_setPO(**m_initialState);
260+
}
261+
}
262+
263+
private:
264+
py::module_ m_numpy;
265+
int m_majorVersion;
266+
py::object m_setPO;
267+
py::dict m_initialState;
268+
269+
};
270+
271+
237272
/// Implement the addObject function.
238273
py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs& kwargs)
239274
{
275+
//Instantiating this object will make sure the numpy representation is fixed during the call of this function, and comes back to its previous state after
276+
[[maybe_unused]] const NumpyReprFixerRAII numpyReprFixer;
277+
240278
std::string name {};
241279
if (kwargs.contains("name"))
242280
{
@@ -291,6 +329,8 @@ py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs
291329
if(d)
292330
d->setPersistent(true);
293331
}
332+
333+
294334
return PythonFactory::toPython(object.get());
295335
}
296336

@@ -360,6 +400,9 @@ py::object createObject(Node* self, const std::string& type, const py::kwargs& k
360400

361401
py::object addChildKwargs(Node* self, const std::string& name, const py::kwargs& kwargs)
362402
{
403+
//Instantiating this object will make sure the numpy representation is fixed during the call of this function, and comes back to its previous state after
404+
[[maybe_unused]] const NumpyReprFixerRAII numpyReprFixer;
405+
363406
if (sofapython3::isProtectedKeyword(name))
364407
throw py::value_error("addChild: Cannot call addChild with name " + name + ": Protected keyword");
365408
BaseObjectDescription desc (name.c_str());
@@ -378,6 +421,7 @@ py::object addChildKwargs(Node* self, const std::string& name, const py::kwargs&
378421
d->setPersistent(true);
379422
}
380423

424+
381425
return py::cast(node);
382426
}
383427

0 commit comments

Comments
 (0)