python-list to c++-vector

Mmmm ... if you really want to only have list, you may use
boost::python::list as argument : simpler, clearer and boost.python does
the verification job for you !


But if you want to be able to extract every sequence, I would suggest
the use of python iterator as it's more general. For that, you should
use the PyObject_GetIter function (or there is a boost object ???) and
then use the "next" attribute like this :

void set( boost::python::object o ) 
{
  try
    {
      object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ) );
      while( 1 )
	{
	  object obj = extract<object>( iter_obj.attr( "next" )() ); // Should always work
	  int val = extract<int>( obj ); // Should launch an exception if you wannot extract an int ...
	  _ints.push_back(val);
	}
    }
  catch( error_already_set )
    {
      PyErr_Clear(); // If there is an exception (no iterator, extract failed or end of the list reached), clear it and exit the function
      return;
    }
}





The only pb I have with my function is you cannot make the difference
between the various exceptions.


Le lun 29/09/2003 à 18:04, Lars Kunert a écrit :
> Hi!

> Is this here really the easiest and the fastest way to transfer a small 
> (<10000) python-list of integers into a std::vector?! 




> // Using 
> =======================================================================
> using namespace boost::python;

> // Module 
> ======================================================================
> BOOST_PYTHON_MODULE(Iterator_module)
> {
>     class_< Ints >("Ints", init<>())
>         .def("set", &Ints::set )
>         ;
> };

> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
-- 
Pierre Barbier de Reuille


INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France


tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值