[python] Defining a variable

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?

On Wed, Sep 24, 2008 at 5:38 PM, Robert Dailey
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?

Robert Dailey wrote:
On Wed, Sep 24, 2008 at 5:38 PM, Robert Dailey
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?
How about: scope().attr("something") = whatever;

Robert Dailey wrote:
On Wed, Sep 24, 2008 at 7:08 PM, Neal Becker
wrote: Robert Dailey wrote:
<snip>
How about: scope().attr("something") = whatever;
Where would this code go? What does scope() do?
You could put it here: BOOST_PYTHON_MODULE ... { scope().attr ("something") = 42;

On Thu, Sep 25, 2008 at 6:31 AM, Neal Becker
Robert Dailey wrote:
On Wed, Sep 24, 2008 at 7:08 PM, Neal Becker
wrote: Robert Dailey wrote:
<snip>
How about: scope().attr("something") = whatever;
Where would this code go? What does scope() do?
You could put it here: BOOST_PYTHON_MODULE ... {
scope().attr ("something") = 42;
I can't do it where I define the module because I'm passing a pointer to an object to python. This needs to be available as the script is executed, and this pointer value is only known to a certain class (It isn't globally accessible, so it couldn't possibly be set where the python module is defined). I know it needs to be done the way I originally posted, but I need to know why it is throwing an exception. Thanks for the help.

Robert Dailey
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?
Please try to compile in release mode to see if it works. If it only crashes in Debug mode then you are a victim of the decision to have boost::python Debug link to Python release version and the trouble that follows from this. See <http://www.nabble.com/-python--partially-resolved:-the-never-ending-story- of-vc8-win32-linker-settings-desaster-td18945799.html> about the details. Carefully check the verbose linker output to see what happens. Markus

On Thu, Sep 25, 2008 at 5:10 AM, Markus Werle
Robert Dailey
writes: 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?
Please try to compile in release mode to see if it works. If it only crashes in Debug mode then you are a victim of the decision to have boost::python Debug link to Python release version and the trouble that follows from this. See < http://www.nabble.com/-python--partially-resolved:-the-never-ending-story- of-vc8-win32-linker-settings-desaster-td18945799.htmlhttp://www.nabble.com/-python--partially-resolved:-the-never-ending-story-of...> about the details.
Carefully check the verbose linker output to see what happens.
We specifically compiled Boost 1.36 to link against the debug python libraries when boost debug is built. I used dependency walker to ensure it was not loading the release library.
participants (3)
-
Markus Werle
-
Neal Becker
-
Robert Dailey