What you do looks right. bp::object(bp::handle<>(c_object))
may be a little shorter in some places. (Watch out for compilers confusing your code with function declarations.) bpobj.ptr() will give you the PyObject* directly. bphdl.get() does the same for handle<>. (Improvements are always welcome, but a complete set of patches is a lot of work and I doubt even wrapping the entire Python API will make much of a practical difference.) Ralf ----- Original Message ---- From: Murray Cumming <murr...@murrayc.com> To: cplusplus-sig@python.org Sent: Thu, February 4, 2010 12:16:41 PM Subject: [C++-sig] Getting a b::p::object for a PyObject and vice-versa. Can someone confirm that this is correct: 1. To get a boost::python::object that wraps an existing PyObject* (when you get a PyObject from a C function not wrapped in boost::python), can someone confirm that this is correct: Either: a) PyObject* c_object = get_the_c_object(); //returns a reference. boost::python::handle<> handle(c_object); boost::python::object cpp_object(handle); or b) PyObject* c_object = get_the_c_object(); //returns no extra reference. boost::python::handle<> handle(boost::python::borrowed(cObject)); boost::python::object cpp_object(handle); The code for b) can be simplified by doing boost::python::object cpp_object(boost::python::borrowed(cObject)) but I don't see a way to simplify a) I also see allow_null here, but like borrowed, it's not documented: http://www.boost.org/doc/libs/1_41_0/libs/python/doc/v2/handle.html#allow_null-spec so I can only guess at its purpose. In general I wish this was much simpler and stated clearly somewhere. 2. How can I get the underlying PyObject* from a boost::python::object (when I need it to call a C function not wrapped in boost::python)? I guessed at this, but it doesn't seem to be working for me: boost::python::extract<PyObject*> extractor(cpp_object); if(extractor.check()) { PyObject* c_object = extractor; } _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig