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

Matthew Scouten matthew.scouten at gmail.com
Fri Oct 19 18:38:09 CEST 2007


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.



More information about the Cplusplus-sig mailing list