[C++-sig] Re: Documentation error or too much newbie?

Dirk Gerrits dirk at gerrits.homeip.net
Sat Sep 13 23:40:22 CEST 2003


yakumoklesk at yahoo.es wrote:
> Opps, sorry. In the documentation there is written
> 
> main_namespace dict(...)
> 
> I think it is
> 
> dict main_namespace(...), because dict is an boost.python object 
> representing a python dict, isn't it?

Yes, a very embarassing mistake on my part. But the fix you suggested 
has been in CVS for quite some time. Even before the 1.30.1 release I 
believe. That it's not in 1.30.1 or 1.30.2 is perhaps due to my negligance?

> But the fact is that
> 
> dict main_namespace( handle<>( borrowed( PyModule_GetDict( 
> main_module.get() ) ) );
> 
> gives me an acces violation whet it is executed. Of course, before this 
> line is executed first I initialize the pyton interpreter with Py_Initialize() 
> and then I add the main module (if it isn't added yet) using the line (as 
> shown in documentation)
> 
> handle<> main_module(borrowed( 
> PyImport_AddModule("__main__") )); 
> 
> So, what have I forgot to do? How can I avoid the ACCES VIOLATION 
> given by the execution of the "dict main_namespace(..)" statement?

I just tried the following out on MSVC7.1 with Python 2.3 and a Boost 
from CVS, though I think it's a few weeks old now. (Better do a CVS 
update. :)) It worked for me, so I assume you are using a different 
compiler and/or different versions of Python and Boost.Python. Some more 
information would help...

#include <iostream>
#include <boost/python.hpp>

using namespace boost::python;

int main()
{
     Py_Initialize();
     handle<> main_module(borrowed( PyImport_AddModule("__main__") ));
     dict main_namespace(handle<>(borrowed(
         PyModule_GetDict(main_module.get()) )));
     handle<>( PyRun_String("result = 5 ** 2", Py_file_input,
         main_namespace.ptr(), main_namespace.ptr()) );
     int five_squared = extract<int>( main_namespace["result"] );
     std::cout << "5^2 = " << five_squared << "\n";
     Py_Finalize();
}

Regards,
Dirk Gerrits






More information about the Cplusplus-sig mailing list