[C++-sig] Nested Boost::Python dictionaries
Jim Bosch
talljimbo at gmail.com
Sat May 12 22:03:44 CEST 2012
On 05/12/2012 12:22 PM, DarkAnt wrote:
> I'm trying to create a boost::python::dict that stores another
> boost::python::dict.
>
> int main()
> {
> Py_Initialize();
> boost::python::dict parent;
> try{
> parent["child_dict"] =
> boost::make_shared<boost::python::dict>(boost::python::dict());
> }
> catch(...){
> PyErr_Print();
> }
> return 0;
> }
>
> TypeError: No to_python (by-value) converter found for C++ type: class
> boost::shared_ptr<class boost::python::dict>
>
> I was under the impression that boost::shared_ptr had special
> treatment in boost::python(that is the library already knew what to do
> with it). I'm not quite sure if I'm supposed to write this to_python
> converter or if I'm supposed to achieve this in a different manner. If
> I do write the to_python converter what's the method of keeping track
> of both reference counts?
There's no need to use shared_ptr on Python objects; those will be
tracked using Python's own reference counting system.
Just doing
parent["child_dict"] = boost::python::dict();
will do what you want.
Note that boost::python::dict (as well as boost::python::object, etc.)
is actually a smart pointer itself, holding a PyObject* and
incrementing/decrementing the reference count in the C++ copy
constructor, assignment operator, and destructor.
HTH!
Jim
More information about the Cplusplus-sig
mailing list