[C++-sig] Calling list.__len__() from C++ causes crash with >= 100 elements

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Sun Aug 6 08:48:47 CEST 2006


--- Geoffrey French <gsculpt at googlemail.com> wrote:
> void printListLength(boost::python::object elements)
> {
>     boost::python::extract<int> lenExtract( elements.attr( "__len__" )() );
>     if ( lenExtract.check() )
>     {
>         int numElements = lenExtract;
>         std::cout << numElements << " elements" << std::endl;
>     }
> }

I can reproduce your problem under FC3 and FC5 (both x86_64). I am not sure if
this is a bug or a feature. In any case, this workaround does the trick for me:

void printListLength(boost::python::object elements)
{
    boost::python::object len_result = elements.attr( "__len__" )();
    boost::python::extract<int> lenExtract( len_result );
    if ( lenExtract.check() )
    {
        int numElements = lenExtract;
        std::cout << numElements << " elements" << std::endl;
    }
}

BTW: With the latest boost CVS you can simply write

  std::cout << len(elements) << " elements" << std::endl;


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Cplusplus-sig mailing list