GeezerVison is showing github.com — Added call policies to def(init<...>) and added tests to see that the… · boostorg/python@acbb5be
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: include/boost/python/class.hpp +15-2Lines changed: 15 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -168,9 +168,22 @@ class class_ : public objects::class_base | 168 | 168 | } | 169 | 169 |
| 170 | 170 | template <BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, class T)> | 171 | | - self& def(init<BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, T)> const& i, char const* doc = 0) | | 171 | + self& def(init<BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, T)> const& i) | 172 | 172 | { | 173 | | - define_init(*this, i, doc); | | 173 | + define_init(*this, i, default_call_policies(), 0); | | 174 | + return *this; | | 175 | + } | | 176 | + | | 177 | + template <class CallPolicyOrDoc, BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, class T)> | | 178 | + self& def( | | 179 | + init<BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, T)> const& i, | | 180 | + CallPolicyOrDoc const& policy_or_doc, | | 181 | + char const* doc = 0) | | 182 | + { | | 183 | + typedef detail::def_helper<CallPolicyOrDoc> helper; | | 184 | + define_init(*this, i, | | 185 | + helper::get_policy(policy_or_doc), | | 186 | + helper::get_doc(policy_or_doc, doc)); | 174 | 187 | return *this; | 175 | 188 | } | 176 | 189 |
| |
Collapse file Copy file name to clipboardExpand all lines: include/boost/python/init.hpp +10-10Lines changed: 10 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -352,12 +352,12 @@ namespace detail { | 352 | 352 | template <int N> | 353 | 353 | struct define_class_init_helper { | 354 | 354 |
| 355 | | - template <class ClassT, class ArgsT> | 356 | | - static void apply(ClassT& cl, ArgsT const& args, char const* doc) | | 355 | + template <class ClassT, class CallPoliciesT, class ArgsT> | | 356 | + static void apply(ClassT& cl, CallPoliciesT const& policies, ArgsT const& args, char const* doc) | 357 | 357 | { | 358 | | - cl.def_init(args, default_call_policies(), doc); | | 358 | + cl.def_init(args, policies, doc); | 359 | 359 | typename boost::mpl::pop_back<ArgsT>::sequence next; | 360 | | - define_class_init_helper<N-1>::apply(cl, next, doc); | | 360 | + define_class_init_helper<N-1>::apply(cl, policies, next, doc); | 361 | 361 | } | 362 | 362 | }; | 363 | 363 |
| @@ -374,10 +374,10 @@ namespace detail { | 374 | 374 | template <> | 375 | 375 | struct define_class_init_helper<0> { | 376 | 376 |
| 377 | | - template <class ClassT, class ArgsT> | 378 | | - static void apply(ClassT& cl, ArgsT const& args, char const* doc) | | 377 | + template <class ClassT, class CallPoliciesT, class ArgsT> | | 378 | + static void apply(ClassT& cl, CallPoliciesT const& policies, ArgsT const& args, char const* doc) | 379 | 379 | { | 380 | | - cl.def_init(args, default_call_policies(), doc); | | 380 | + cl.def_init(args, policies, doc); | 381 | 381 | } | 382 | 382 | }; | 383 | 383 | } | @@ -403,13 +403,13 @@ namespace detail { | 403 | 403 | // __init__(int) | 404 | 404 | // | 405 | 405 | /////////////////////////////////////////////////////////////////////////////// | 406 | | -template <class ClassT, class InitT> | | 406 | +template <class ClassT, class CallPoliciesT, class InitT> | 407 | 407 | void | 408 | | -define_init(ClassT& cl, InitT const& i, char const* doc) | | 408 | +define_init(ClassT& cl, InitT const& i, CallPoliciesT const& policies, char const* doc) | 409 | 409 | { | 410 | 410 | enum { n_defaults_plus_1 = InitT::n_defaults + 1 }; | 411 | 411 | typedef typename InitT::sequence args_t; | 412 | | - detail::define_class_init_helper<n_defaults_plus_1>::apply(cl, args_t(), doc); | | 412 | + detail::define_class_init_helper<n_defaults_plus_1>::apply(cl, policies, args_t(), doc); | 413 | 413 | } | 414 | 414 |
| 415 | 415 | }} // namespace boost::python | |
Collapse file Copy file name to clipboardExpand all lines: test/defaults.cpp +33-8Lines changed: 33 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | #include <boost/python/tuple.hpp> | 11 | 11 | #include <boost/python/list.hpp> | 12 | 12 | #include <boost/python/module.hpp> | | 13 | +#include <boost/python/return_internal_reference.hpp> | 13 | 14 |
| 14 | 15 | #if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245 | 15 | 16 | # include <iostream> // works around a KCC intermediate code generation bug | @@ -69,6 +70,20 @@ BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs, foo, 1, 4) | 69 | 70 | // Overloaded member functions with default arguments | 70 | 71 | // | 71 | 72 | /////////////////////////////////////////////////////////////////////////////// | | 73 | +struct Y { | | 74 | + | | 75 | + Y() {} | | 76 | + | | 77 | + object | | 78 | + get_state() const | | 79 | + { | | 80 | + return format % make_tuple(a, b, c, d); | | 81 | + } | | 82 | + | | 83 | + int a; char b; std::string c; double d; | | 84 | +}; | | 85 | + | | 86 | + | 72 | 87 | struct X { | 73 | 88 |
| 74 | 89 | X() {} | @@ -83,11 +98,15 @@ struct X { | 83 | 98 | return format % make_tuple(a, b, c, d); | 84 | 99 | } | 85 | 100 |
| 86 | | - object | 87 | | - bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0) const | | 101 | + Y const& | | 102 | + bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0) | 88 | 103 | { | 89 | | - // tests zero arg member function | 90 | | - return format % make_tuple(a, b, c, d); | | 104 | + // tests zero arg member function and return_internal_reference policy | | 105 | + y.a = a; | | 106 | + y.b = b; | | 107 | + y.c = c; | | 108 | + y.d = d; | | 109 | + return y; | 91 | 110 | } | 92 | 111 |
| 93 | 112 | object | @@ -114,19 +133,20 @@ struct X { | 114 | 133 | return state; | 115 | 134 | } | 116 | 135 |
| | 136 | + Y y; | 117 | 137 | object state; | 118 | 138 | }; | 119 | 139 |
| 120 | 140 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs, bar, 1, 4) | 121 | | -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar2_stubs, bar2, 0, 4) // tests zero arg member function | | 141 | +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs2, bar2, 0, 4) | 122 | 142 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_2_stubs, foo, 1, 2) | 123 | 143 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3) | 124 | 144 |
| 125 | 145 | /////////////////////////////////////////////////////////////////////////////// | 126 | 146 |
| 127 | 147 | BOOST_PYTHON_MODULE_INIT(defaults_ext) | 128 | 148 | { | 129 | | - def("foo", foo, foo_stubs(), default_call_policies()); | | 149 | + def("foo", foo, foo_stubs()); | 130 | 150 | def("bar", (object(*)(int, char, std::string, double))0, bar_stubs()); | 131 | 151 |
| 132 | 152 | // Show that this works with the old obsolete module version of def(). | @@ -135,6 +155,10 @@ BOOST_PYTHON_MODULE_INIT(defaults_ext) | 135 | 155 | .def("barfoo", (object(*)(int, char, std::string, double))0, bar_stubs()) | 136 | 156 | ; | 137 | 157 |
| | 158 | + class_<Y>("Y", no_init) | | 159 | + .def("get_state", &Y::get_state) | | 160 | + ; | | 161 | + | 138 | 162 | class_<X>("X") | 139 | 163 |
| 140 | 164 | # if (!defined(BOOST_INTEL_CXX_VERSION) || BOOST_INTEL_CXX_VERSION > 600) | @@ -145,9 +169,10 @@ BOOST_PYTHON_MODULE_INIT(defaults_ext) | 145 | 169 | .def_init(args<int, char, std::string>()) | 146 | 170 | .def_init(args<int, char, std::string, double>()) | 147 | 171 | # endif | 148 | | - .def("get_state", &X::get_state) | | 172 | + .def("get_state", &X::get_state) | 149 | 173 | .def("bar", &X::bar, X_bar_stubs()) | 150 | | - .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs(), default_call_policies()) | | 174 | + .def("bar2", &X::bar2, X_bar_stubs2(), return_internal_reference<>()) | | 175 | + .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs()) | 151 | 176 | .def("foo", (object(X::*)(int, bool) const)0, X_foo_2_stubs()) | 152 | 177 | .def("foo", (object(X::*)(list, list, bool) const)0, X_foo_3_stubs()) | 153 | 178 | ; | |
Collapse file Copy file name to clipboardExpand all lines: test/defaults.py +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 56 | 56 | >>> x = X(1, 'X', "Phoenix", 3.65) | 57 | 57 | >>> x.get_state() | 58 | 58 | 'int(1); char(X); string(Phoenix); double(3.65); ' | 59 | | -
| | 59 | +>>> x.bar2().get_state() | | 60 | +'int(0); char(D); string(default); double(0.0); ' | | 61 | +>>> x.bar2(1).get_state() | | 62 | +'int(1); char(D); string(default); double(0.0); ' | | 63 | +>>> x.bar2(1, 'K').get_state() | | 64 | +'int(1); char(K); string(default); double(0.0); ' | | 65 | +>>> x.bar2(1, 'K', "Kim").get_state() | | 66 | +'int(1); char(K); string(Kim); double(0.0); ' | | 67 | +>>> x.bar2(1, 'K', "Kim", 9.9).get_state() | | 68 | +'int(1); char(K); string(Kim); double(9.9); ' | 60 | 69 |
| 61 | 70 | """ | 62 | 71 | def run(args = None): | |
|