Dynamic attributes of objects stored as std::shared_ptr's in std::vector's
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
I would like to add dynamic attributes to objects stored as shared pointers in std::vector's. I have read the docs and I'm still not sure whether this is truly possible or no. However, while performing some tests I have discovered an inconsistency that pybind11 devs might be interested in.
This inconsistent behaviour can be reproduced with the following c++ code:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
#include <memory>
namespace py = pybind11;
class Foo {
public:
Foo() {}
~Foo() {}
};
class Bar {
public:
Bar() {
foos.push_back(std::make_shared<Foo>());
}
~Bar() {}
std::vector<std::shared_ptr<Foo>> foos;
};
PYBIND11_MODULE(pybug, m) {
py::class_<Foo, std::shared_ptr<Foo>> foo(m, "Foo", py::dynamic_attr());
foo
.def(py::init<>());
py::class_<Bar> bar(m, "Bar");
bar
.def(py::init<>())
.def_readwrite("foos", &Bar::foos);
}
which can be compiled (at least on my Linux box) with
g++ pybug.cpp -shared --std=c++11 -fPIC `python3 -m pybind11 --includes` -o pybug`python3-config --extension-suffix`
Now, on the python side the following snippet works or not depending whether the line starting with myfoo is commented or not:
import pybug
bar = pybug.Bar()
myfoo = bar.foos[0] # if this line is commented out then the subsequent print line will raise an "AttributeError"
bar.foos[0].b = 2
print(bar.foos[0].b)
Is there some hidden caching going on here (as suggested by this answer)? Is it possible to add dynamic attributes to the instances stored in the foos vector without keeping an explicit reference to each of them?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproducer in pybug.cpp and compile it using the command in the issue, then run the provided Python snippet with and without the myfoo reference. Trace the two access paths for bar.foos[0] and determine whether dynamic attributes persist consistently; done means the behavior is explained and the issue is fixed or clearly documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100