Jump to content
  • Advertisement
Sign in to follow this  
Kiryn

[Boost.Python] Calling function

This topic is 3984 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

I have this console for testing purposes and I have my python script but I can't figure out how to call the python function :\.
#include <iostream>
#include <boost/python.hpp>
#include <Python.h>
#include <string>

int main(int argc, char *argv[])
{
	using namespace boost::python;
	try
	{
		Py_Initialize();
		PySys_SetArgv(argc, argv);

		Py_Initialize();

		object main_module = import("__main__").attr("__dict__");

		std::string file = "omfgwtfbbq.py";
		exec_file(file.c_str(), main_module, main_module);
	}
	catch(error_already_set const &)
	{
		PyErr_Print();
	}
	return 0;
}

def foo():
  print 'omg'
  print 'wtf'
  print 'bbq'
  
 def bar():
  print 'rofl'

But I can't figure how to do it. :\. Any help be appricated :)

Share this post


Link to post
Share on other sites
Advertisement
*commence wild slashing at the dark*

My guess, based on 5mins looking at the boost.python docs and some knowledge which I don't understand how I even have, would be that you have to extract the function from the main_module's and call the proxy object.


object foo = extract<object>(main_module["foo"]);
foo();



At least, it seems reasonably sane to me... [grin]

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!