Hi,
I have two questions and a contribution regarding using PyBindGen.
Q 1) ReferenceCountingMethodsPolicy seems to show that the peekref parameter is optional. That's good, because our project does not have such a capability. However, when I pass only incref_method and decref_method I get the following.
Code generation and compilation gives me this:
static int
PyWorld__tp_traverse(PyWorld *self, visitproc visit, void *arg)
{
Py_VISIT(self->inst_dict);
if (self->obj && typeid(*self->obj) == typeid(PyWorld__PythonHelper) && self->obj->None() == 1)
Py_VISIT((PyObject *) self);
return 0;
}
Which results in this error:
world-binding.cpp: In function ‘int PyWorld__tp_traverse(PyWorld*, int (*)(PyObject*, void*), void*)’:
world-binding.cpp:458:89: error: ‘class World’ has no member named ‘None’
So my question is, why would it check for unique ownership. I know it's to do with Python garbage collection, but I can't think of a scenario where the check is useful here. Also, how can I fix this (without hacking our project to include a peekref.. I don't have that level of access).
Q 2) I am using add_container to support properties of type std::vector<World*>, which works, but because it wraps the returned vector I am left with a lot of pointers that haven't had their reference count incremented living outside of the original container. Should the original object drop its references, I'll have stale pointers. Is there a way to have PyBindGen return a python copy of the list, with object wrapper entries instead, so that normal memory management rules apply, and reference counts have been incremented, and ultimately will be decremented when the list is deleted?
Contribution) I have copied and modified the std::string support classes for Glib::ustring. I'd like to give these back to the project, as I think that they are generally useful. How do I do this?
Any questions, if I have missed something etc, please ask.
And a final note, I have searched google silly, gone over the docs many times and tried to understand the source code. I am not asking these questions without having spent a long time trying to find the answers first.
Many thanks,
Michael.
My example class description.
mod = pybindgen.Module('MyModule')
mempolicy = pybindgen.cppclass.ReferenceCountingMethodsPolicy(incref_method='reference', decref_method='unreference')
mod.add_include('"world.h"')
klass = mod.add_class('World', memory_policy=mempolicy, automatic_type_narrowing=True, allow_subclassing=True)
mod.add_container('std::vector<World*>', 'World*', 'vector')
klass.add_constructor([])
klass.add_method('do_hello', None, [param('Glib::ustring&', 'val')], is_virtual=True, custom_name='_do_hello', visibility='protected')
klass.add_method('do_other', retval('World *', caller_owns_return=True), [], is_virtual=True, custom_name='_do_other', visibility='protected')
klass.add_instance_attribute('name', retval('Glib::ustring'), getter='name', setter='set_name')
klass.add_instance_attribute('sizes', retval('std::vector<World*>'), getter='sizes', setter='set_sizes')
klass.add_method('hello', None, [param('Glib::ustring&', 'val')], custom_name='hello', visibility='public')
klass.add_method('other', retval('World *', caller_owns_return=True), [], custom_name='other', visibility='public')
mod.generate(sys.stdout)