[C++-sig] Re: embedding examples

David Abrahams dave at boost-consulting.com
Mon Mar 1 14:15:00 CET 2004


Ingo Luetkebohle <ingo at fargonauten.de> writes:

> Hi,
>
> I have trouble compiling the 'Using the interpreter' examples from the
> tutorial.
>
> Code like this:
>   handle<> main_module(borrowed( PyImport_AddModule("__main__") ));
>   dict main_namespace(handle<>(borrowed(
> PyModule_GetDict(main_module.get()) )));
> results in 
>   error: variable declaration is not allowed here
>
> using gcc 3.2 and gcc 3.3 on Linux.
>
> When I use
>   handle<> main_module(borrowed( PyImport_AddModule("__main__") ));
>   handle<> main_nsh(borrowed( PyModule_GetDict(main_module.get()) ));
>   dict main_namespace(main_nsh);
>
> it compiles and the resulting dict is usable.
>
> So, this is not a showstopper but it doesn't work as documented and
> results in longer code.  Is there anything I can do to get the intended
> behaviour?

[joel, can you fix this?]

I think this will work.  Please let us know if it doesn't:

    // Retrieve the main module
    object main_module = python::extract<python::object>(
        PyImport_AddModule("__main__")
    );
    
    // Retrieve the main module's namespace
    dict main_namespace(
         extract<dict>(main_module.attr("__dict__"))
    );

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list