mirror of
https://github.com/boostorg/python.git
synced 2026-01-19 16:32:16 +00:00
NULL shared_ptr conversions, more tests for custom to-python shared_ptr registrations
[SVN r18263]
This commit is contained in:
@@ -6,9 +6,11 @@
|
||||
#ifndef MAKE_INSTANCE_DWA200296_HPP
|
||||
# define MAKE_INSTANCE_DWA200296_HPP
|
||||
|
||||
# include <boost/python/detail/wrap_python.hpp>
|
||||
# include <boost/python/object/instance.hpp>
|
||||
# include <boost/python/converter/registered.hpp>
|
||||
# include <boost/python/detail/decref_guard.hpp>
|
||||
# include <boost/python/detail/none.hpp>
|
||||
|
||||
namespace boost { namespace python { namespace objects {
|
||||
|
||||
@@ -24,6 +26,9 @@ struct make_instance_impl
|
||||
|
||||
PyTypeObject* type = Derived::get_class_object(x);
|
||||
|
||||
if (type == 0)
|
||||
return python::detail::none();
|
||||
|
||||
PyObject* raw_result = type->tp_alloc(
|
||||
type, objects::additional_instance_size<Holder>::value);
|
||||
|
||||
|
||||
@@ -34,6 +34,9 @@ struct make_ptr_instance
|
||||
template <class U>
|
||||
static inline PyTypeObject* get_class_object_impl(U const volatile* p)
|
||||
{
|
||||
if (p == 0)
|
||||
return 0;
|
||||
|
||||
PyTypeObject* derived = get_derived_class_object(is_polymorphic<U>::type(), p);
|
||||
if (derived)
|
||||
return derived;
|
||||
|
||||
@@ -95,8 +95,53 @@ shared_ptr<Y> factory(int n)
|
||||
static int stored_v() { return functions<Z>::get()->v(); }
|
||||
static shared_ptr<Z> stored_z() { return functions<Z>::get(); }
|
||||
|
||||
// regressions from Nicodemus
|
||||
struct A
|
||||
{
|
||||
virtual int f() = 0;
|
||||
static int call_f(shared_ptr<A>& a) { return a->f(); }
|
||||
};
|
||||
|
||||
struct B: A
|
||||
{
|
||||
int f() { return 1; }
|
||||
};
|
||||
|
||||
boost::shared_ptr<A> New(bool make)
|
||||
{
|
||||
return boost::shared_ptr<A>( make ? new B() : 0 );
|
||||
}
|
||||
|
||||
struct A_Wrapper: A
|
||||
{
|
||||
A_Wrapper(PyObject* self_):
|
||||
A(), self(self_) {}
|
||||
|
||||
int f() {
|
||||
return call_method< int >(self, "f");
|
||||
}
|
||||
|
||||
PyObject* self;
|
||||
};
|
||||
|
||||
// ------
|
||||
|
||||
BOOST_PYTHON_MODULE(shared_ptr_ext)
|
||||
{
|
||||
class_<A, boost::shared_ptr<A_Wrapper>, boost::noncopyable>("A")
|
||||
.def("call_f", &A::call_f)
|
||||
.staticmethod("call_f")
|
||||
;
|
||||
|
||||
// This is the ugliness required to register a to-python converter
|
||||
// for shared_ptr<A>.
|
||||
objects::class_value_wrapper<
|
||||
shared_ptr<A>
|
||||
, objects::make_ptr_instance<A, objects::pointer_holder<shared_ptr<A>,A> >
|
||||
>();
|
||||
|
||||
def("New", &New);
|
||||
|
||||
class_<X, boost::noncopyable>("X", init<int>())
|
||||
.def("value", &X::value)
|
||||
;
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
'''
|
||||
>>> from shared_ptr_ext import *
|
||||
|
||||
>>> a = New(1)
|
||||
>>> A.call_f(a)
|
||||
1
|
||||
>>> New(0)
|
||||
|
||||
>>> type(factory(3))
|
||||
<class 'shared_ptr_ext.Y'>
|
||||
>>> type(factory(42))
|
||||
|
||||
Reference in New Issue
Block a user