15 | void register_ptr_to_python()
16 | {
17 | typedef typename boost::python::pointee::type X;
18 | objects::class_value_wrapper<
19 | P
20 | , objects::make_ptr_instance<
21 | X
22 | , objects::pointer_holder
23 | >
24 | >();
25 | }
26 |
27 | }} // namespace boost::python
28 |
29 | #endif // REGISTER_PTR_TO_PYTHON_HPP
30 |
31 |
32 |
--------------------------------------------------------------------------------
/doc/images/callouts/17.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | ]>
7 |
18 |
--------------------------------------------------------------------------------
/doc/reference/import.qbk:
--------------------------------------------------------------------------------
1 | [section boost/python/import.hpp]
2 | [section Introduction]
3 | Exposes a mechanism for importing python modules.
4 | [endsect]
5 | [section Function `import`]
6 | ``object import(str name);``
7 | [variablelist
8 | [[Effects][Imports the module named by name.]]
9 | [[Returns][An instance of object which holds a reference to the imported module.]]
10 | ]
11 | [endsect]
12 | [section Examples]
13 | The following example demonstrates the use of import to access a function in python, and later call it from within C++.
14 | ``
15 | #include
16 | #include
17 |
18 | using namespace boost::python;
19 |
20 | void print_python_version()
21 | {
22 | // Load the sys module.
23 | object sys = import("sys");
24 |
25 | // Extract the python version.
26 | std::string version = extract(sys.attr("version"));
27 | std::cout << version << std::endl;
28 | }
29 | ``
30 | [endsect]
31 | [endsect]
32 |
--------------------------------------------------------------------------------
/doc/html/images/callouts/17.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | ]>
7 |
18 |
--------------------------------------------------------------------------------
/example/numpy/Jamfile:
--------------------------------------------------------------------------------
1 | # Copyright Stefan Seefeld 2016.
2 | # Distributed under the Boost Software License, Version 1.0.
3 | # (See accompanying file LICENSE_1_0.txt or copy at
4 | # http://www.boost.org/LICENSE_1_0.txt)
5 |
6 | import python ;
7 |
8 | # Adjust the following if Boost.Python isn't installed in a default location
9 | lib boost_numpy
10 | :
11 | : /usr/local/Boost/lib
12 | /usr/local/Boost/include
13 | ;
14 |
15 | project numpy
16 | : requirements
17 | /usr/local/Boost/include
18 | boost_numpy
19 | .
20 | ;
21 |
22 | exe simple : simple.cpp boost_numpy /python//python ;
23 | exe dtype : dtype.cpp boost_numpy /python//python ;
24 | exe ndarray : ndarray.cpp /python//python ;
25 | exe fromdata : fromdata.cpp /python//python ;
26 | exe ufunc : ufunc.cpp /python//python ;
27 | exe wrap : wrap.cpp /python//python ;
28 |
29 | python-extension gaussian : gaussian.cpp ;
30 |
--------------------------------------------------------------------------------
/src/converter/arg_to_python_base.cpp:
--------------------------------------------------------------------------------
1 | // Copyright David Abrahams 2002.
2 | // Distributed under the Boost Software License, Version 1.0. (See
3 | // accompanying file LICENSE_1_0.txt or copy at
4 | // http://www.boost.org/LICENSE_1_0.txt)
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | namespace boost { namespace python { namespace converter {
13 |
14 | namespace detail
15 | {
16 | arg_to_python_base::arg_to_python_base(
17 | void const volatile* source, registration const& converters)
18 | # if !defined(BOOST_MSVC) || BOOST_MSVC <= 1300 || _MSC_FULL_VER > 13102179
19 | : handle<>
20 | # else
21 | : m_ptr
22 | # endif
23 | (converters.to_python(source))
24 | {
25 | }
26 | }
27 |
28 | }}} // namespace boost::python::converter
29 |
--------------------------------------------------------------------------------
/include/boost/python/object/find_instance.hpp:
--------------------------------------------------------------------------------
1 | // Copyright David Abrahams 2002.
2 | // Distributed under the Boost Software License, Version 1.0. (See
3 | // accompanying file LICENSE_1_0.txt or copy at
4 | // http://www.boost.org/LICENSE_1_0.txt)
5 | #ifndef FIND_INSTANCE_DWA2002312_HPP
6 | # define FIND_INSTANCE_DWA2002312_HPP
7 |
8 | # include
9 |
10 | namespace boost { namespace python { namespace objects {
11 |
12 | // Given a type_id, find the instance data which corresponds to it, or
13 | // return 0 in case no such type is held. If null_shared_ptr_only is
14 | // true and the type being sought is a shared_ptr, only find an
15 | // instance if it turns out to be NULL. Needed for shared_ptr rvalue
16 | // from_python support.
17 | BOOST_PYTHON_DECL void* find_instance_impl(PyObject*, type_info, bool null_shared_ptr_only = false);
18 |
19 | }}} // namespace boost::python::objects
20 |
21 | #endif // FIND_INSTANCE_DWA2002312_HPP
22 |
--------------------------------------------------------------------------------
/include/boost/python/detail/value_is_shared_ptr.hpp:
--------------------------------------------------------------------------------
1 | // Copyright David Abrahams 2003.
2 | // Copyright Stefan Seefeld 2016.
3 | // Distributed under the Boost Software License, Version 1.0. (See
4 | // accompanying file LICENSE_1_0.txt or copy at
5 | // http://www.boost.org/LICENSE_1_0.txt)
6 |
7 | #ifndef boost_python_detail_value_is_shared_ptr_hpp_
8 | #define boost_python_detail_value_is_shared_ptr_hpp_
9 |
10 | #include
11 | #include
12 |
13 | namespace boost { namespace python { namespace detail {
14 |
15 | template
16 | struct value_is_shared_ptr
17 | {
18 | static bool const value = is_shared_ptr
20 | ::type>
21 | ::type>
22 | ::value;
23 | typedef mpl::bool_ type;
24 | };
25 |
26 | }}} // namespace boost::python::detail
27 |
28 | #endif // VALUE_IS_SHARED_PTR_DWA2003224_HPP
29 |
--------------------------------------------------------------------------------
/include/boost/python/detail/pointee.hpp:
--------------------------------------------------------------------------------
1 | // Copyright David Abrahams 2002.
2 | // Distributed under the Boost Software License, Version 1.0. (See
3 | // accompanying file LICENSE_1_0.txt or copy at
4 | // http://www.boost.org/LICENSE_1_0.txt)
5 | #ifndef POINTEE_DWA2002323_HPP
6 | # define POINTEE_DWA2002323_HPP
7 |
8 | # include
9 |
10 | namespace boost { namespace python { namespace detail {
11 |
12 | template
13 | struct pointee_impl
14 | {
15 | template struct apply : remove_pointer {};
16 | };
17 |
18 | template <>
19 | struct pointee_impl
20 | {
21 | template struct apply
22 | {
23 | typedef typename T::element_type type;
24 | };
25 | };
26 |
27 | template
28 | struct pointee
29 | : pointee_impl::value>::template apply
30 | {
31 | };
32 |
33 | }}} // namespace boost::python::detail
34 |
35 | #endif // POINTEE_DWA2002323_HPP
36 |
--------------------------------------------------------------------------------
/test/back_reference.py:
--------------------------------------------------------------------------------
1 | # Copyright David Abrahams 2004. Distributed under the Boost
2 | # Software License, Version 1.0. (See accompanying
3 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4 | '''
5 | >>> from back_reference_ext import *
6 | >>> y = Y(3)
7 | >>> z = Z(4)
8 | >>> x_instances()
9 | 2
10 | >>> y2 = copy_Y(y)
11 | >>> x_instances()
12 | 3
13 | >>> z2 = copy_Z(z)
14 | >>> x_instances()
15 | 4
16 | >>> assert y_identity(y) is y
17 | >>> y_equality(y, y)
18 | 1
19 |
20 | >>> print(y_identity.__doc__.splitlines()[1])
21 | y_identity( (Y)arg1) -> object :
22 | '''
23 |
24 | def run(args = None):
25 | import sys
26 | import doctest
27 |
28 | if args is not None:
29 | sys.argv = args
30 | return doctest.testmod(sys.modules.get(__name__))
31 |
32 | if __name__ == '__main__':
33 | print("running...")
34 | import sys
35 | status = run()[0]
36 | if (status == 0): print("Done.")
37 | sys.exit(status)
38 |
--------------------------------------------------------------------------------
/doc/images/callouts/14.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | ]>
7 |
18 |
--------------------------------------------------------------------------------
/include/boost/python/object.hpp:
--------------------------------------------------------------------------------
1 | // Copyright David Abrahams 2002.
2 | // Distributed under the Boost Software License, Version 1.0. (See
3 | // accompanying file LICENSE_1_0.txt or copy at
4 | // http://www.boost.org/LICENSE_1_0.txt)
5 | #ifndef OBJECT_DWA2002612_HPP
6 | # define OBJECT_DWA2002612_HPP
7 |
8 | # include
9 | # include
10 | # include
11 | # include
12 | # include
13 | # include
14 | # include
15 |
16 | namespace boost { namespace python {
17 |
18 | inline ssize_t len(object const& obj)
19 | {
20 | ssize_t result = PyObject_Length(obj.ptr());
21 | if (PyErr_Occurred()) throw_error_already_set();
22 | return result;
23 | }
24 |
25 | }} // namespace boost::python
26 |
27 | #endif // OBJECT_DWA2002612_HPP
28 |
--------------------------------------------------------------------------------
/doc/html/images/callouts/14.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | ]>
7 |
18 |
--------------------------------------------------------------------------------
/test/aligned_class.cpp:
--------------------------------------------------------------------------------
1 | // Distributed under the Boost Software License, Version 1.0. (See
2 | // accompanying file LICENSE_1_0.txt or copy at
3 | // http://www.boost.org/LICENSE_1_0.txt)
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | using namespace boost::python;
12 |
13 | struct BOOST_ALIGNMENT(32) X
14 | {
15 | int x;
16 | BOOST_ALIGNMENT(32) float f;
17 | X(int n, float _f) : x(n), f(_f)
18 | {
19 | BOOST_ASSERT((reinterpret_cast(&f) % 32) == 0);
20 | }
21 | };
22 |
23 | int x_function(X& x) { return x.x;}
24 | float f_function(X& x) { return x.f;}
25 |
26 | BOOST_PYTHON_MODULE(aligned_class_ext)
27 | {
28 | class_("X", init());
29 | def("x_function", x_function);
30 | def("f_function", f_function);
31 | }
32 |
33 | #include "module_tail.cpp"
34 |
--------------------------------------------------------------------------------
/include/boost/python/detail/is_wrapper.hpp:
--------------------------------------------------------------------------------
1 | // Copyright David Abrahams 2004. Distributed under the Boost
2 | // Software License, Version 1.0. (See accompanying
3 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4 | #ifndef IS_WRAPPER_DWA2004723_HPP
5 | # define IS_WRAPPER_DWA2004723_HPP
6 |
7 | # include
8 | # include
9 |
10 | namespace boost { namespace python {
11 |
12 | template class wrapper;
13 |
14 | namespace detail
15 | {
16 | typedef char (&is_not_wrapper)[2];
17 | is_not_wrapper is_wrapper_helper(...);
18 | template
19 | char is_wrapper_helper(wrapper const volatile*);
20 |
21 | // A metafunction returning true iff T is [derived from] wrapper
22 | template
23 | struct is_wrapper
24 | : mpl::bool_<(sizeof(detail::is_wrapper_helper((T*)0)) == 1)>
25 | {};
26 |
27 | }}} // namespace boost::python::detail
28 |
29 | #endif // IS_WRAPPER_DWA2004723_HPP
30 |
--------------------------------------------------------------------------------
/test/bienstman4.cpp:
--------------------------------------------------------------------------------
1 | // Copyright David Abrahams 2002.
2 | // Distributed under the Boost Software License, Version 1.0. (See
3 | // accompanying file LICENSE_1_0.txt or copy at
4 | // http://www.boost.org/LICENSE_1_0.txt)
5 |
6 | #include