[C++-sig] embedded python ?

troy d. straszheim troy at resophonic.com
Fri Apr 3 22:01:10 CEST 2009


mad city wrote:
> Thanks. Good advice.
> 
>  From the quickstart project, the embedded.cpp file function
> 
> 
> void exec_file_test(std::string const &script)
> {
>     std::cout << "running file " << script << "..." << std::endl;
>    
>     // Run a python script in an empty environment.
>     python::dict global;
>     python::object result = python::exec_file(script.c_str(), global, 
> global);
> 
>     // Extract an object the script stored in the global dictionary.
>     BOOST_TEST(python::extract<int>(global["number"]) ==  42);
>    
>     std::cout << "success!" << std::endl;
> }
> 
> calls my python script file - parseLogs.py which starts with

try something like this.  When you exec the file you have to supply a 
globals dictionary that contains whatever globals you want to use (like 
import)

     Py_Initialize();

     bp::object main = bp::import("__main__");
     bp::object py_ = main.attr("__dict__");
     bp::exec_file("conf.py", py_, py_);
     int = bp::extract<int>(py_["some_int_is_here"];

-t


More information about the Cplusplus-sig mailing list