Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61851 - in sandbox/python_extensions: . boost boost/mpl boost/mpl/adapted boost/python boost/python/from_python boost/python/to_python libs libs/python libs/python/test
From: talljimbo_at_[hidden]
Date: 2010-05-07 21:04:05


Author: jbosch
Date: 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
New Revision: 61851
URL: http://svn.boost.org/trac/boost/changeset/61851

Log:
initial import for miscellaneous Boost.Python extension code
Added:
   sandbox/python_extensions/LICENSE_1_0.txt (contents, props changed)
   sandbox/python_extensions/SConstruct (contents, props changed)
   sandbox/python_extensions/boost/
   sandbox/python_extensions/boost/mpl/
   sandbox/python_extensions/boost/mpl/adapted/
   sandbox/python_extensions/boost/mpl/adapted/std_pair.hpp (contents, props changed)
   sandbox/python_extensions/boost/python/
   sandbox/python_extensions/boost/python/from_python/
   sandbox/python_extensions/boost/python/from_python/boost_fusion.hpp (contents, props changed)
   sandbox/python_extensions/boost/python/from_python/container.hpp (contents, props changed)
   sandbox/python_extensions/boost/python/from_python/iterator.hpp (contents, props changed)
   sandbox/python_extensions/boost/python/from_python/std_pair.hpp (contents, props changed)
   sandbox/python_extensions/boost/python/to_python/
   sandbox/python_extensions/boost/python/to_python/boost_fusion.hpp (contents, props changed)
   sandbox/python_extensions/boost/python/to_python/copy_to_list.hpp (contents, props changed)
   sandbox/python_extensions/boost/python/to_python/implicit.hpp (contents, props changed)
   sandbox/python_extensions/boost/python/to_python/std_auto_ptr.hpp (contents, props changed)
   sandbox/python_extensions/boost/python/to_python/std_pair.hpp (contents, props changed)
   sandbox/python_extensions/libs/
   sandbox/python_extensions/libs/python/
   sandbox/python_extensions/libs/python/test/
   sandbox/python_extensions/libs/python/test/SConscript (contents, props changed)
   sandbox/python_extensions/libs/python/test/test_mod.cpp (contents, props changed)
   sandbox/python_extensions/libs/python/test/test_script.py (contents, props changed)

Added: sandbox/python_extensions/LICENSE_1_0.txt
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/LICENSE_1_0.txt 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,23 @@
+Boost Software License - Version 1.0 - August 17th, 2003
+
+Permission is hereby granted, free of charge, to any person or organization
+obtaining a copy of the software and accompanying documentation covered by
+this license (the "Software") to use, reproduce, display, distribute,
+execute, and transmit the Software, and to prepare derivative works of the
+Software, and to permit third-parties to whom the Software is furnished to
+do so, all subject to the following:
+
+The copyright notices in the Software and this entire statement, including
+the above license grant, this restriction and the following disclaimer,
+must be included in all copies of the Software, in whole or in part, and
+all derivative works of the Software, unless such copies or derivative
+works are solely in the form of machine-executable object code generated by
+a source language processor.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.

Added: sandbox/python_extensions/SConstruct
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/SConstruct 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,37 @@
+import distutils.sysconfig
+import re
+import os
+
+def ApplyFlags(env, flags):
+ flags = env.ParseFlags(flags)
+ flags["CCFLAGS"] = [opt for opt in flags["CCFLAGS"] if not opt.startswith("-O")]
+ flags["CFLAGS"] = [opt for opt in flags["CFLAGS"] if not opt.startswith("-O")]
+ debug = ARGUMENTS.get('debug', 0)
+ if int(debug):
+ try:
+ flags["CPPDEFINES"].remove("NDEBUG")
+ except: pass
+ env.MergeFlags(flags)
+
+def ConfigurePython(env):
+ cflags = " ".join(v for v in distutils.sysconfig.get_config_vars("BASECFLAGS","OPT")
+ if v is not None).split()
+ libs = " ".join(v for v in distutils.sysconfig.get_config_vars("BLDLIBRARY","LIBS")
+ if v is not None).split()
+ try: # not valid for C++
+ cflags.remove("-Wstrict-prototypes")
+ except ValueError: pass
+ cflags = [f for f in cflags if not f.startswith("-O")]
+ try:
+ libs.remove("-L.")
+ except ValueError: pass
+ cflags.append("-I%s" % distutils.sysconfig.get_python_inc())
+ ApplyFlags(env, cflags + libs)
+
+env = Environment()
+ConfigurePython(env)
+env.Append(LIBS = "boost_python")
+env.Append(CPPPATH = "#")
+
+Export("env")
+SConscript("libs/python/test/SConscript")

Added: sandbox/python_extensions/boost/mpl/adapted/std_pair.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/mpl/adapted/std_pair.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,81 @@
+// Copyright 2010 Jim Bosch.
+// 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)
+
+#ifndef BOOST_MPL_ADAPTED_STD_PAIR_HPP
+#define BOOST_MPL_ADAPTED_STD_PAIR_HPP
+
+#include <utility>
+#include <boost/mpl/int.hpp>
+#include <boost/mpl/deref.hpp>
+#include <boost/mpl/next_prior.hpp>
+#include <boost/mpl/begin_end.hpp>
+#include <boost/mpl/front.hpp>
+#include <boost/mpl/back.hpp>
+#include <boost/mpl/size.hpp>
+#include <boost/mpl/empty.hpp>
+#include <boost/mpl/iterator_tags.hpp>
+
+namespace boost {
+namespace mpl {
+
+template <typename T1, typename T2, typename I>
+struct std_pair_iterator {
+ typedef bidirectional_iterator_tag category;
+};
+
+template <typename T1, typename T2>
+struct deref< std_pair_iterator< T1, T2, int_<0> > > {
+ typedef T1 type;
+};
+
+template <typename T1, typename T2>
+struct deref< std_pair_iterator< T1, T2, int_<1> > > {
+ typedef T2 type;
+};
+
+template <typename T1, typename T2, typename I>
+struct next< std_pair_iterator<T1,T2,I> > {
+ typedef std_pair_iterator<T1,T2,typename next<I>::type> type;
+};
+
+template <typename T1, typename T2, typename I>
+struct prior< std_pair_iterator<T1,T2,I> > {
+ typedef std_pair_iterator<T1,T2,typename prior<I>::type> type;
+};
+
+template <typename T1, typename T2>
+struct begin< std::pair<T1,T2> > {
+ typedef std_pair_iterator< T1, T2, int_<0> > type;
+};
+
+template <typename T1, typename T2>
+struct end< std::pair<T1,T2> > {
+ typedef std_pair_iterator< T1, T2, int_<2> > type;
+};
+
+template <typename T1, typename T2>
+struct front< std::pair<T1,T2> > {
+ typedef T1 type;
+};
+
+template <typename T1, typename T2>
+struct back< std::pair<T1,T2> > {
+ typedef T2 type;
+};
+
+template <typename T1, typename T2>
+struct size< std::pair<T1,T2> > {
+ typedef int_<2> type;
+};
+
+template <typename T1, typename T2>
+struct empty< std::pair<T1,T2> > {
+ typedef bool_<false> type;
+};
+
+} // namespace boost::mpl
+} // namespace boost
+
+#endif // !BOOST_MPL_ADAPTED_STD_PAIR_HPP

Added: sandbox/python_extensions/boost/python/from_python/boost_fusion.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/python/from_python/boost_fusion.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,104 @@
+// Copyright 2010 Jim Bosch.
+// 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)
+
+#ifndef BOOST_PYTHON_FROM_PYTHON_BOOST_FUSION_HPP
+#define BOOST_PYTHON_FROM_PYTHON_BOOST_FUSION_HPP
+
+#include <boost/python.hpp>
+#include <boost/fusion/include/for_each.hpp>
+#include <boost/fusion/include/mpl.hpp>
+#include <boost/mpl/for_each.hpp>
+
+namespace boost {
+namespace python {
+
+namespace detail {
+
+struct boost_fusion_from_python_failed {};
+
+struct boost_fusion_from_python_check {
+ object iter;
+ list copy;
+
+ template <typename T> void operator()(T const & x) {
+ handle<> item_handle(allow_null(PyIter_Next(iter.ptr())));
+ if (!item_handle) {
+ PyErr_Clear();
+ throw boost_fusion_from_python_failed();
+ }
+ object item(item_handle);
+ extract<T> can_extract(item);
+ if (!can_extract) throw boost_fusion_from_python_failed();
+ copy.append(item);
+ }
+
+ explicit boost_fusion_from_python_check(object const & iter_) :
+ iter(iter_) {}
+};
+
+struct boost_fusion_from_python_set {
+ list sequence;
+ int index;
+
+ template <typename T> void operator()(T & x) const {
+ x = extract<T>(sequence[index]);
+ }
+
+ explicit boost_fusion_from_python_set(object const & sequence_) :
+ sequence(sequence_), index(0) {}
+};
+
+} // namespace detail
+
+template <typename Sequence>
+struct boost_fusion_from_python {
+
+ static void declare() {
+ converter::registry::push_back(
+ &convertible,
+ &construct,
+ type_id< Sequence >()
+ );
+ }
+
+ static void* convertible(PyObject* obj_ptr) {
+ handle<> iter_handle(allow_null(PyObject_GetIter(obj_ptr)));
+ if (!iter_handle) {
+ PyErr_Clear();
+ return NULL;
+ }
+ detail::boost_fusion_from_python_check functor =
+ detail::boost_fusion_from_python_check(object(iter_handle));
+ try {
+ boost::mpl::for_each<Sequence>(functor);
+ handle<> p(allow_null(PyIter_Next(functor.iter.ptr())));
+ if (p) return NULL; // Python sequence is too long; require exact match.
+ } catch (detail::boost_fusion_from_python_failed&) {
+ return NULL;
+ }
+ return incref(functor.copy.ptr());
+ }
+
+ static void construct(
+ PyObject* obj_ptr,
+ converter::rvalue_from_python_stage1_data* data
+ ) {
+ typedef converter::rvalue_from_python_storage<Sequence> storage_t;
+ storage_t* storage = reinterpret_cast<storage_t*>(data);
+ void* bytes = storage->storage.bytes;
+ Sequence * r = new (bytes) Sequence;
+ handle<> copy(reinterpret_cast<PyObject*>(data->convertible));
+ detail::boost_fusion_from_python_set functor
+ = detail::boost_fusion_from_python_set(object(copy));
+ boost::fusion::for_each(*r,functor);
+ data->convertible = bytes;
+ }
+
+};
+
+} // namespace boost::python
+} // namespace boost
+
+#endif // !BOOST_PYTHON_FROM_PYTHON_BOOST_FUSION_HPP

Added: sandbox/python_extensions/boost/python/from_python/container.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/python/from_python/container.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,68 @@
+// Copyright 2010 Jim Bosch.
+// 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)
+
+#ifndef BOOST_PYTHON_FROM_PYTHON_CONTAINER_HPP
+#define BOOST_PYTHON_FROM_PYTHON_CONTAINER_HPP
+
+#include <boost/python/from_python/iterator.hpp>
+
+namespace boost {
+namespace python {
+
+/**
+ * @brief An rvalue from-python converter that creates a container (or anything
+ * else that can be constructed from a pair of iterators) from an arbitrary
+ * Python sequence.
+ *
+ * For overloaded functions, this converter will match any zero-length sequence
+ * or any sequence in which the first element is the correct type. It will not
+ * match Python objects which do not have a __len__ special method, or
+ * sequences in which the first element is the wrong type, allowing
+ * other converters to attempt these. It will raise an exception if the first
+ * element is the correct type and a subsequent element is not, or if the object
+ * has __len__ but not __iter__ or __getitem__.
+ */
+template <typename Container, typename Value=typename Container::value_type>
+struct container_from_python_sequence {
+
+ static void declare() {
+ converter::registry::push_back(
+ &convertible,
+ &construct,
+ type_id< Container >()
+ );
+ }
+
+ static void* convertible(PyObject * obj) {
+ try {
+ object sequence(handle<>(borrowed(obj)));
+ if (len(sequence) > 1) {
+ if (!extract<Value>(sequence[0]).check())
+ return NULL;
+ }
+ return obj;
+ } catch (error_already_set & err) {
+ handle_exception();
+ return NULL;
+ }
+ }
+
+ static void construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data) {
+ object sequence(handle<>(borrowed(obj)));
+ typedef converter::rvalue_from_python_storage<Container> storage_t;
+ storage_t* storage = reinterpret_cast<storage_t*>(data);
+ void* bytes = storage->storage.bytes;
+ new (bytes) Container(
+ from_python_iterator<Value>(sequence),
+ from_python_iterator<Value>()
+ );
+ data->convertible = bytes;
+ }
+};
+
+} // namespace boost::python
+} // namespace boost
+
+#endif // !BOOST_PYTHON_FROM_PYTHON_BOOST_FUSION_HPP

Added: sandbox/python_extensions/boost/python/from_python/iterator.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/python/from_python/iterator.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,65 @@
+// Copyright 2010 Jim Bosch.
+// 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)
+
+#ifndef BOOST_PYTHON_FROM_PYTHON_ITERATOR_HPP
+#define BOOST_PYTHON_FROM_PYTHON_ITERATOR_HPP
+
+#include <boost/python.hpp>
+#include <boost/iterator/iterator_facade.hpp>
+
+namespace boost {
+namespace python {
+
+template <typename Value>
+class from_python_iterator : public boost::iterator_facade<from_python_iterator<Value>,
+ Value,
+ boost::forward_traversal_tag,
+ Value
+ >
+{
+ bool _at_end;
+ object _iter;
+ object _item;
+
+ Value dereference() const {
+ return extract<Value>(_item);
+ }
+
+ void increment() {
+ PyObject * item = PyIter_Next(_iter.ptr());
+ if (item == NULL) {
+ if (PyErr_Occurred()) {
+ throw_error_already_set();
+ } else {
+ _item = object();
+ _at_end = true;
+ }
+ } else {
+ _item = object(handle<>(item));
+ }
+ }
+
+ bool equal(from_python_iterator const & other) const {
+ return other._at_end && this->_at_end;
+ }
+
+ friend class boost::iterator_core_access;
+public:
+
+ from_python_iterator() : _at_end(true), _iter(), _item() {}
+
+ explicit from_python_iterator(object const & iterable) : _at_end(false), _iter(), _item() {
+ PyObject * iter = PyObject_GetIter(iterable.ptr());
+ if (iter == NULL) throw_error_already_set();
+ _iter = object(handle<>(iter));
+ increment();
+ }
+
+};
+
+} // namespace boost::python
+} // namespace boost
+
+#endif // !BOOST_PYTHON_FROM_PYTHON_ITERATOR_HPP

Added: sandbox/python_extensions/boost/python/from_python/std_pair.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/python/from_python/std_pair.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,22 @@
+// Copyright 2010 Jim Bosch.
+// 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)
+
+#ifndef BOOST_PYTHON_FROM_PYTHON_STD_PAIR_HPP
+#define BOOST_PYTHON_FROM_PYTHON_STD_PAIR_HPP
+
+#include <boost/python/from_python/boost_fusion.hpp>
+#include <boost/mpl/adapted/std_pair.hpp>
+#include <boost/fusion/include/std_pair.hpp>
+
+namespace boost {
+namespace python {
+
+template <typename T1, typename T2>
+struct std_pair_from_python : public boost_fusion_from_python< std::pair<T1,T2> > {};
+
+} // namespace boost::python
+} // namespace boost
+
+#endif // !BOOST_PYTHON_FROM_PYTHON_BOOST_FUSION_HPP

Added: sandbox/python_extensions/boost/python/to_python/boost_fusion.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/python/to_python/boost_fusion.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,44 @@
+// Copyright 2010 Jim Bosch.
+// 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)
+
+#ifndef BOOST_PYTHON_TO_PYTHON_BOOST_FUSION_HPP
+#define BOOST_PYTHON_TO_PYTHON_BOOST_FUSION_HPP
+
+#include <boost/python.hpp>
+#include <boost/fusion/include/for_each.hpp>
+
+namespace boost {
+namespace python {
+
+namespace detail {
+
+struct boost_fusion_to_python_append {
+ mutable boost::python::list sequence;
+ template <typename T> void operator()(T const & x) const { sequence.append(x); }
+};
+
+} // namespace boost::python::detail
+
+template <typename Sequence>
+struct boost_fusion_to_python {
+
+ static PyObject * convert(Sequence const & input) {
+ detail::boost_fusion_to_python_append func;
+ boost::fusion::for_each(input,func);
+ return boost::python::incref(boost::python::tuple(func.sequence).ptr());
+ }
+
+ static PyTypeObject const * get_pytype() { return &PyTuple_Type; }
+
+ static void declare() {
+ boost::python::to_python_converter<Sequence,boost_fusion_to_python,true>();
+ }
+
+};
+
+} // namespace boost::python
+} // namespace boost
+
+#endif // !BOOST_PYTHON_TO_PYTHON_BOOST_FUSION_HPP

Added: sandbox/python_extensions/boost/python/to_python/copy_to_list.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/python/to_python/copy_to_list.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,57 @@
+// Copyright 2010 Jim Bosch.
+// 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
+
+#ifndef BOOST_PYTHON_COPY_TO_LIST_HPP
+#define BOOST_PYTHON_COPY_TO_LIST_HPP
+
+#include <boost/python.hpp>
+#include <boost/range.hpp>
+
+namespace boost { namespace python {
+
+/**
+ * @brief A model of ResultConverterGenerator (see Boost.Python docs) that copies any valid iterator
+ * range into a Python list.
+ *
+ * Useful for functions that return a STL container that one would like transformed into a Python list;
+ * use return_value_policy<copy_to_list>().
+ */
+struct copy_to_list {
+
+ template <typename Container>
+ struct converter {
+
+ typedef typename boost::range_const_iterator<Container>::type Iterator;
+
+ inline bool convertible() const { return true; }
+
+ inline PyObject * operator()(Container const & container) const {
+ boost::python::list result;
+ try {
+ for (Iterator i = boost::begin(container); i != boost::end(container); ++i) {
+ result.append(boost::python::object(*i));
+ }
+ } catch (error_already_set & exc) {
+ handle_exception();
+ return NULL;
+ }
+ Py_INCREF(result.ptr());
+ return result.ptr();
+ }
+
+ inline PyTypeObject const * get_pytype() const { return &PyList_Type; }
+
+ };
+
+ template <typename Container>
+ struct apply {
+ typedef converter<Container> type;
+ };
+
+};
+
+}}
+
+#endif // !BOOST_PYTHON_COPY_TO_LIST_HPP

Added: sandbox/python_extensions/boost/python/to_python/implicit.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/python/to_python/implicit.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,38 @@
+// Copyright 2010 Jim Bosch.
+// 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)
+
+#ifndef BOOST_PYTHON_TO_PYTHON_IMPLICIT_HPP
+#define BOOST_PYTHON_TO_PYTHON_IMPLICIT_HPP
+
+#include <boost/python.hpp>
+
+namespace boost {
+namespace python {
+
+template <typename Source, typename Target>
+struct implicit_to_python {
+ typedef typename boost::python::copy_const_reference::apply<Target const &>::type Converter;
+
+ static PyObject * convert(Source const & source) {
+ Converter converter;
+ Target target(source);
+ return converter(target);
+ }
+
+ static PyTypeObject const * get_pytype() {
+ Converter converter;
+ return converter.get_pytype();
+ }
+
+ static void declare() {
+ boost::python::to_python_converter<Source,implicit_to_python,true>();
+ }
+
+};
+
+} // namespace boost::python
+} // namespace boost
+
+#endif // !BOOST_PYTHON_TO_PYTHON_IMPLICIT_HPP

Added: sandbox/python_extensions/boost/python/to_python/std_auto_ptr.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/python/to_python/std_auto_ptr.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,34 @@
+// Copyright 2010 Jim Bosch.
+// 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)
+
+#ifndef BOOST_PYTHON_TO_PYTHON_STD_AUTO_PTR_HPP
+#define BOOST_PYTHON_TO_PYTHON_STD_AUTO_PTR_HPP
+
+#include <boost/python.hpp>
+#include <memory>
+
+namespace boost {
+namespace python {
+
+template <typename U>
+struct to_python_value< std::auto_ptr<U> > : detail::builtin_to_python {
+
+ typename boost::python::copy_const_reference::apply<boost::shared_ptr<U> const &>::type _shared_converter;
+
+ inline PyObject* operator()(std::auto_ptr<U> x) const {
+ boost::shared_ptr<U> shared(x);
+ return _shared_converter(shared);
+ }
+
+ inline PyTypeObject const* get_pytype() const {
+ return _shared_converter.get_pytype();
+ }
+
+};
+
+} // namespace boost::python
+} // namespace boost
+
+#endif // !BOOST_PYTHON_TO_PYTHON_STD_AUTO_PTR_HPP

Added: sandbox/python_extensions/boost/python/to_python/std_pair.hpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/boost/python/to_python/std_pair.hpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,23 @@
+// Copyright 2010 Jim Bosch.
+// 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)
+
+#ifndef BOOST_PYTHON_TO_PYTHON_STD_PAIR_HPP
+#define BOOST_PYTHON_TO_PYTHON_STD_PAIR_HPP
+
+#include <boost/fusion/adapted/std_pair.hpp>
+#include <boost/python/to_python/boost_fusion.hpp>
+
+namespace boost {
+namespace python {
+
+template <typename T1, typename T2>
+struct std_pair_to_python
+ : public boost_fusion_to_python< std::pair<T1,T2> >
+{};
+
+} // namespace boost::python
+} // namespace boost
+
+#endif // !BOOST_PYTHON_TO_PYTHON_STD_PAIR_HPP

Added: sandbox/python_extensions/libs/python/test/SConscript
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/libs/python/test/SConscript 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,8 @@
+Import("env")
+import os
+
+module = env.SharedLibrary("test_mod", "test_mod.cpp", SHLIBPREFIX="")
+path = os.path.abspath(os.curdir)
+runs = [env.Command(".%s" % script, [module, script], "python %s" % os.path.join(path,script))
+ for script in ("test_script.py",)]
+test = Alias("test", [runs])

Added: sandbox/python_extensions/libs/python/test/test_mod.cpp
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/libs/python/test/test_mod.cpp 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,35 @@
+#include <boost/python.hpp>
+#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
+#include <boost/python/to_python/copy_to_list.hpp>
+#include <boost/python/from_python/container.hpp>
+
+namespace bp = boost::python;
+
+static std::vector<int> return_int_vector() {
+ std::vector<int> result;
+ result.push_back(5);
+ result.push_back(3);
+ return result;
+}
+
+static bool accept_int_vector_cref(std::vector<int> const & v) {
+ return v[0] == 5 && v[1] == 3;
+}
+
+static bool accept_int_vector_ref(std::vector<int> & v) {
+ return v[0] == 5 && v[1] == 3;
+}
+
+BOOST_PYTHON_MODULE(test_mod) {
+ bp::class_< std::vector<int> >("int_vector")
+ .def(bp::vector_indexing_suite< std::vector<int> >())
+ ;
+ bp::container_from_python_sequence< std::vector<int>, int >::declare();
+
+ bp::def("return_int_vector_as_list", &return_int_vector, bp::return_value_policy<bp::copy_to_list>());
+ bp::def("return_int_vector_wrapped", &return_int_vector);
+
+ bp::def("accept_int_vector_cref", &accept_int_vector_cref);
+ bp::def("accept_int_vector_ref", &accept_int_vector_ref);
+}
+

Added: sandbox/python_extensions/libs/python/test/test_script.py
==============================================================================
--- (empty file)
+++ sandbox/python_extensions/libs/python/test/test_script.py 2010-05-07 21:04:03 EDT (Fri, 07 May 2010)
@@ -0,0 +1,23 @@
+import unittest
+import test_mod
+
+class TestVector(unittest.TestCase):
+
+ def testCopyToList(self):
+ v = test_mod.return_int_vector_as_list()
+ self.assertEqual(type(v), list)
+ self.assertEqual(v, [5,3])
+
+ def testWrapped(self):
+ v = test_mod.return_int_vector_wrapped()
+ self.assertEqual(type(v), test_mod.int_vector)
+ self.assert_(test_mod.accept_int_vector_cref(v))
+ self.assert_(test_mod.accept_int_vector_ref(v))
+
+ def testContainerFromPythonSequence(self):
+ v = [5, 3]
+ self.assert_(test_mod.accept_int_vector_cref(v))
+ self.assertRaises(TypeError, test_mod.accept_int_vector_ref, v)
+
+if __name__=="__main__":
+ unittest.main()


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk