[C++-sig] python namespace

Francois Ostiguy ostiguy at fnal.gov
Thu Nov 21 16:57:31 CET 2002


> Have you taken a close look at the error messages and the
> documentation for scope at
> http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/python/doc/v2/scope.html#scope-spec
> ?
> They tell all.
>
>    python::scope inner_scope1(object(inner_module1));
>
> Hmm, I probably should make the scope constructor explicit.

Thanks, once again. At least I understand the origin of the error message.
My code now compiles, unfortunately, the result is not what I expected.

here is the current code for my toy example:

using std::complex;
namespace python = boost::python;

void f1() {
  std:: cout <<"f1" << std::endl;
}

void f2() {
  std:: cout <<"f2" << std::endl;
}


static char filename[] = "outer.so";
BOOST_PYTHON_MODULE(outer)
{

  python::handle<> inner_module1( PyModule_New("outer.inner1"));
  python::object inner_object1(inner_module1);
  inner_object1.attr("__file__") = filename;

  python::scope inner_scope1(inner_object1);
  python::def("f1", &f1);

  python::handle<> inner_module2( PyModule_New("outer.inner2"));
  python::object inner_object2(inner_module2);
  inner_object2.attr("__file__") = filename;

  python::scope inner_scope2(inner_object2);
  python::def("f2", &f2);
}

When I load the module, here is what I get

Python 2.2.2 (#1, Oct 28 2002, 17:04:52)
[GCC 3.1.1] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import outer
>>> dir()
['__builtins__', '__doc__', '__name__', 'outer']
>>> dir(outer)
['__doc__', '__file__', '__name__']
>>>

That is to say, "inner1" and "inner2" are not defined in the "outer"
scope. The documentation for PyModule_New(char* ) and other related
functions is terse and not very helpful in terms of explaining how they
should be used ... I am stumped.


-Francois

----------------------------------------------------------------------------
Dr. Jean-Francois OSTIGUY                              voice: (630) 840-2231
Beam Physics Dept MS220                                  FAX: (630) 840-6039
Fermi National Accelerator Laboratory                email: ostiguy at fnal.gov
Batavia IL 60510-0500                           WWW:www-ap.fnal.gov/~ostiguy






More information about the Cplusplus-sig mailing list