[C++-sig] stuffing a PyObject into a object

Lloyd Weehuizen lloyd at fusion.net.nz
Sat Oct 20 01:43:44 CEST 2007


Hi Matthew

I'm not sure why the code you found suggests a PyIter_Next, I've never 
used that myself. I'm guessing it was pulled from an iteration example 
at one point and lost its context.

I do my conversions like this:
python::object(python::handle<>(python::borrowed(x)))

 From what I can tell, allow_null does not return a python None, it 
simply allows null to pass through the system. After looking through the 
boost source, it looks like handle converts the NULL to a None when 
necessary (See handle.hpp:256 (get_managed_object)).

Cheers,
Lloyd

Matthew Scouten wrote:
> Right, so....
> I've got a void* that I happen to know is really a PyObject*. (how do
> i know that you ask? It was handed to me by python and tunneled
> through a 3rd party c++ library that promised not to  mess with it.) I
> need to make it back into a boost::python::object so I can hand it
> back to python.
> 
> I googled for how to do this and came across:
> http://osdir.com/ml/python.c++/2002-12/msg00286.html
> 
> where I found the following recommendation:
> 
> boost::python::handle<> h(boost::python::allow_null(PyIter_Next(x)));
> if (!h.get()) break; // end of iteration
> boost::python::object o(h);
> 
> I can understand this except for a couple of things,
> 
> What is the PyIter_Next there for? demonstration purposes? or is it an
> essential part of the conversion? Same question for that whole second
> line.
> 
> will 'allow_null' make a 'None' out of a null pointer? or do some
> other crazy thing?
> 
> more to the point, will this code work:
> 
> object foo(void* callbackID == NULL)
> {
> 	boost::python::handle<> h(boost::python::allow_null((PyObject*)callbackID))
> 	return boost::python::object o(h);
> }
> 
> I am trying to understand what is going on, and not just fall into
> guess and check programming.
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig



More information about the Cplusplus-sig mailing list