[C++-sig] Re: Embedding Tutorial

Dirk Gerrits dirk at gerrits.homeip.net
Thu Dec 19 08:30:48 CET 2002


William Trenker wrote:
> I've just been studying the embedding tutorial and wondering if it is 
> really necessary to use BOOST_PYTHON_MODULE() in all cases.  Is what 
> follows feasible?
> 
> #include <boost/python.cpp>
> #include <stdio.h>
> using namespace boost::python;
> int main(int argc, char** argv): {
>    Py_Initialize();
>    handle<> main_module( borrowed(PyImport_AddModule("__main__")) );
>    handle<> main_namespace( borrowed(PyModule_GetDict(main_module)) );
>    handle<> result( PyRun_String("1 + 1",
>            Py_eval_input, main_namespace.get(), main_namespace.get()) );
>    object py_result(result);
>    int result = extract<int>(py_result);
>    printf("result = %i\n",result);
>    //Py_Finalize();  AVOID (for now)
>    return 0;
> };
> 
> (The above may have errors; I haven't been able to test the code. 

Looks okay, except for the python.cpp instead of python.hpp of course. 
;) And <cstdio> is the C++ way of writing <stdio.h> but that's nitpicking.

> I don't yet know how to make a Jamfile that builds a boost.python-enbled 
> executable rather than an extension module.  So please consider this as 
> psuedocode for now.)
> 
> Feedback is appreciated,

I apoligize if I didn't make it clear in the tutorial that using 
BOOST_PYTHON_MODULE in your embedded programs is something you CAN do 
instead of something you MUST do.

I'm still working on the tutorial, well at least I will be working on it 
again next week when my Christmas vacation starts. ;) If you think I 
should make this issue clearer, I shall try to do so. Other things on 
the agenda are explaining exception handling, a template Jamfile and 
moving the 'C-way' of doing things into a FAQ/rationale section instead 
of the tutorial.

Regards,
Dirk Gerrits







More information about the Cplusplus-sig mailing list