Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This makes it possible for pybind11 classes to pick up new attributes on the fly, just like regular Python classes. The feature is enabled using
py::dynamic_attr
:Previous discussions about dynamic attributes: #180, #270, #275. Compared to before, this PR adds
tp_traverse
andtp_clear
in order to let Python's garbage collector resolve any reference cycles.test_cyclic_gc
specifically tests a situation which would fail without opting into the GC.The addition of
__dict__
also means that pickling needs to be handled differently for these classes. ThePickleableWithDict
test was added for this reason.If the implementation looks good, I'll add a section to the documentation.
Question about function linkage: This may be a little off-topic, but I noticed it while adding
traverse
andclear
. The existing functions which are passed to Python (generic_type::init
,generic_type::new_instance
and similar) are all just regular C++ functions. I was under the impression that these needed to be marked withextern "C"
in order to get the correct linkage before being passed to Python's C API. Obviously, it works as it is now, but are there any portability concerns since it shouldn't technically be allowed to call a function with C++ linkage from C?