Header <boost/python/extract.hpp>

Contents

Introduction

Exposes a mechanism for extracting C++ object values from
generalized Python objects. Note that
extract<...> can also be used to
"downcast" an object to some specific ObjectWrapper. Because
invoking a mutable python type with an argument of the same type
(e.g. list([1,2]) typically makes a copy of
the argument object, this may be the only way to access the ObjectWrapper's
interface on the original object.

Classes

Class template extract

extract<T> can be used to extract a value of
an arbitrary C++ type from an instance of object. Two usages are supported:

extract<T>(o) is a temporary object
which is implicitly convertible to T (explicit conversion
is also available through the object's function-call
operator). However, if no conversion is available which can convert
o to an object of type T, a Python
TypeError exception will be raised.

extract<T> x(o); constructs an extractor
whose check() member function can be used to ask whether
a conversion is available without causing an exception to be thrown.

Class extract
constructors and destructor

extract(PyObject* p);
extract(object const&);

Requires: The first form requires that p is non-null.

Effects:Stores a pointer to the Python object managed
by its constructor argument. In particular, the reference
count of the object is not incremented. The onus is on the user
to be sure it is not destroyed before the extractor's conversion
function is called.

Class extract
observer functions

result_type operator()() const;
operator result_type() const;

Effects: Converts the stored pointer to
result_type, which is either T or
T const&.

Returns: An object of result_type
corresponding to the one referenced by the stored pointer.

Throws:error_already_set
and sets a TypeError if no such conversion is
available. May also emit other unspecified exceptions thrown by
the converter which is actually used.

bool check() const;

Postconditions: None. In particular, note that a
return value of true does not preclude an exception
being thrown from operator result_type() or
operator()().

Returns:falseonly if no conversion from the
stored pointer to T is available.