[C++-sig] [boost.python] Indexing suite problems with convertable element type
Kirill Lapshin
kir at lapshin.net
Thu Sep 7 21:37:36 CEST 2006
Hi,
I am just making my first step in boost.python world, so please forgive
me for potential FAQ, but I can't find an answer in docs and by googling.
I have a custom date class, that I registered for conversion to Python's
datetime.date object. Individual dates convert fine, but when I
registered vector<date> with indexing suite, I have a problem that I can
insert python date objects into vector, but can't extract them. E.g.
>>> v = vector_day()
>>> v.append( datetime.date(2006,1,1) )
>>> len(v)
1
>>> v[0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: No Python class registered for C++ class class day
vector_day is exposed like this:
bp::class_< std::vector<day, std::allocator<day> > >( "vector_day"
)
.def( bp::vector_indexing_suite<
::std::vector<day, std::allocator<day> > >() );
As I said, individual date objects convert just fine back and forth,
i.e. function
day test_day( day x ) { return x+1; }
exposed as
typedef day ( *function_ptr_t )( day );
bp::def( "test_day", function_ptr_t( &test_day ), ( bp::arg("x") ),
bp::default_call_policies() );
can be used from Python without problems:
>>> test_day( datetime.date(2006,1,1) )
datetime.date(2006, 1, 2)
What am I doing wrong?
Regards,
Kirill Lapshin
More information about the Cplusplus-sig
mailing list