Skip to content

Commit e75c1d3

Browse files
committed
[SofaPython3] Cleaning in SpellingSuggestionHelper.h
1 parent 1433b78 commit e75c1d3

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

Plugin/src/SofaPython3/SpellingSuggestionHelper.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#pragma once
2121

2222
#include <functional>
23+
#include <algorithm>
2324
#include <iostream>
2425
#include <sofa/helper/DiffLib.h>
2526

@@ -32,27 +33,13 @@ void fillVectorOfStringFrom(const Iterable& v, const UnaryOperation& op, const P
3233
std::transform(v.begin(), v.end(), op, func);
3334
}
3435

35-
//template<class Iterable>
36-
//std::ostream& emitSpellingMessage(std::ostream& ostream, const std::string& message, const Iterable& iterable, const std::string& name, sofa::Size numEntries=5, double thresold=0.6)
37-
//{
38-
// std::vector<std::string> possibleNames;
39-
// fillVectorOfStringFrom(iterable, std::back_inserter(possibleNames), [](const typename Iterable::value_type d) { return d->getName(); });
40-
41-
// auto spellingSuggestions = sofa::helper::getClosestMatch(name, possibleNames, numEntries, thresold);
42-
// if(!spellingSuggestions.empty())
43-
// {
44-
// for(auto& [name, score] : spellingSuggestions)
45-
// ostream << message << "'" << name << "' ("<< std::to_string((int)(100*score))+"% match)" << std::endl;
46-
// }
47-
// return ostream;
48-
//}
49-
5036
template<class Iterable, class PickingFunction=std::function<const std::string(typename Iterable::value_type)> >
5137
std::ostream& emitSpellingMessage(std::ostream& ostream, const std::string& message, const Iterable& iterable, const std::string& name,
5238
sofa::Size numEntries=5, SReal thresold=0.6_sreal,
5339
PickingFunction f = [](const typename Iterable::value_type d) { return d->getName(); })
5440
{
5541
std::vector<std::string> possibleNames;
42+
possibleNames.reserve(iterable.size());
5643
fillVectorOfStringFrom(iterable, std::back_inserter(possibleNames), f);
5744

5845
auto spellingSuggestions = sofa::helper::getClosestMatch(name, possibleNames, numEntries, thresold);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ py::object BindingBase::__getattr__(py::object self, const std::string& attribut
361361
emitSpellingMessage(tmp, " - The link named ", selfbase->getLinks(), attributeName, 2, 0.6);
362362

363363
// Also provide spelling hints on python functions.
364-
emitSpellingMessage(tmp, " - The python attribute named ", py::cast<py::dict>(py::type::of(self).attr("__dict__")), attributeName, 5, 0.8,
364+
emitSpellingMessage(tmp, " - The python attribute named ", py::cast<py::dict>(pybind11_compat::type::of(self).attr("__dict__")), attributeName, 5, 0.8,
365365
[](const std::pair<py::handle, py::handle>& kv) { return py::cast<std::string>(std::get<0>(kv)); });
366366

367367
std::stringstream message;

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ namespace sofapython3 {
3030
template <typename T>
3131
using py_shared_ptr = sofa::core::sptr<T>;
3232

33+
namespace pybind11_compat
34+
{
35+
using namespace pybind11;
36+
class type : public pybind11::object {
37+
public:
38+
PYBIND11_OBJECT(type, pybind11::object, PyType_Check)
39+
40+
/// Return a type handle from a handle or an object
41+
static pybind11::handle handle_of(pybind11::handle h) { return handle((PyObject*) Py_TYPE(h.ptr())); }
42+
43+
/// Return a type object from a handle or an object
44+
static type of(pybind11::handle h) { return type(type::handle_of(h), borrowed_t{}); }
45+
46+
// Defined in pybind11/cast.h
47+
/// Convert C++ type to handle if previously registered. Does not convert
48+
/// standard types, like int, float. etc. yet.
49+
/// See https://github.com/pybind/pybind11/issues/2486
50+
template<typename T>
51+
static handle handle_of();
52+
53+
/// Convert C++ type to type if previously registered. Does not convert
54+
/// standard types, like int, float. etc. yet.
55+
/// See https://github.com/pybind/pybind11/issues/2486
56+
template<typename T>
57+
static type of() {return type(type::handle_of<T>(), borrowed_t{}); }
58+
};
59+
}
60+
3361
} // namespace sofapython3
3462

3563
PYBIND11_DECLARE_HOLDER_TYPE(T, sofapython3::py_shared_ptr<T>, true)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
*******************************************************************************
1818
* Contact information: contact@sofa-framework.org *
1919
******************************************************************************/
20-
21-
2220
/// Neede to have automatic conversion from pybind types to stl container.
2321
#include <pybind11/stl.h>
24-
#include <pybind11/eval.h>
22+
#include <pybind11/numpy.h>
2523

2624
#include <sofa/simulation/Simulation.h>
2725
#include <sofa/core/ComponentNameHelper.h>
@@ -69,6 +67,8 @@ using sofa::core::objectmodel::BaseObjectDescription;
6967
#include <queue>
7068
#include <sofa/core/objectmodel/Link.h>
7169

70+
SOFAPYTHON3_BIND_ATTRIBUTE_ERROR()
71+
7272
/// Makes an alias for the pybind11 namespace to increase readability.
7373
namespace py { using namespace pybind11; }
7474

@@ -484,7 +484,7 @@ py::object __getattr__(py::object pyself, const std::string& name)
484484
emitSpellingMessage(tmp, " - The child node named ", selfnode->getChildren(), name, 2, 0.8);
485485

486486
// Also provide spelling hints on python functions.
487-
emitSpellingMessage(tmp, " - The python attribute named ", py::cast<py::dict>(py::type::of(pyself).attr("__dict__")), name, 5, 0.8,
487+
emitSpellingMessage(tmp, " - The python attribute named ", py::cast<py::dict>(pybind11_compat::type::of(pyself).attr("__dict__")), name, 5, 0.8,
488488
[](const std::pair<py::handle, py::handle>& kv) { return py::cast<std::string>(std::get<0>(kv)); });
489489

490490
std::stringstream message;
@@ -495,7 +495,7 @@ py::object __getattr__(py::object pyself, const std::string& name)
495495
message << " You possibly wanted to access: " << msgendl;
496496
message << tmp.rdbuf();
497497
}
498-
throw py::attribute_error(message.str());
498+
throw pybind11::attribute_error(message.str());
499499
}
500500

501501
/// gets an item using its path (path is dot-separated, relative to the object

0 commit comments

Comments
 (0)