On Mittwoch, März 12, 2003, at 06:16 Uhr, David Abrahams wrote:
harold fellermann <harold@imb-jena.de> writes:
Thank you, this works fine. But I still cannot get along with my stuff... You're not being (completely) honest with us ;-) There's a class called "Node" somewhere which you haven't told us about.
True, the X and Y-classes where to much of abstraction ... so here is the code that troubles me, and I hope I won't cut out anything essential this time. class Node { unsigned id; Network *net; // some stupid data that has nothing to do with python nor boost... }; class Network : public Node { public: virtual size_t add_node(Node &); python::list nodes; private: BGL_GRAPH graph; // BGL_GRAPH is a typedef defined somewhere other }; My implementation of Network::add_node is this: size_t Network::add_node(Node &node) { // get new BGL-node size_t id = add_vertex(graph); node.id = id; node.net = this; // insert the node in nodelist nodes.insert(0,node); return id; } and here is how I wrapped those lines: BOOST_PYTHON_MODULE(network) { class_<Node>("Node") .def_readonly("net_id", &Node::id) // [...] ; class_<Network, bases<Node> >("Network") .def_readonly("nodes", &Network::nodes) .def("addNode", &Network::add_node, with_custodian_and_ward<1,2>()) // [...] ; } I don't know whether I still need 'with_custodian_and_ward' here (historical reasons). This seems to work if compiled as a python module. At least, my little test program from network import * net = Network() node = Node() net.addNode(node) print net.nodes doesn't complain about anything. Using the same classes from C++ causes trouble. Here is my main function (forgot to copy the Py_Initialize last time, so this is not the point). int main() { Py_Initialize(); Network net; Node node; net.add_node(node); cout << "node added." << endl; return 0; } The code fails in net.add_node(node). Here once again is my debugger's backtrace (In my last posting, things where slightly more complex, but I could reduce the erroneous behaviour to the lines above): (gdb) run Starting program: /home/tsb/harold/cpp_source/test [New Thread 1024 (LWP 27184)] Program received signal SIGABRT, Aborted. [Switching to Thread 1024 (LWP 27184)] 0x401a6df1 in kill () from /lib/libc.so.6 (gdb) bt #0 0x401a6df1 in kill () from /lib/libc.so.6 #1 0x4008f06d in pthread_kill () from /lib/libpthread.so.0 #2 0x4008f5eb in raise () from /lib/libpthread.so.0 #3 0x401a84d9 in abort () from /lib/libc.so.6 #4 0x4012a5d7 in __cxxabiv1::__terminate(void (*)()) () from /usr/lib/libstdc++.so.5 #5 0x4012a624 in std::terminate() () from /usr/lib/libstdc++.so.5 #6 0x4012a7a6 in __cxa_throw () from /usr/lib/libstdc++.so.5 #7 0x4004df59 in boost::python::throw_error_already_set() () from /usr/local/lib/libboost_python.so.1.29.0 #8 0x080e9cb4 in boost::python::converter::arg_to_python<Node>::arg_to_python(Node const&) () #9 0x080e8679 in _object* boost::python::api::object_initializer<false, false>::get<Node>(Node const*, int*) () #10 0x080e7542 in boost::python::api::object::object<Node>(Node const&) () #11 0x080e6458 in Network::add_node(Node&) () #12 0x080e1e20 in main () #13 0x401954a2 in __libc_start_main () from /lib/libc.so.6 Why does the code run in python but not in C++? There must be a logical error I made, but I can't see it. Thank you very much for your help. By the way: as you can guess, I'm a newbie to boost::python (also with absolutely no knowledge about the Python-C-API). I think that there is a little gap between the short tutorial introduction and the rather technical reference. Is there any additional documentation that could narrow this gap? If not, I'd like to enhance the tutorial to boost::python, as soon as I look through the library, so that newbies like me have no need to steel your time with their questions. Is this a deal? - harold - -- Always remember that you are unique; just like everyone else. --