|
|
Boost.PythonHeader <boost/python/from_python.hpp> |
from_python
from_python synopsis
from_python constructor
from_python observer functions
<boost/python/from_python.hpp> introduces a class
template from_python<T> for extracting a C++ object of
type T from a Python object.
from_python<class T>from_python<T> is the type used internally by
Boost.Python to extract C++ function arguments from a Python argument tuple
when calling a wrapped function. It can also be used directly to make
similar conversions in other contexts.
from_python synopsis
namespace boost { namespace python
{
template <class T>
struct from_python : private boost::noncopyable // Exposition only.
// from_python<T> meets the NonCopyable requirements
{
from_python(PyObject*);
bool convertible() const;
convertible-to-T operator()(PyObject*) const;
};
}
from_python constructorfrom_python(PyObject* p);
p != 0
from_python object suitable
for extracting a C++ object of type T from p.
from_python observer functionsbool convertible() const;
false if the conversion cannot succeed.
This indicates that either:
from_python_converter was registered for
T, or
p by returning 0 from its
convertible() function
operator() due to
an exception.
from_python<> is used in
overload resolution, and throwing an exception can be slow, it is useful
to be able to rule out a broad class of unsuccessful conversions without
throwing an exception.
convertible-to-T operator()(PyObject* p) const;
*p refers to the same object which was
passed to the constructor, and convertible() returns
true.
T.
#include <string>
#include <boost/python/from_python.hpp>
// If a std::string can be extracted from p, return its
// length. Otherwise, return 0.
std::size_t length_if_string(PyObject* p)
{
from_python<std::string> converter(p);
if (!converter.convertible())
return 0;
else
return converter(p).size();
}
Revised 13 November, 2002
© Copyright Dave Abrahams 2002.