One solution is to suppress the Boost.Python default initializer and register a custon initializer that can construct a Xi_CW<4>
object from an iterable Python object that contains INT96
objects.
- The default initializer can be suppressed by providing
boost::python::no_init
as the init_spec
when exposing the class via boost::python::class_
.
- An auxiliary function wrapped by
boost::python::make_constructor()
can be exposed as the __init__
method on a class.
Here is a complete example based on the types presented in the original question:
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/python.hpp>
/// @brief Mockup spam class.
struct spam
{
int value[3];
spam() {};
spam(int x, int y, int z)
{
value[0] = x;
value[1] = y;
value[2] = z;
}
};
/// @brief Mockup egg class.
template <std::size_t N>
class egg
{
public:
explicit egg(spam (&spams)[N]) : spams_(spams) {}
/// @brief Return string reprenstation of the egg class.
std::string to_string()
{
std::stringstream stream;
stream << "[";
BOOST_FOREACH(spam& s, spams_)
{
stream << "[" << s.value[0] << ", "
<< s.value[1] << ", "
<< s.value[2]
<< "]";
}
stream << "]";
return stream.str();
}
private:
spam spams_[N];
};
/// @brief Auxiliary function that will attempt to create an egg<N> type
/// from an iterable Python object that contains spam objects.
template <std::size_t N>
egg<N>* make_egg(boost::python::object object)
{
spam spams[N];
// Iterate over the python object, extracting and copying spam objects.
// Boost.Python will handle throwing exceptions for the appropriate cases:
// - object does not support indexing (__getitem__)
// - object[N-1] is out of range
// - object[i] does not contain or cannot be converted to a spam&
for (long i = 0; i < N; ++i)
spams[i] = boost::python::extract<spam&>(object[i]);
// Construct egg from the local spam array. Ownership of the object is
// passed to Boost.Python
return new egg<N>(spams);
}
/// @brief Expose an egg<N> type to Python.
///
/// @param name The name that will be exposed to Python.
template <std::size_t N>
boost::python::class_<egg<N> > expose_egg(const char* name)
{
namespace python = boost::python;
// Explicitly suppress the Boost.Python default constructor.
typedef egg<N> egg_type;
python::class_<egg_type> egg_class(name, python::no_init);
// Register a custom factory function as the constructor method.
egg_class
.def("__init__", python::make_constructor(&make_egg<N>))
.def("__str__", &egg_type::to_string)
;
return egg_class;
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
// Expose spam.
python::class_<spam>("Spam", python::init<int, int, int>());
// Expose different egg types.
expose_egg<2>("Egg2");
expose_egg<4>("Egg4");
}
Interactive usage:
>>> import example
>>> spams = [
... example.Spam(0, 1, 2),
... example.Spam(1, 2, 3),
... example.Spam(2, 3, 4),
... example.Spam(3, 4, 5)
... ]
>>> print example.Egg2(spams)
[[0, 1, 2][1, 2, 3]]
>>> print example.Egg4(tuple(spams)) # different type
[[0, 1, 2][1, 2, 3][2, 3, 4][3, 4, 5]]
>>> print example.Egg4(spams[:1]) # expect IndexError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> print example.Egg4(42) # expect TypeError (42[0] is invalid)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__'
>>> print example.Egg4([42]) # expect TypeError (conversion)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: No registered converter was able to extract a C++ reference to
type spam from this Python object of type int