Boost logo

Boost Users :

Subject: Re: [Boost-users] [python] Defining a variable
From: Robert Dailey (rcdailey_at_[hidden])
Date: 2008-09-24 19:44:49


On Wed, Sep 24, 2008 at 5:38 PM, Robert Dailey <rcdailey_at_[hidden]> wrote:

> Hi,
>
> I'm embedding python through Boost.Python. Before I "import" or "exec" a
> specific PY file, I want to set a global variable in that script that the
> user will be able to access when the script is finally executed or imported.
> How would I do this?
>

I've come up with the following code:

    boost::python::object GetNamespace( char const* mod )
    {
        using namespace boost::python;
        object theModule = import( mod );
        return theModule.attr( "__dict__" );
    }

    template< typename t_type >
    bool AddGlobal( std::string const& name, t_type const& global )
    {
        using namespace boost::python;
        object main = GetNamespace( "__main__" );
        main[name] = global;
        return true;
    }

However, the above results in the following exception if I pass a pointer
type to the 'global' parameter in my AddGlobal function. Note that the
pointer type is that of a user defined class, like Interface*:

*First-chance exception at 0x7d4e2366 in Crusades_Debug.exe: Microsoft C++
exception: boost::python::error_already_set at memory location 0x0012ed44..*

Anyone know why this is happening? I haven't exposed the class in question
(class Interface) through class_(). Could this be the problem?



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net