[C++-sig] extractor

Mike Rovner mike at bindkey.com
Thu Jan 23 23:28:52 CET 2003


I'm trying to implement an extractor.
Unfortunately I couldn't find an example of it.
Please point me to a simple one.

My code:
#include <string>
#include <vector>
#include <boost/python.hpp>
using namespace boost::python;

template <class T>
struct vec_extractor
{
  static std::vector<T>& execute(PyObject* obj)
  {
    int size=PySequence_Size(obj);
    std::vector<T> *vec=new std::vector<T>(size);
    for(int i=0; i<size; ++i)
      (*vec)[i]=extract<T>(PySequence_GetItem(obj,i));
    return *vec;
  }
};

static std::string test_ext(std::vector<bool>& v)
{
  std::string cout("[");
  for(int i=0; i<v.size(); ++i)
    cout += (v[i]? 'T': 'F') << ' ';
  cout += ']';
  //delete &v;
  return cout;
}

BOOST_PYTHON_MODULE(ext)
{
  lvalue_from_pytype<vec_extractor<bool>,&PyTuple_Type>();
  lvalue_from_pytype<vec_extractor<bool>,&PyList_Type>();

  def("test", test_ext);
}

dies in execute, because it get an invalid obj pointer(0x1).
In normalized_extractor::execute(pyobj) I got PyObj just fine,
but return from
template <class U>
inline U& void_ptr_to_reference(
    void const volatile* p,
    U&(*)())
{ return *(U*)p;}
gives me an 1.

Can you please direct me a little bit futher.

Mike

PS. I'm studying
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/p
ython/doc/v2/reference.html every minute and the reason I'm asking is my
lack of understanding. :(







More information about the Cplusplus-sig mailing list