Skip to content

added 'construct_custodian_for' and test #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/boost/python/init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ class init : public init_base<init<BOOST_PYTHON_OVERLOAD_ARGS> >
policies, this->doc_string(), this->keywords());
}

template <std::size_t ward>
init_with_call_policies<with_custodian_and_ward_postcall<1, ward+1>, self_t>
operator[](construct_custodian_for<ward> const&) const
{
typedef with_custodian_and_ward_postcall<1, ward+1> init_policy;
return init_with_call_policies<init_policy, self_t>(
init_policy(), this->doc_string(), this->keywords());
}

typedef detail::type_list<BOOST_PYTHON_OVERLOAD_ARGS> signature_;

typedef detail::is_optional<
Expand Down
43 changes: 43 additions & 0 deletions include/boost/python/with_custodian_and_ward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,49 @@ struct with_custodian_and_ward_postcall : BasePolicy_
}
};

template <std::size_t ward>
struct construct_custodian_for : default_call_policies
{
BOOST_STATIC_ASSERT(ward > 0);

template <class ArgumentPackage>
static PyObject* postcall(ArgumentPackage const& args_, PyObject* result)
{
std::size_t arity_ = detail::arity(args_);
#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
if (ward > arity_ )
#else
// check if ward exceeds the arity
// (this weird formulation avoids "always false" warnings
// for arity_ = 0)
if ( (std::max<std::size_t>)(0, ward) > arity_ )
#endif
{
PyErr_SetString(
PyExc_IndexError
, "boost::python::construct_custodian_for: argument index out of range"
);
return 0;
}

PyObject* patient = detail::get_prev<ward>::execute(args_, result);
PyObject* nurse = detail::get_prev<1>::execute(args_.base);

if (nurse == 0) return 0;

result = default_call_policies::postcall(args_, result);
if (result == 0)
return 0;

if (python::objects::make_nurse_and_patient(nurse, patient) == 0)
{
Py_XDECREF(result);
return 0;
}
return result;
}
};


}} // namespace boost::python

Expand Down
18 changes: 18 additions & 0 deletions test/test_pointer_adoption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <boost/python/manage_new_object.hpp>
#include <boost/python/return_internal_reference.hpp>
#include <boost/python/class.hpp>
#include <boost/python/make_constructor.hpp>

using namespace boost::python;

Expand Down Expand Up @@ -86,6 +87,11 @@ A* as_A(Base* b)
return dynamic_cast<A*>(b);
}

B* create_B(A* a)
{
return new B(a);
}

BOOST_PYTHON_MODULE(test_pointer_adoption_ext)
{
def("num_a_instances", num_a_instances);
Expand All @@ -102,6 +108,8 @@ BOOST_PYTHON_MODULE(test_pointer_adoption_ext)
class_<A, bases<Base> >("A", no_init)
.def("content", &A::content)
.def("get_inner", &A::get_inner, return_internal_reference<>())
.def("create_B", &create_B, with_custodian_and_ward_postcall<0,1,
return_value_policy<manage_new_object> >())
;

class_<inner>("inner", no_init)
Expand All @@ -120,6 +128,16 @@ BOOST_PYTHON_MODULE(test_pointer_adoption_ext)

.def("a_content", &B::a_content)
;

class_<B>("B1")
.def(init<A*>()[construct_custodian_for<1>()])
.def("a_content", &B::a_content)
;

class_<B>("B2")
.def("__init__", make_constructor(&create_B, construct_custodian_for<1>()))
.def("a_content", &B::a_content)
;
}

#include "module_tail.cpp"
62 changes: 61 additions & 1 deletion test/test_pointer_adoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
"""
>>> from test_pointer_adoption_ext import *
>>> import sys

>>> num_a_instances()
0

>>> a = create('dynamically allocated')
>>> num_a_instances()
1

>>> sys.getrefcount(a)
2
>>> a.content()
'dynamically allocated'

Expand Down Expand Up @@ -55,13 +57,71 @@
Test call policies for constructors here

>>> a = create('second a')
>>> a.content()
'second a'
>>> num_a_instances()
1
>>> b = a.create_B()
>>> num_a_instances()
1
>>> sys.getrefcount(a)
3
>>> b.a_content()
'second a'
>>> b = None
>>> num_a_instances()
1
>>> sys.getrefcount(a)
2

>>> b = B(a)
>>> num_a_instances()
1
>>> sys.getrefcount(a)
3
>>> a.content()
'second a'
>>> b.a_content()
'second a'
>>> b = None
>>> num_a_instances()
1
>>> sys.getrefcount(a)
2

>>> b = B1(a)
>>> num_a_instances()
1
>>> sys.getrefcount(a)
3
>>> a.content()
'second a'
>>> b.a_content()
'second a'
>>> b = None
>>> num_a_instances()
1
>>> sys.getrefcount(a)
2

>>> b = B2(a)
>>> num_a_instances()
1
>>> sys.getrefcount(a)
3
>>> a.content()
'second a'
>>> b.a_content()
'second a'
>>> b = None
>>> num_a_instances()
1
>>> sys.getrefcount(a)
2

>>> b = B(a)
>>> num_a_instances()
1

>>> del a
>>> num_a_instances()
Expand Down