Skip to content
Snippets Groups Projects
Commit 4ea1c64a authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Fixed bug with setAttrPy

parent 6872857b
No related branches found
No related tags found
1 merge request!16Unified interface for attributes
Pipeline #32430 passed with warnings
...@@ -89,6 +89,8 @@ class test_operator_binding(unittest.TestCase): ...@@ -89,6 +89,8 @@ class test_operator_binding(unittest.TestCase):
# Check that added Python attribute is accessible in C++ # Check that added Python attribute is accessible in C++
# Return the value of an attribute named "d" of type float64 (double in C++) # Return the value of an attribute named "d" of type float64 (double in C++)
self.assertEqual(aidge_core.test_DynamicAttributes_binding_check(attrs), 18.56) self.assertEqual(aidge_core.test_DynamicAttributes_binding_check(attrs), 18.56)
attrs.set_attr("d", 23.89)
self.assertEqual(aidge_core.test_DynamicAttributes_binding_check(attrs), 23.89)
def test_compute_output_dims(self): def test_compute_output_dims(self):
in_dims=[25, 25] in_dims=[25, 25]
......
...@@ -48,7 +48,7 @@ public: ...@@ -48,7 +48,7 @@ public:
template<class T> T& getAttr(const std::string& name) template<class T> T& getAttr(const std::string& name)
{ {
#ifdef PYBIND #ifdef PYBIND
// If attribute does not exist in C++, it might have been created in Python // If attribute does not exist in C++, it might have been created or modified in Python
auto it = mAttrs.find(name); auto it = mAttrs.find(name);
if (it == mAttrs.end()) { if (it == mAttrs.end()) {
auto itPy = mAttrsPy.find(name); auto itPy = mAttrsPy.find(name);
...@@ -66,7 +66,7 @@ public: ...@@ -66,7 +66,7 @@ public:
template<class T> const T& getAttr(const std::string& name) const template<class T> const T& getAttr(const std::string& name) const
{ {
#ifdef PYBIND #ifdef PYBIND
// If attribute does not exist in C++, it might have been created in Python // If attribute does not exist in C++, it might have been created or modified in Python
auto it = mAttrs.find(name); auto it = mAttrs.find(name);
if (it == mAttrs.end()) { if (it == mAttrs.end()) {
auto itPy = mAttrsPy.find(name); auto itPy = mAttrsPy.find(name);
...@@ -129,7 +129,11 @@ public: ...@@ -129,7 +129,11 @@ public:
#ifdef PYBIND #ifdef PYBIND
void addAttrPy(const std::string& name, py::object&& value) void addAttrPy(const std::string& name, py::object&& value)
{ {
mAttrsPy.emplace(std::make_pair(name, value)); auto it = mAttrs.find(name);
assert(it == mAttrs.end() && "attribute already exists");
const auto& res = mAttrsPy.emplace(std::make_pair(name, value));
assert(res.second && "attribute already exists");
} }
void setAttrPy(const std::string& name, py::object&& value) void setAttrPy(const std::string& name, py::object&& value)
...@@ -137,6 +141,9 @@ public: ...@@ -137,6 +141,9 @@ public:
auto resPy = mAttrsPy.emplace(std::make_pair(name, value)); auto resPy = mAttrsPy.emplace(std::make_pair(name, value));
if (!resPy.second) if (!resPy.second)
resPy.first->second = std::move(value); resPy.first->second = std::move(value);
// Force getAttr() to take attribute value from mAttrsPy and update mAttrs
mAttrs.erase(name);
} }
#endif #endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment