Boost python library
David Abrahams
david.abrahams at rcn.com
Wed Jan 9 15:36:54 CET 2002
Hans,
Firstly, unless I'm missing something, the code you sent me couldn't
possibly compile. The only declaration of point::init is commented out, yet
you reference it in the geotest module's initialization function.
Secondly, you are treading in the hazy area of shared libraries (not covered
by the C++ standard) and the even hazier area of VC6, so all bets are off
;-)
Third, your point's wrapper for the copy constructor reads:
point_class.def(python::constructor<const point>());
I think you want this to be a reference------^^^^^^^^^^^
Fourth, however, I'm pretty sure none of those are your problem. Any given
module may only be exposed to one of the following:
class_builder<T,U>
import_converters<T>
for any given T. Each one generates a set of conversion functions _when_
_instantiated_ that is appropriate to whether T is exported from the current
module or not. If you declare both in the same translation unit, duplicate
to_python/from_python functions with the same signature will be generated.
HTH,
Dave
P.S. The rewrite will not have this limitation, as conversions will be done
in a much more cross-module-friendly manner. In fact, there will be no need
to declare import converters.
----- Original Message -----
From: "Rosbach, Hans A." <Hans.A.Rosbach at dnv.com>
To: "'David Abrahams'" <david.abrahams at rcn.com>; "Rosbach, Hans A."
<Hans.A.Rosbach at dnv.com>
Cc: "Xavier Defrang" <xavier at perceval.net>; "Ullrich Koethe"
<koethe at informatik.uni-hamburg.de>; "Ralf W. Grosse-Kunstleve"
<rwgk at cci.lbl.gov>; "Niklas Blomberg" <niklas_blomberg at telia.com>; "Martin
Casado" <casado2 at llnl.gov>; "Barry Scott" <barry at scottb.demon.co.uk>;
"Arnaldur Gylfason" <arnaldur at decode.is>; "Anton Gluck"
<gluc at midway.uchicago.edu>; "Alex Martelli" <aleaxit at yahoo.com>; "Martin
Casado" <mcasado at gmx.net>; "scott snyder" < snyder at fnal.gov>
Sent: Wednesday, January 09, 2002 8:46 AM
Subject: RE: Boost python library
> Thanks for your reply.
>
> > Hi Hans,
> >
> > ----- Original Message -----
> > From: "Rosbach, Hans A." <Hans.A.Rosbach at dnv.com>
> >
> >
> > > I'm making a test using the boost python library in order to get
> > > an understanding of how to use it ahead of actual use in real
> > > code. The test is done to enable us to compare different methods to
> > > make our applications scriptable.
> > >
> > > We have some big applications, and a COM based scripting using
> > > Jscript/Javascript/Ecmascript as language. This code we want to
replace
> > > with something better, and boost python is one of the alternatives.
> > >
> > > We will both use embedding of python and extending with our own
> > > user visible classes.
> > >
> > > Our C++ classes will be thin, just a wrapper for the real data within
> > > the application. In the test I found that using a static inline member
> > > function,
> > > I could place the boost python code in the same header file as the
rest
> of
> > > the
> > > class definition. I.e. one place for both the C++ and python
definition
> > of
> > > the class.
> > > Ideal for making the code easy to maintain.
> >
> > It sounds like a great application for Boost.Python, and the plans we
have
> > for the near future will make it even better.
> >
> > > However, when I wanted to have my classes in different modules, I was
> > unable
> > > to do so with the inline init functions. I got an error message from
> the
> > > MSVC6 compiler - ambiguous call to overloaded function.
> >
> > I think you'll need to show me a lot more detail before I have even a
clue
> > of what's going wrong.
> >
>
> Ok, let me show you my test example. The relevant part consists of
> two Python modules, with one wrapped class in each.
>
> The classes: point and line -
>
> #ifndef POINT_H
> #define POINT_H
>
> #include <../application/application_config.h>
>
> #include <iostream>
> #include <string>
>
> #include <boost/python/class_builder.hpp>
> #include <boost/python/cross_module.hpp>
>
>
>
> namespace python = boost::python;
>
> class APPLICATION_IMPORT_EXPORT point
> {
> public:
> point(double x,double y,double z);
> point(const point& p);
> ~point();
> virtual double x() const;
> virtual double y() const;
> virtual double z() const;
>
> virtual void X(double x);
>
> /* static void init(python::class_builder<point>& point_class,
> python::module_builder& module);
> */
> private:
> double m_x;
> double m_y;
> double m_z;
>
> };
> /*
> inline void point::init(python::class_builder<point>& point_class,
> python::module_builder& module)
> {
> point_class.def(python::constructor<double,double,double>());
> point_class.def(python::constructor<const point>());
> point_class.def(&point::x,"x");
> point_class.def(&point::y,"y");
> point_class.def(&point::z,"z");
> point_class.def(&point::x,"__getattr__X__");
> point_class.def(&point::X,"__setattr__X__");
> }
>
> */
> #endif // POINT_H
>
>
> #ifndef LINE_H
> #define LINE_H
>
> #include <../application/application_config.h>
> #include <../application/point.h>
>
> #include <boost/python/class_builder.hpp>
> #include <boost/python/cross_module.hpp>
>
> namespace python = boost::python;
>
> class APPLICATION_IMPORT_EXPORT line
> {
> public:
> line(const point& p1,const point& p2);
> virtual ~line();
> virtual point p1() const;
> virtual point p2() const;
>
> static void init(python::class_builder<line>& line_class,
> python::module_builder& module);
>
> protected:
>
> private:
> point m_p1;
> point m_p2;
>
> };
>
> inline void line::init(python::class_builder<line>& line_class,
> python::module_builder& module)
> {
> line_class.def(python::constructor<point,point>());
> line_class.def(&line::p1,"p1");
> line_class.def(&line::p2,"p2");
> }
>
>
> #endif // LINE_H
>
>
> You can see the inline functions. When these are called making a
> single module, everything works fine. Using
>
> #include <iostream>
> #include <string>
>
> #include <boost/python/class_builder.hpp>
>
> namespace python = boost::python;
>
> #include <../application/point.h>
> #include <../application/line.h>
> #include <../application/pypseudofile.h>
>
>
>
> BOOST_PYTHON_MODULE_INIT(geotest)
> {
> try
> {
> // Create an object representing this extension module.
> python::module_builder this_module("geotest");
>
> python::class_builder<point> point_class(this_module,"point");
> python::export_converters(point_class);
> /*
> point_class.def(python::constructor<double,double,double>());
> point_class.def(python::constructor<const point>());
> point_class.def(&point::x,"x");
> point_class.def(&point::y,"y");
> point_class.def(&point::z,"z");
> point_class.def(&point::x,"__getattr__X__");
> point_class.def(&point::X,"__setattr__X__");
> */
> point::init(point_class,this_module);
> }
> catch(...)
> {
> python::handle_exception(); // Deal with the exception for Python
> }
> }
>
> and
>
> #include <iostream>
> #include <string>
>
> #include <boost/python/class_builder.hpp>
>
> namespace python = boost::python;
>
> #include <../application/point.h>
> #include <../application/line.h>
> #include <../application/pypseudofile.h>
>
>
>
> BOOST_PYTHON_MODULE_INIT(geotest2)
> {
> try
> {
> python::module_builder this_module("geotest2");
> python::import_converters<point>
> point_class_converters("geotest","point");
> python::class_builder<line> line_class(this_module,"line");
>
> line_class.def(python::constructor<point,point>());
> line_class.def(&line::p1,"p1");
> line_class.def(&line::p2,"p2");
>
> // line::init(line_class,this_module);
>
> }
> catch(...)
> {
> python::handle_exception(); // Deal with the exception for Python
> }
> }
>
> gives me these messages from MSVC6
>
>
> geotest2.cpp
> e:\boost_1_25_1\boost/python/detail/init_function.hpp(265) : error
> C2668: 'from_python' : ambiguous call to overloaded function
> e:\boost_1_25_1\boost/python/detail/init_function.hpp(260) :
> while compiling class-template member function 'class
> boost::python::detail::instance_holder_base *__thiscall
> boost::python::detail::init1<class boost::python::detail::instance_val
> ue_holder<class point,class boost::python::detail::held_instance<class
> point> >,class point const &>::create_holder(class
> boost::python::detail::extension_instance *,struct _object *,struct
_object
> *) const'
> e:\boost_1_25_1\boost/python/detail/init_function.hpp(266) : error
> C2661: 'instance_value_holder<class point,class
> boost::python::detail::held_instance<class point>
> >::instance_value_holder<class point,class
> boost::python::detail::held_instance<clas
> s point> >' : no overloaded function takes 2 parameters
> e:\boost_1_25_1\boost/python/detail/init_function.hpp(260) :
> while compiling class-template member function 'class
> boost::python::detail::instance_holder_base *__thiscall
> boost::python::detail::init1<class boost::python::detail::instance_val
> ue_holder<class point,class boost::python::detail::held_instance<class
> point> >,class point const &>::create_holder(class
> boost::python::detail::extension_instance *,struct _object *,struct
_object
> *) const'
>
>
>
>
> I hope this is both shows my problem and my wish. I can make it
> compile fine by using the explicite code instead of the calls to the
> inline init functions. It is thus possible to create a working
> example, but not as clean as I would like it. I would prefer the
> definitions for python and for C++ to reside in the same file.
>
> [cuts]
> >
> > > I have used boost 1.25.1.
> >
> > The latest CVS state moves all of Boost.Python into a shared library,
> which
> > should produces /significantly/ smaller executable sizes for any
> application
> > which uses multiple extension modules. I suggest you try that out. Of
> > course, I realize that has nothing to do with your questions.
> >
> > Regards,
> > Dave
>
> We don't use CVS, thus I have no experience with it, although I would
> imagine that my favorite editor probably would make it easy if I got
> around to it. Any pointers to an easy introduction to setting up CVS
> for cygwin and/or emacs on ms nt4?
>
> Regards,
> Hans
>
>
>
> **********************************************************************
> Neither the confidentiality nor the integrity of this message
> can be guaranteed following transmission on the Internet.
> This message has been swept by MAILsweeper at DNV for
> the presence of computer viruses.
> **********************************************************************
More information about the Cplusplus-sig
mailing list