
Hi guys, Is boost::python supposed to work with msvc2005 ? I took the hello world from "embedding python" and "error_already_set" is being thrown at me... Here's the code I used : #include <iostream> #include <string> #include <boost/python.hpp> using namespace boost::python; void greet() { Py_Initialize(); // Retrieve the main module. object main = import("__main__"); // Retrieve the main module's namespace object global(main.attr("__dict__")); // Define greet function in Python. object result = exec( "def greet(self): \n" " return 'Hello from Python!' \n", global, global); // Create a reference to it. object greet = global["greet"]; // Call it. std::string message = extract<std::string>(greet()); std::cout << message << std::endl; Py_Finalize(); } int main() { greet(); return 0; }

Philippe Vaucher wrote:
Hi guys,
Is boost::python supposed to work with msvc2005 ?
Yes.
I took the hello world from "embedding python" and "error_already_set" is being thrown at me...
Can you find out what the python exception is that triggered it ?
Here's the code I used :
[...]
Py_Finalize();
Don't call Py_Finalize, as that doesn't work with boost.python. (Unfortunately that didn't use to be documented in the tutorial, but now is, with the boost python tutorial that will ship with 1.34.1) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...

Can you find out what the python exception is that triggered it ?
Here is my call stack and the code http://cpp.sourceforge.net/?show=38616 The line throwing the exception is marked with a comment. Don't call Py_Finalize, as that doesn't work with boost.python.
(Unfortunately that didn't use to be documented in the tutorial, but now is, with the boost python tutorial that will ship with 1.34.1)
Hum ok. Cheers for help! Philippe

Can you find out what the python exception is that triggered it ?
Here is my call stack and the code http://cpp.sourceforge.net/?show=38616 The line throwing the exception is marked with a comment.
By the way it's not really python that fails... it's code in errors.hpp : template <class T> inline T* expect_non_null(T* x) { if (x == 0) throw_error_already_set(); return x; } Philippe

on Mon Jul 23 2007, "Philippe Vaucher" <philippe.vaucher-AT-gmail.com> wrote:
Can you find out what the python exception is that triggered it ?
Here is my call stack and the code http://cpp.sourceforge.net/?show=38616 The line throwing the exception is marked with a comment.
By the way it's not really python that fails... it's code in errors.hpp :
template <class T> inline T* expect_non_null(T* x) { if (x == 0) throw_error_already_set(); return x; }
No, it really is Python that fails. A null pointer there is just Python's way of reporting an error. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com

On Mon, Jul 23, 2007 at 03:29:40PM +0200, Philippe Vaucher wrote:
// Define greet function in Python. object result = exec( "def greet(self): \n" " return 'Hello from Python!' \n", global, global);
This is a doc bug, greet(self) should be greet(), since it's a regular function, not a method in a class. I'm fairly sure that this has been reported, but I can't find any ticket for it. If you had caught the exception in python, it would have been something along the lines of: TypeError: greet() takes exactly 1 argument (0 given) -- Lars Viklund ------------------- To make it is hell. To fail is divine.

on Mon Jul 23 2007, zao-AT-acc.umu.se (Lars Viklund) wrote:
On Mon, Jul 23, 2007 at 03:29:40PM +0200, Philippe Vaucher wrote:
// Define greet function in Python. object result = exec( "def greet(self): \n" " return 'Hello from Python!' \n", global, global);
This is a doc bug, greet(self) should be greet(), since it's a regular function, not a method in a class. I'm fairly sure that this has been reported, but I can't find any ticket for it.
Please create one, then. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com

On Mon, Jul 23, 2007 at 06:51:22PM -0400, David Abrahams wrote:
on Mon Jul 23 2007, zao-AT-acc.umu.se (Lars Viklund) wrote:
This is a doc bug, greet(self) should be greet(), since it's a regular function, not a method in a class. I'm fairly sure that this has been reported, but I can't find any ticket for it.
Please create one, then.

FWIW, I think the boost::python doc really lack small example of simple things, like: 1) How am I supposed to iterate over boost::python::object::operator[] ? 2) How am I supposed to iterate over boost::python::object::attr() ? After googling a lot I found out that for 2) I could do : boost::python::dict = boost::python::extract<boost::python::dict>(obj.attr ("__dict__")); boost::python::object it = dict.iterkeys(); while(it) { // use boost::python::extract<yourtype>(it()) here it.attr("next")(); } But to find that out I really had to sweat. And I'm still looking for 1)... and not close to find out because few people embed python into C++. Philippe

This is a doc bug, greet(self) should be greet(), since it's a regular function, not a method in a class. I'm fairly sure that this has been reported, but I can't find any ticket for it. If you had caught the exception in python, it would have been something along the lines of: TypeError: greet() takes exactly 1 argument (0 given)
You are right, removing "self" makes my code work like a charm :) Thank you. Philippe
participants (4)
-
David Abrahams
-
Philippe Vaucher
-
Stefan Seefeld
-
zao@acc.umu.se