2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 06:02:14 +00:00

*** empty log message ***

[SVN r12805]
This commit is contained in:
Dave Abrahams
2002-02-14 15:57:40 +00:00
parent 08c909fd41
commit 12c7981450
12 changed files with 331 additions and 363 deletions

View File

@@ -76,19 +76,24 @@ first parameter. Its template arguments are:
<table border="1" summary="class_ template parameters">
<tr>
<th>Parameter
<th>Description
<th>Requirements
<th>Default
<tr>
<td><code>T</code>
<td>The class being exposed to Python
<td>A class type.
<tr>
<td><code>Bases</code>
<td>An <a href="../../../mpl/doc/Sequences.html">MPL sequence</a> of C++ base classes
<td>An <a href="../../../mpl/doc/Sequences.html">MPL
sequence</a> of C++ base classes of <code>T</code>.
<td>An unspecified empty sequence
<tr>
<td><a href="HolderGenerator.html">HolderGenerator</a></code>
<td>A type generator for the holder which
maintains the C++ object inside the Python instance.
<td><code>HolderGenerator</code>
<td>A model of <code><a href="HolderGenerator.html">HolderGenerator</a></code>.
<td><code>boost::python::objects::value_holder_generator</code>
</table>

View File

@@ -78,7 +78,7 @@ void handle_exception() throw();
<dl class="handle_exception-semantics">
<dt><b>Requires:</b> The first form requires that the expression
<code><a
href="../../function/doc/reference.html#functionN">function0</a>&lt;void&gt;(f)</code>
href="../../../function/doc/reference.html#functionN">function0</a>&lt;void&gt;(f)</code>
is valid. The second form requires that a C++ exception is currently
being handled (see section 15.1 in the C++ standard).
</dt>

View File

@@ -1,142 +0,0 @@
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
class Bar { int x; }
class Foo
{
public:
Foo(int x) : b(x) {}
Bar const& get_bar() const { return b; }
private:
Bar b;
};
using namespace boost::python;
BOOST_PYTHON_MODULE_INIT(my_module)
{
module m("my_module")
.add(
class_<Bar>()
)
.add(
class_<Foo>()
.def_init(args<int>())
.def("get_bar", &Foo::get_bar
, return_value_policy<copy_const_reference>())
)
}
using namespace boost::python;
ref foo =
class_<Foo,bases<Bar,Baz> >()
.def_init(args<int,char const*>())
.def_init(args<double>())
.def("get_name", &Foo::get_name, return_internal_reference<>())
.def("set_name", &Foo::set_name)
.object();
<blockquote><pre>
#include &lt;string&gt;
#include &lt;boost/python/errors.hpp&gt;
#include &lt;boost/python/reference.hpp&gt;
// Returns a std::string which has the same value as obj's "__name__"
// attribute.
std::string get_name(boost::python::ref obj)
{
// throws if there's no __name__ attribute
PyObject* p = boost::python::expect_non_null(
PyObject_GetAttrString(obj.get(), "__name__"));
// throws if it's not a Python string
std::string result(
boost::python::expect_non_null(
PyString_AsString(p)));
Py_XDECREF(p); // Done with p
return result;
}
//
// Demonstrate form 1 of handle_exception
//
// Place a Python Int object whose value is 1 if a and b have
// identical "__name__" attributes, 0 otherwise.
void same_name_impl(PyObject*&amp; result, PyObject* a, PyObject* b)
{
result = PyInt_FromLong(
get_name(boost::python::ref(a1)) == get_name(boost::python::ref(a2)));
}
// This is an example Python 'C' API interface function
extern "C" PyObject*
same_name(PyObject* args, PyObject* keywords)
{
PyObject* a1;
PyObject* a2;
PyObject* result = 0;
if (!PyArg_ParseTuple(args, const_cast&lt;char*&gt;("OO"), &amp;a1, &amp;a2))
return 0;
// Use boost::bind to make an object compatible with
// boost::Function0&lt;void&gt;
if (boost::python::handle_exception(
boost::bind&lt;void&gt;(same_name_impl, boost::ref(result), a1, a2)))
{
// an exception was thrown; the Python error was set by
// handle_exception()
return 0;
}
return result;
}
//
// Demonstrate form 2 of handle_exception. Not well-supported by all
// compilers.
//
extern "C" PyObject*
same_name2(PyObject* args, PyObject* keywords)
{
PyObject* a1;
PyObject* a2;
PyObject* result = 0;
if (!PyArg_ParseTuple(args, const_cast&lt;char*&gt;("OO"), &amp;a1, &amp;a2))
return 0;
try {
return PyInt_FromLong(
get_name(boost::python::ref(a1)) == get_name(boost::python::ref(a2)));
}
catch(...)
{
// If an exception was thrown, translate it to Python
boost::python::handle_exception();
return 0;
}
}
</pre></blockquote>
template <class T>
struct from_python
{
from_python(PyObject*);
bool convertible() const;
X operator()(PyObject*) const;
};

155
doc/v2/make_function.html Normal file
View File

@@ -0,0 +1,155 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../boost.css">
<title>Boost.Python - &lt;boost/python/make_function.hpp&gt;</title>
</head>
<body link="#0000ff" vlink="#800080">
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
"header">
<tr>
<td valign="top" width="300">
<h3><a href="../../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../../c++boost.gif" border="0"></a></h3>
</td>
<td valign="top">
<h1 align="center">Boost.Python</h1>
<h2 align="center">Header &lt;boost/python/make_function.hpp&gt;</h2>
</td>
</tr>
</table>
<hr>
<h2>Contents</h2>
<dl class="page-index">
<dt><a href="#introduction">Introduction</a></dt>
<dt><a href="#functions">Functions</a></dt>
<dl class="page-index">
<dt><a href="#make_function-spec">make_function</a></dt>
<dt><a href="#make_constructor-spec">make_constructor</a></dt>
</dl>
<dt><a href="#examples">Example</a></dt>
</dl>
<hr>
<h2><a name="introduction"></a>Introduction</h2>
<p>
<code><a href="#make_function-spec">make_function</a>()</code> and
<code><a href="#make_constructor-spec">make_constructor</a>()</code>
are the functions used internally by <code>class_&lt;&gt;::<a
href="class.html#class_-spec-modifiers">def</a></code>,
<code>class_&lt;&gt;::<a
href="module.html#module-spec-modifiers">def</a></code>,
and
<code>class_&lt;&gt;::<a
href="class.html#class_-spec-modifiers">def_init</a></code>
to produce Python callable objects which wrap C++ functions and member
functions.
</p>
<h2><a name="functions"></a>Functions</h2>
<pre>
<a name="make_function-spec">template &lt;class F&gt;
objects::function* make_function(F f)&lt;/a&gt;
template &lt;class F, class Policies&gt;
objects::function* make_function(F f, Policies const&amp; policies)
</pre>
<dl class="function-semantics">
<dt><b>Requires:</b> <code>F</code> is a function pointer or member
function pointer type</dt>
<dt><b>Effects:</b> Creates a Python callable object which, when
called from Python, converts its arguments to C++ and calls
<code>f</code>. If <code>F</code> is a pointer-to-member-function
type, the target object of the function call (<code>*this</code>)
will be taken from the first Python argument, and subsequent Python
arguments will be used as the arguments to <code>f</code>. If
<code>policies</code> are supplied, it must be a model of <a
href="CallPolicies.html">CallPolicies</a>, and will be applied to
the function as described <a href="CallPolicies.html">here</a>.</dt>
<dt><b>Returns:</b> A pointer convertible to <code>PyObject*</code>
which refers to the new Python callable object.</dt>
</dl>
<pre>
<a name="make_constructor-spec">template &lt;class T, class ArgList, class Generator&gt;
objects::function* make_constructor();</a>
</pre>
<dl class="make_constructor-semantics">
<dt><b>Requires:</b> <code>T</code> is a class
type. <code>ArgList</code> is an <a
href="../../../mpl/doc/Sequences.html">MPL sequence</a> of C++
argument types (<i>A1,&nbsp;A2,...&nbsp;AN</i>) such that if
<code>a1,&nbsp;a2</code>...&nbsp;<code>aN</code> are objects of type
<i>A1,&nbsp;A2,...&nbsp;AN</i> respectively, the expression
<code>new Generator::apply&lt;T&gt;::type(a1,&nbsp;a2</code>...&nbsp;<code>aN</code>) is
valid. Generator is a model of <a
href="HolderGenerator.html">HolderGenerator</a>.
</dt>
<dt><b>Effects:</b> Creates a Python callable object which, when
called from Python, expects its first argument to be a Boost.Python
extension class object. It converts its remaining its arguments to
C++ and passes them to the constructor of a dynamically-allocated
<code>Generator::apply&lt;T&gt;::type</code> object. The result is
installed in the extension class object.
</dt>
<dt><b>Returns:</b> The new Python callable object</dt>
</dl>
<h2><a name="examples"></a>Example</h2>
<p>C++ function exposed below returns a callable object wrapping one
of two functions.
<pre>
#include &lt;boost/python/make_function.hpp&gt;
#include &lt;boost/python/module.hpp&gt;
char const* foo() { return "foo"; }
char const* bar() { return "bar"; }
PyObject* choose_function(bool selector)
{
if (selector)
return boost::python::make_function(foo);
else
return boost::python::make_function(bar);
}
BOOST_PYTHON_MODULE_INIT(make_function_test)
{
module("make_function_test")
.def("choose_function", choose_function);
}
</pre>
</p>
It can be used this way in Python:
<pre>
&gt;&gt;&gt; from make_function_test import *
&gt;&gt;&gt; f = choose_function(1)
&gt;&gt;&gt; g = choose_function(0)
&gt;&gt;&gt; f()
'foo'
&gt;&gt;&gt; g()
'bar'
</pre>
<p>
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
14 February 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a>
2002. All Rights Reserved.</i></p>
</body>
</html>

View File

@@ -0,0 +1,115 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../boost.css">
<title>Boost.Python - &lt;boost/python/manage_new_object.hpp&gt;</title>
</head>
<body link="#0000ff" vlink="#800080">
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
"header">
<tr>
<td valign="top" width="300">
<h3><a href="../../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../../c++boost.gif" border="0"></a></h3>
</td>
<td valign="top">
<h1 align="center">Boost.Python</h1>
<h2 align="center">Header &lt;boost/python/manage_new_object.hpp&gt;</h2>
</td>
</tr>
</table>
<hr>
<h2>Contents</h2>
<dl class="page-index">
<dt><a href="#classes">Classes</a></dt>
<dl class="page-index">
<dt><a href="#manage_new_object-spec">Class <code>manage_new_object</code></a></dt>
<dl class="page-index">
<dt><a href="#manage_new_object-spec-synopsis">Class <code>manage_new_object</code> synopsis</a></dt>
<dt><a href="#manage_new_object-spec-metafunctions">Class <code>manage_new_object</code> metafunctions</a></dt>
</dl>
</dl>
<dt><a href="#examples">Example</a></dt>
</dl>
<hr>
<h2><a name="classes"></a>Classes</h2>
<h3><a name="manage_new_object-spec"></a>Class <code>manage_new_object</code></h3>
<p>
<code>manage_new_object</code> is a model of <a
href="ResultConverterGenerator.html">ResultConverterGenerator</a>
which can be used to wrap C++ functions which return a pointer to an
object allocated with a <i>new-expression</i>, and expect the caller
to take responsibility for deleting that object.
</p>
<h4><a name="manage_new_object-spec-synopsis"></a>Class <code>manage_new_object</code> synopsis</h4>
<pre>
namespace boost { namespace python
{
struct manage_new_object
{
template &lt;class T&gt; struct apply;
};
}}
</pre>
<h4><a name="manage_new_object-spec-metafunctions"></a>Class <code>manage_new_object</code> metafunctions</h4>
<pre>
template &lt;class T&gt; struct apply
</pre>
<dl class="metafunction-semantics">
<dt><b>Requires:</b> <code>T</code> is <code>U*</code> for some <code>U</code>.</dt>
<dt><b>Returns:</b> <code>typedef <a href="to_python_indirect.html#to_python_indirect-spec">to_python_indirect</a>&lt;T&gt; type;</code></dt>
</dl>
<h2><a name="examples"></a>Example</h2>
<p> In C++:
<pre>
#include &lt;boost/python/module.hpp&gt;
#include &lt;boost/python/class.hpp&gt;
#include &lt;boost/python/manage_new_object.hpp&gt;
#include &lt;boost/python/return_value_policy.hpp&gt;
struct Foo {
Foo(int x) : x(x){}
int get_x() { return x; }
int x;
};
Foo* make_foo(int x) { return new Foo(x); }
// Wrapper code
using namespace boost::python;
BOOST_PYTHON_MODULE_INIT(my_module)
{
module m("my_module")
.def(&quot;make_foo&quot;, make_foo)
.add(
class_&lt;Foo&gt;()
.def(&quot;get_x&quot;, &amp;Foo::get_x)
)
}
</pre>
In Python:
<pre>
&gt;&gt;&gt; from my_module import *
&gt;&gt;&gt; f = make_foo(3) # create a Foo object
&gt;&gt;&gt; f.get_x()
3
</pre>
<p>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
14 February 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a>
2002. All Rights Reserved.</i></p>
</body>
</html>

View File

@@ -1,210 +0,0 @@
// Copyright David Abrahams 2001. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
//
// This work was funded in part by Lawrence Berkeley National Labs
//
// This file generated for 5-argument member functions and 6-argument free
// functions by gen_call.py
#ifndef CALL_DWA20011214_HPP
# define CALL_DWA20011214_HPP
# include <boost/python/detail/returning.hpp>
namespace boost { namespace python {
template <class R>
inline PyObject* call(R (*f)(), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0>
inline PyObject* call(R (*f)(A0), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1>
inline PyObject* call(R (*f)(A0, A1), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2>
inline PyObject* call(R (*f)(A0, A1, A2), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3>
inline PyObject* call(R (*f)(A0, A1, A2, A3), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4>
inline PyObject* call(R (*f)(A0, A1, A2, A3, A4), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4, class A5>
inline PyObject* call(R (*f)(A0, A1, A2, A3, A4, A5), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
// Member functions
template <class R, class A0>
inline PyObject* call(R (A0::*f)(), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1>
inline PyObject* call(R (A0::*f)(A1), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2>
inline PyObject* call(R (A0::*f)(A1, A2), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3>
inline PyObject* call(R (A0::*f)(A1, A2, A3), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4>
inline PyObject* call(R (A0::*f)(A1, A2, A3, A4), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4, class A5>
inline PyObject* call(R (A0::*f)(A1, A2, A3, A4, A5), PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0>
inline PyObject* call(R (A0::*f)() const, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1>
inline PyObject* call(R (A0::*f)(A1) const, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2>
inline PyObject* call(R (A0::*f)(A1, A2) const, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3>
inline PyObject* call(R (A0::*f)(A1, A2, A3) const, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4>
inline PyObject* call(R (A0::*f)(A1, A2, A3, A4) const, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4, class A5>
inline PyObject* call(R (A0::*f)(A1, A2, A3, A4, A5) const, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0>
inline PyObject* call(R (A0::*f)() volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1>
inline PyObject* call(R (A0::*f)(A1) volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2>
inline PyObject* call(R (A0::*f)(A1, A2) volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3>
inline PyObject* call(R (A0::*f)(A1, A2, A3) volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4>
inline PyObject* call(R (A0::*f)(A1, A2, A3, A4) volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4, class A5>
inline PyObject* call(R (A0::*f)(A1, A2, A3, A4, A5) volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0>
inline PyObject* call(R (A0::*f)() const volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1>
inline PyObject* call(R (A0::*f)(A1) const volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2>
inline PyObject* call(R (A0::*f)(A1, A2) const volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3>
inline PyObject* call(R (A0::*f)(A1, A2, A3) const volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4>
inline PyObject* call(R (A0::*f)(A1, A2, A3, A4) const volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
template <class R, class A0, class A1, class A2, class A3, class A4, class A5>
inline PyObject* call(R (A0::*f)(A1, A2, A3, A4, A5) const volatile, PyObject* args, PyObject* keywords)
{
return detail::returning<R>::call(f, args, keywords);
}
}} // namespace boost::python
#endif // CALL_DWA20011214_HPP

View File

@@ -6,6 +6,7 @@
#ifndef BUILTIN_CONVERTERS_DWA2002124_HPP
# define BUILTIN_CONVERTERS_DWA2002124_HPP
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/none.hpp>
# include <string>
namespace boost { namespace python {
@@ -44,18 +45,19 @@ namespace detail
BOOST_PYTHON_TO_PYTHON_BY_VALUE(signed T, PyInt_FromLong(x)) \
BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned T, PyInt_FromLong(x))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, PyInt_FromLong(x))
BOOST_PYTHON_TO_INT(char)
BOOST_PYTHON_TO_INT(short)
BOOST_PYTHON_TO_INT(int)
BOOST_PYTHON_TO_INT(long)
# undef BOOST_TO_PYTHON_INT
BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, PyString_FromString(x))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, x ? PyString_FromString(x) : detail::none())
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, PyString_FromString(x.c_str()))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, PyFloat_FromDouble(x))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, PyFloat_FromDouble(x))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, PyFloat_FromDouble(x))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(PyObject*, x)
BOOST_PYTHON_TO_PYTHON_BY_VALUE(PyObject*, x ? x : detail::none())
namespace converter
{

View File

@@ -13,6 +13,7 @@
# include <boost/python/converter/type_id.hpp>
# include <boost/python/converter/registry.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/utility.hpp>
namespace boost { namespace python { namespace converter {

View File

@@ -30,7 +30,7 @@ class BOOST_PYTHON_DECL module_base
void add_type(ref);
// Return a reference to the Python module object being built
ref module() const;
ref object() const;
private:
ref m_module;
@@ -76,7 +76,7 @@ class module : public module_base
//
// inline implementations
//
inline ref module_base::module() const
inline ref module_base::object() const
{
return m_module;
}

View File

@@ -150,6 +150,31 @@ namespace
}
};
extern "C" PyObject* identity_unaryfunc(PyObject* x)
{
Py_INCREF(x);
return x;
}
unaryfunc non_null = identity_unaryfunc;
struct convertible_to_bool
{
static unaryfunc* execute(PyObject* obj)
{
return &non_null;
}
};
struct py_object_as_bool
{
static bool execute(PyObject* obj)
{
return PyObject_IsTrue(obj);
}
};
struct convertible_to_double
{
static unaryfunc* execute(PyObject* obj)
@@ -203,6 +228,9 @@ namespace
void initialize_builtin_converters()
{
static scalar_from_python<
bool, convertible_to_bool, py_object_as_bool> bool_from_python;
REGISTER_INT_CONVERTERS2(char);
REGISTER_INT_CONVERTERS2(short);
REGISTER_INT_CONVERTERS2(int);

View File

@@ -29,6 +29,7 @@ BOOST_PYTHON_MODULE_INIT(builtin_converters_ext)
{
boost::python::module("builtin_converters_ext")
.def("rewrap_value_bool", by_value<bool>::rewrap)
.def("rewrap_value_signed_char", by_value<signed char>::rewrap)
.def("rewrap_value_unsigned_char", by_value<unsigned char>::rewrap)
.def("rewrap_value_int", by_value<int>::rewrap)
@@ -44,6 +45,7 @@ BOOST_PYTHON_MODULE_INIT(builtin_converters_ext)
.def("rewrap_value_cstring", by_value<char const*>::rewrap)
.def("rewrap_const_reference_bool", by_const_reference<bool>::rewrap)
.def("rewrap_const_reference_signed_char", by_const_reference<signed char>::rewrap)
.def("rewrap_const_reference_unsigned_char", by_const_reference<unsigned char>::rewrap)
.def("rewrap_const_reference_int", by_const_reference<int>::rewrap)

View File

@@ -1,5 +1,11 @@
"""
>>> from builtin_converters_ext import *
>>> rewrap_value_bool(None)
0
>>> rewrap_value_bool(0)
0
>>> rewrap_value_bool(33)
1
>>> rewrap_value_signed_char(42)
42
>>> rewrap_value_unsigned_char(42)
@@ -29,6 +35,12 @@
>>> rewrap_value_string('yo, wassup?')
'yo, wassup?'
>>> rewrap_const_reference_bool(None)
0
>>> rewrap_const_reference_bool(0)
0
>>> rewrap_const_reference_bool('yes')
1
>>> rewrap_const_reference_signed_char(42)
42
>>> rewrap_const_reference_unsigned_char(42)