[C++-sig] Multiple registration safety and unit testing

Stefan Seefeld stefan at seefeld.name
Thu Apr 21 09:46:26 EDT 2016


Hi David,

I'm just reviewing Boost.Python PRs, and looking at
https://github.com/boostorg/python/pull/55 I was reminded of this little
exchange.
Sorry for having dropped the ball here...

On 01.02.2016 17:36, David Sankel wrote:
> On Sat, Jan 30, 2016 at 7:30 AM, Stefan Seefeld <stefan at seefeld.name
> <mailto:stefan at seefeld.name>> wrote:
>
>
>     Yes. Can you describe your use-case ? And what do you mean exactly by
>     "semantically different" ? :-)
>
>
> In one '.cpp' file I have something like:
>
>
>     void addFooAssets()
>     {
>       boost::python::class_< Foo > ( /* etc. */ )
>         .def( /* etc */ );
>     }
>
>     // testing code follows.
>
>     BOOST_PYTHON_MODULE( fooTest )
>     {
>       addFooAssets();
>     }
>
>     BOOST_AUTO_TEST_CASE( fooAssets ) {
>       // import 'fooTest' and check that all is good.
>     }
>
> In another '.cpp' file, I have something like:
>
>     void initBarPackage()
>     {
>       boost::python::object barPackage = boost::python::scope();
>       barPackage.attr( "__path__" ) = "bar";
>
>       boost::python::object barFooPackage(
>         boost::python::handle<>(
>     boost::python::borrowed(::PyImport_AddModule( "bar.foo" ) ) ) );
>       barPackage.attr( "foo" ) = barFooPackage;
>
>       {
>         const boost::python::scope fooScope( barFooPackage );
>         addFooAssets();
>       }
>     }
>
>     // testing code follows
>

What do you attempt to do in the above ? Why not simply create "bar" via
BOOST_PYTHON_MODULE(bar) ? For example:

BOOST_PYTHON_MODULE(bar)
{
  object barPackage = scope();
  object barFooPackage = import("foo");
  barPackage.attr( "foo" ) = barFooPackage;
}

should have the effect you are seeking, if I understand correctly. There
is no need to invoke "addFooAssets()" twice.

        Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list