From 472b18881b54a1fb84abd1fcf0485fec58d6deaa Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 23 Dec 2008 07:55:33 +0000 Subject: [PATCH] Boost.Python enable_shared_from_this patches by Nicolas Lelong and Chad Austin: http://mail.python.org/pipermail/cplusplus-sig/2008-December/014103.html http://mail.python.org/pipermail/cplusplus-sig/2008-February/013003.html [SVN r50368] --- .../converter/shared_ptr_from_python.hpp | 10 ++-- test/Jamfile.v2 | 1 + test/enable_shared_from_this.cpp | 48 +++++++++++++++++++ test/enable_shared_from_this.py | 26 ++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 test/enable_shared_from_this.cpp create mode 100755 test/enable_shared_from_this.py diff --git a/include/boost/python/converter/shared_ptr_from_python.hpp b/include/boost/python/converter/shared_ptr_from_python.hpp index 6b49c469..c0910776 100644 --- a/include/boost/python/converter/shared_ptr_from_python.hpp +++ b/include/boost/python/converter/shared_ptr_from_python.hpp @@ -45,10 +45,14 @@ struct shared_ptr_from_python if (data->convertible == source) new (storage) shared_ptr(); else + { + boost::shared_ptr hold_convertible_ref_count( + (void*)0, shared_ptr_deleter(handle<>(borrowed(source))) ); + // use aliasing constructor new (storage) shared_ptr( - static_cast(data->convertible), - shared_ptr_deleter(handle<>(borrowed(source))) - ); + hold_convertible_ref_count, + static_cast(data->convertible)); + } data->convertible = storage; } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 4e1479b4..5f66be63 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -75,6 +75,7 @@ bpl-test crossmod_exception [ bpl-test return_arg ] [ bpl-test staticmethod ] [ bpl-test shared_ptr ] +[ bpl-test enable_shared_from_this ] [ bpl-test andreas_beyer ] [ bpl-test polymorphism ] [ bpl-test polymorphism2 ] diff --git a/test/enable_shared_from_this.cpp b/test/enable_shared_from_this.cpp new file mode 100644 index 00000000..c246284a --- /dev/null +++ b/test/enable_shared_from_this.cpp @@ -0,0 +1,48 @@ +// Copyright David Abrahams 2002. +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include +#include +#include +#include "test_class.hpp" + +#include + +using namespace boost::python; +using boost::shared_ptr; + +class Test; +typedef shared_ptr TestPtr; + +class Test : public boost::enable_shared_from_this { +public: + static TestPtr construct() { + return TestPtr(new Test); + } + + void act() { + TestPtr kungFuDeathGrip(shared_from_this()); + } + + void take(TestPtr t) { + } +}; + +BOOST_PYTHON_MODULE(enable_shared_from_this_ext) +{ + class_("Test") + .def("construct", &Test::construct).staticmethod("construct") + .def("act", &Test::act) + .def("take", &Test::take) + ; +} + +#include "module_tail.cpp" + + diff --git a/test/enable_shared_from_this.py b/test/enable_shared_from_this.py new file mode 100755 index 00000000..cca46ec8 --- /dev/null +++ b/test/enable_shared_from_this.py @@ -0,0 +1,26 @@ +# Copyright David Abrahams 2004. Distributed under the Boost +# Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +''' +>>> from enable_shared_from_this_ext import * + +>>> x = Test.construct() +>>> x.take(x) +>>> x.act() +''' + +def run(args = None): + import sys + import doctest + + if args is not None: + sys.argv = args + return doctest.testmod(sys.modules.get(__name__)) + +if __name__ == '__main__': + print "running..." + import sys + status = run()[0] + if (status == 0): print "Done." + sys.exit(status) +