[C++-sig] conversion of std::list<...>

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Wed Jun 26 19:11:34 CEST 2002


> Cool! Please remind me of this on Wednesday; I am currently stuck in a
> Microsoft gravity well, trying to survive the inevitable impact.

The

struct register_container_from_python_sequence

in the bpl_utils still has an undesirable feature: the input Python sequence is
traversed twice. This does not matter if the user passes a list or tuple, but
will cause confusion (at least) if the user passes an object that can only be
traversed once (something similar to a file object for example; or some kind of
iterator).

Consider the static member functions of
register_container_from_python_sequence:

static void* convertible(PyObject* obj_ptr)
{
  //...
  sequence_fast seq(obj, 0); // first traversal
  //...
}

static void construct(PyObject* obj_ptr,
boost::python::converter::rvalue_stage1_data* data)
{
  //...
  sequence_fast seq(obj, 0); // second traversal
  //...
}

Could we somehow avoid the second traversal?

Idea: sequence_fast has a handle<> with the result of PySequence_Fast().
I know I could return the corresponding PyObject* from convertible(),
and this will then be passed as the first argument to construct(). I.e. I could
just use the PyObject* knowing it is the result of PySequence_Fast() and would
not have to call PySequence_Fast() a second time.

Problem: PySequence_Fast returns a new reference. There is no guarantee that
construct() will be called. So the reference count has to be managed outside of
convertible() and construct(). Could this be done in the current framework?

Thanks,
        Ralf


__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com





More information about the Cplusplus-sig mailing list