Jump to content
  • Advertisement
Sign in to follow this  
sipickles

Getting started with boost::python - Exception already!

This topic is 4006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to embed python with boost::python. Unfortunately, I've come unstuck already. Here's my code
	object main_module( ( handle<>( borrowed( PyImport_AddModule( "__main__" ) ) ) ) );

	object main_namespace = main_module.attr("__dict__");

	FILE* myPython = fopen( "myPython.py", "r" );

	handle<> ignored( ( PyRun_File( myPython, "myPython.py",
						Py_file_input, 
						main_namespace.ptr(), 
						main_namespace.ptr() ) ) );

	fclose( myPython );

and heres the exception: First-chance exception at 0x7c918fea in Server 20070601.exe: 0xC0000005: Access violation writing location 0x00000010. Unhandled exception at 0x7c918fea in Server 20070601.exe: 0xC0000005: Access violation writing location 0x00000010. What am I doing wrong? must be something simple :) Thanks! Simon EDIT: some info may help.... MSVC++ 2005 Express Boost 1.34 Python 2.52 [Edited by - sipickles on June 1, 2007 1:15:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
I've managed to make a little progress by using boost::python::exec_file, now available in boost 1.34.

Now I struggle to extract anything from a python function apart from std::string and int.

Here's what I am trying to do:

// C++
python::dict global;
python::object result = python::exec_file("myPython.py", global, global)l

python::object greet = global["greet"];
std::string message = python::extract<std::string>(greet());


// MYPYTHON.PY
def greet(self):
return "hello, world!"




The extract attempt gives 'error_already_set' exception.

Help!

Share this post


Link to post
Share on other sites
If you run that stand-alone Python code, you'll see that it doesn't work, and raises an exception. Hint: that's not a function you've defined, that's a method.

(PS. Also copy and paste in your actual code, rather than typing it out separately, so that we can see if there are syntax errors that may contribute to the problem.)

Share this post


Link to post
Share on other sites
Yes, you are right. I was foolishly copying code from the boost.org tutorials

Oh well! Guess I'll have to keep twiddling til I randomly hit upon the solution!

Here's the cut and paste code:

	try
{
// Run a python script in an empty environment.
python::dict global;
python::object result = python::exec_file("myPython.py", global, global);

int i= 0;
// Extract an object the script stored in the global dictionary.

i = python::extract<int>(global["number"]);
g_log->Log("success!! %d\n", i);

std::string animal = python::extract<std::string>(global["animal"]);
g_log->Log("animal == %s\n", animal.c_str());

// Create a reference to it.
python::object greet = global["greet"];

// Call it.
std::string message = python::extract<std::string>(greet);
g_log->Log("%s\n", message.c_str());
}
catch(python::error_already_set)
{
g_log->SYSTEM( "Python Error\n" );
}




number = 42
animal = "aardvark"

def greet():
return animal


it excepts on the line:

python::object greet = global["greet"];

I guess this means there is no greet object in the python global namespace...

Share this post


Link to post
Share on other sites
It could mean anything. There'll be a way to get hold of that exception and find out what it means, so that would be worth looking into.

Share this post


Link to post
Share on other sites
Though you seem to have gotten around this, you still might want to know. For some reason, PyRun_File can't take a regular FILE* for it's argument. I can't remember the actual reason. It's something about Python wanting one format of FILE but Windows has a different one. Put simply, you can't use PyRun_File. I'm not at home right now, but when I am, I'll try looking for where I found this.

Share this post


Link to post
Share on other sites
Yeah, it depends whether the library was built with the same compiler as you are using. You can rebuild from source to overcome this, apparently!

Share this post


Link to post
Share on other sites
Well, the solution turned out to be simple. Not sure I TOTALLY understand why, its like I had to instantiate a function, then extract the object produced:

	try
{
// Run a python script in an empty environment.
python::dict global;
python::object result = python::exec_file("myPython.py", global, global);

int i= 0;
// Extract an object the script stored in the global dictionary.

i = python::extract<int>(global["number"]);
g_log->Log("number = %d\n", i);

std::string animal = python::extract<std::string>(global["animal"]);
g_log->Log("string == %s\n", animal.c_str());


// Call it.
std::string message = python::extract<std::string>(global["passedFunc"]);
g_log->Log("%s\n", message.c_str());
}
catch(python::error_already_set)
{
g_log->SYSTEM( "Python Error\n" );
}





number = 42
animal = "aardvark"

def pythonFunc():
return "Hello from Python"

passedFunc = pythonFunc()



Bingo! :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!