[C++-sig] Somewhat beginners error in using boost::python::list from C++?

2011-01-15 Thread Michael Wand
Hello,

sorry if this question is too trivial, but I've got some rather strange problem
with using the boost::python type wrapper for the python list.

It goes as follows: I've got a very small demo program (say main.cpp). 
Running it causes a segfault. (The actual project is much larger, of course.)

The system I use is a Linux OpenSuse 11.1, with the distribution package 
python-2.6.0-2.23.1 
installed. I manually installed boost 1.44 (because the newer versions didn't 
quite work with the 
boost-log package, which is needed for the actual project). Which other 
information might be helpful?

This is the program:

***
#include 
#include 

using namespace std;

int main() {
boost::python::list myList;
myList.append("foo");
myList.append("fork");
cerr << "Printing length of list." << endl;
cerr << "Length of myList: " << boost::python::len(myList) << endl; 
// the above line causes a segfault!
cerr << "TheList: " << boost::python::extract(myList[0])() << ","
  << boost::python::extract(myList[1])() << endl;
cerr << "Finished" << endl;
}
***

which I compile by the two commands:

/usr/bin/c++-O0 -Wall -g -I. -I/usr/include/python2.6 -I/usr/local/include  
 -o main.o -c main.cpp
/usr/bin/c++   -O0 -Wall -g   main.o  -o boost_list_test -rdynamic 
-L/usr/local/lib /usr/local/lib/libboost_python.so -lpython2.6 
-Wl,-rpath,/usr/local/lib

The segfault occurs at the call to boost::python::len(myList). I debugged 
the program with gdb, but the backtrace
#0  0xb7ec6027 in PyErr_Occurred () from /usr/lib/libpython2.6.so.1.0
#1  0x0804b0b1 in main () at main.cpp:15

doesn't really tell me anything. Interestingly, removing the offending line 
causes a segfault at the *next* line, with a more verbose backtrace:

#0  0xb7e529d7 in PyInt_FromLong () from /usr/lib/libpython2.6.so.1.0
#1  0x0804ba33 in arg_to_python (this=0xbfffee70, x=@0xbfffef48) at 
/usr/local/include/boost/python/converter/builtin_converters.hpp:122
#2  0x0804ba5d in boost::python::api::object_initializer_impl::get (x=@0xbfffef48) at 
/usr/local/include/boost/python/object_core.hpp:393
#3  0x0804ba9e in boost::python::api::object_base_initializer 
(x=@0xbfffef48) at /usr/local/include/boost/python/object_core.hpp:315
#4  0x0804bab1 in object (this=0xbfffeedc, x=@0xbfffef48) at 
/usr/local/include/boost/python/object_core.hpp:334
#5  0x0804bae1 in 
boost::python::api::object_operators::operator[]
 (this=0xbfffef28, key=@0xbfffef48)
at /usr/local/include/boost/python/object_items.hpp:61
#6  0x0804af46 in main () at main.cpp:17

I really don't understand what I'm doing wrong. Do I need to resize the list 
before using append? The documentation
(http://www.boost.org/doc/libs/1_44_0/libs/python/doc/v2/list.html) doesn't say 
anything about this.

Any tips or hints are greatly appreciated!

Thankyou,

- Michael Wand
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Somewhat beginners error in using boost::python::list from C++?

2011-01-15 Thread Stefan Seefeld

On 2011-01-15 09:01, Michael Wand wrote:

int main() {
 boost::python::list myList;
 myList.append("foo");
 myList.append("fork");
 cerr<<  "Printing length of list."<<  endl;
 cerr<<  "Length of myList: "<<  boost::python::len(myList)<<  endl;
// the above line causes a segfault!
 cerr<<  "TheList: "<<  boost::python::extract(myList[0])()<<  ","
   <<  boost::python::extract(myList[1])()<<  endl;
 cerr<<  "Finished"<<  endl;
}


You haven't initialized the Python runtime with "Py_Initialize()".

Stefan



--

  ...ich hab' noch einen Koffer in Berlin...

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig