44// "as is" without express or implied warranty, and with no claim as
55// to its suitability for any purpose.
66
7- // Seems to be neccessary to suppress an ICE with MSVC
8- #include < boost/mpl/comparison/less.hpp>
9-
107#include " simple_type.hpp"
118#include " complicated.hpp"
129#include < boost/python/converter/wrapper.hpp>
1310#include < boost/python/converter/unwrapper.hpp>
1411#include < boost/python/detail/config.hpp>
1512#include < boost/python/convert.hpp>
13+ #include < boost/python/module.hpp>
1614#include < boost/python/object/value_holder.hpp>
1715#include < boost/python/object/class.hpp>
1816#include < boost/python/converter/class.hpp>
@@ -53,17 +51,10 @@ PyTypeObject NoddyType = {
5351};
5452
5553// Create a Noddy containing 42
56- extern " C" PyObject*
57- new_noddy (PyObject* self, PyObject* args)
54+ PyObject* new_noddy ()
5855{
59- NoddyObject* noddy;
60-
61- if (!PyArg_ParseTuple (args," :new_noddy" ))
62- return NULL ;
63-
64- noddy = PyObject_New (NoddyObject, &NoddyType);
56+ NoddyObject* noddy = PyObject_New (NoddyObject, &NoddyType);
6557 noddy->x = 42 ;
66-
6758 return (PyObject*)noddy;
6859}
6960
@@ -92,27 +83,13 @@ PyTypeObject SimpleType = {
9283};
9384
9485// Create a Simple containing "hello, world"
95- extern " C" PyObject*
96- new_simple (PyObject* self, PyObject* args)
86+ PyObject* new_simple ()
9787{
98- SimpleObject* simple;
99-
100- if (!PyArg_ParseTuple (args," :new_simple" ))
101- return NULL ;
102-
103- simple = PyObject_New (SimpleObject, &SimpleType);
88+ SimpleObject* simple = PyObject_New (SimpleObject, &SimpleType);
10489 simple->x .s = " hello, world" ;
105-
10690 return (PyObject*)simple;
10791}
10892
109- // Initial method table for the module
110- static PyMethodDef methods[] = {
111- { " new_noddy" , new_noddy, METH_VARARGS },
112- { " new_simple" , new_simple, METH_VARARGS },
113- {0 , 0 , 0 , 0 }
114- };
115-
11693//
11794// Declare some wrappers/unwrappers to test the low-level conversion
11895// mechanism. See boost/python/converter/source.hpp,target.hpp for a
@@ -252,7 +229,7 @@ simple const& g(simple const& x)
252229
253230BOOST_PYTHON_MODULE_INIT (m1)
254231{
255- PyObject* m1 = Py_InitModule ( const_cast < char *>( " m1" ), methods );
232+ boost::python:: module m1 ( " m1" );
256233
257234 // Create the converters; they are self-registering/unregistering.
258235 static int_wrapper wrap_int;
@@ -262,63 +239,45 @@ BOOST_PYTHON_MODULE_INIT(m1)
262239 static noddy_int_ref_unwrapper unwrap_int3;
263240 static simple_ref_unwrapper unwrap_simple;
264241 static simple_const_ref_unwrapper unwrap_simple_const_ref;
265- #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
266- // These compilers will need additional converters
267242 static simple_ref_wrapper wrap_simple_ref;
268- #endif
269243
270244 // This unwrapper extracts pointers and references to the "complicated" class.
271245 static boost::python::converter::class_unwrapper<complicated> unwrap_complicated;
272246
273- PyObject* d = PyModule_GetDict (m1);
274- if (d == NULL )
275- return ;
276-
277247 // Insert the extension metaclass object
278- if (PyDict_SetItemString (
279- d, " xclass" , (PyObject *)boost::python::object::class_metatype ()) < 0 )
280- return ;
281-
248+ m1.add (boost::python::objects::class_metatype (), " xclass" );
249+
282250 // Insert the base class for all extension classes
283- if (PyDict_SetItemString (
284- d, " xinst" , (PyObject *)boost::python::object::class_type ()) < 0 )
285- return ;
251+ m1.add (boost::python::objects::class_type (), " xinst" );
286252
253+ m1.def (new_noddy, " new_noddy" );
254+ m1.def (new_simple, " new_simple" );
255+
287256 // Expose f()
288- if (PyDict_SetItemString (
289- d, " f" , boost::python::make_function (f)) < 0 )
290- return ;
257+ m1.def (f, " f" );
291258
292259 // Expose g()
293- if (PyDict_SetItemString (
294- d, " g" , boost::python::make_function (g)) < 0 )
295- return ;
260+ m1.def (g, " g" );
296261
297262 // Expose complicated's get_n() member function. See newtest.py
298263 // for how it's used to build an extension class.
299- if (PyDict_SetItemString (
300- d, " get_n" , boost::python::make_function (&complicated::get_n)) < 0 )
301- return ;
264+ m1.def (&complicated::get_n, " get_n" );
302265
303266 // Expose complicated::complicated(simple const&, int) as init1
304- if (PyDict_SetItemString (
305- d, " init1"
306- , boost::python::make_constructor<
307- complicated
308- , boost::mpl::type_list<simple const &,int >
309- , boost::python::object::value_holder_generator>()
310- ) < 0 )
311- return ;
312-
313- // Expose complicated::complicated(simple const&) as init2
314- if (PyDict_SetItemString (
315- d, " init2"
316- , boost::python::make_constructor<
317- complicated
318- , boost::mpl::type_list<simple const &>
319- , boost::python::object::value_holder_generator>()
320- ) < 0 )
321- return ;
267+ boost::python::objects::function* init = boost::python::make_constructor<
268+ complicated
269+ , boost::mpl::type_list<simple const &,int >
270+ , boost::python::objects::value_holder_generator>();
271+
272+ boost::python::ref manager (init);
273+
274+ init->add_overload (
275+ boost::python::make_constructor<
276+ complicated
277+ , boost::mpl::type_list<simple const &>
278+ , boost::python::objects::value_holder_generator>());
279+
280+ m1.add (manager, " init1" );
322281}
323282
324283#include " module_tail.cpp"
0 commit comments