[C++-sig] using extract with embedded boost::python

Simon Pickles sipickles at hotmail.com
Mon Jun 4 15:27:28 CEST 2007


Thanks Stefan.

On consulting libs/python/tests/exec.cpp I got the extract to work, with 
simple data types, like int and std::string.

How can I do the same for a function. I tried:

// 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!"


This gives an error. Its the example from the documentation.

Then, can I also pass parameters?

As for PyErr_Fetch, since I am not using a console application (win32), 
I don't know what to do with PyErr_Fetch or PyErr_Print! Sorry, not big 
on console apps!

Keep up the good work! I'm loving what I see!

Regards

Simon


Stefan Seefeld wrote:
> Simon Pickles wrote:
>   
>> Sorry, A cut-n-paste error, the code which works is the boost example:
>>
>> object main_module((
>>      handle<>(borrowed(PyImport_AddModule("__main__")))));
>>
>> object main_namespace = main_module.attr("__dict__");
>>
>> handle<> ignored((PyRun_String(
>>
>>     "result = 5 ** 2"
>>
>>     , Py_file_input
>>     , main_namespace.ptr()
>>     , main_namespace.ptr())
>> ));
>>
>> int five_squared = extract<int>(main_namespace["result"]);
>>     
>
> Please have a look at the tests that are part of the boost source distribution.
> Notably, libs/python/tests/exec.cpp contains functionality similar to the above,
> and is shown to work correctly on all tested platforms.
>
> Also, it would be good to figure out what (python) exception is raised (indicated
> by the exception_already_set in C++). You can query this using PyErr_Fetch().
> (I'm hoping I will get around to wrap that and related functions in boost.python
> in time for boost 1.35.)
>
> Regards,
> 		Stefan
>
>   

-- 
http://www.simonpickles.com  --- http://www.squirtualreality.com 




More information about the Cplusplus-sig mailing list