minor cleanups

This commit is contained in:
Wenzel Jakob 2016-05-03 13:28:40 +02:00
parent aadc2f3d8c
commit ad69634907
4 changed files with 20 additions and 17 deletions

View File

@ -225,9 +225,6 @@ using cast_op_type = typename std::conditional<std::is_pointer<typename std::rem
typename std::add_pointer<typename intrinsic_type<T>::type>::type, typename std::add_pointer<typename intrinsic_type<T>::type>::type,
typename std::add_lvalue_reference<typename intrinsic_type<T>::type>::type>::type; typename std::add_lvalue_reference<typename intrinsic_type<T>::type>::type>::type;
/// Thrown then trying to cast a null pointer into a reference argument
class invalid_reference_cast : public std::exception { };
/// Generic type caster for objects stored on the heap /// Generic type caster for objects stored on the heap
template <typename type> class type_caster_base : public type_caster_generic { template <typename type> class type_caster_base : public type_caster_generic {
public: public:
@ -256,7 +253,7 @@ public:
template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>; template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
operator type*() { return (type *) value; } operator type*() { return (type *) value; }
operator type&() { if (!value) throw invalid_reference_cast(); return *((type *) value); } operator type&() { if (!value) throw cast_error(); return *((type *) value); }
protected: protected:
typedef void *(*Constructor)(const void *stream); typedef void *(*Constructor)(const void *stream);
@ -763,7 +760,7 @@ public:
NAMESPACE_END(detail) NAMESPACE_END(detail)
template <typename T> inline T cast(handle handle) { template <typename T> T cast(handle handle) {
typedef detail::type_caster<typename detail::intrinsic_type<T>::type> type_caster; typedef detail::type_caster<typename detail::intrinsic_type<T>::type> type_caster;
type_caster conv; type_caster conv;
if (!conv.load(handle, true)) if (!conv.load(handle, true))
@ -771,7 +768,7 @@ template <typename T> inline T cast(handle handle) {
return conv.operator typename type_caster::template cast_op_type<T>(); return conv.operator typename type_caster::template cast_op_type<T>();
} }
template <typename T> inline object cast(const T &value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) { template <typename T> object cast(const T &value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) {
if (policy == return_value_policy::automatic) if (policy == return_value_policy::automatic)
policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy; policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy;
else if (policy == return_value_policy::automatic_reference) else if (policy == return_value_policy::automatic_reference)
@ -779,11 +776,11 @@ template <typename T> inline object cast(const T &value, return_value_policy pol
return object(detail::type_caster<typename detail::intrinsic_type<T>::type>::cast(value, policy, parent), false); return object(detail::type_caster<typename detail::intrinsic_type<T>::type>::cast(value, policy, parent), false);
} }
template <typename T> inline T handle::cast() const { return pybind11::cast<T>(*this); } template <typename T> T handle::cast() const { return pybind11::cast<T>(*this); }
template <> inline void handle::cast() const { return; } template <> inline void handle::cast() const { return; }
template <return_value_policy policy = return_value_policy::automatic_reference, template <return_value_policy policy = return_value_policy::automatic_reference,
typename... Args> inline tuple make_tuple(Args&&... args_) { typename... Args> tuple make_tuple(Args&&... args_) {
const size_t size = sizeof...(Args); const size_t size = sizeof...(Args);
std::array<object, size> args { std::array<object, size> args {
{ object(detail::type_caster<typename detail::intrinsic_type<Args>::type>::cast( { object(detail::type_caster<typename detail::intrinsic_type<Args>::type>::cast(
@ -799,7 +796,7 @@ template <return_value_policy policy = return_value_policy::automatic_reference,
return result; return result;
} }
template <typename... Args> inline object handle::call(Args&&... args) const { template <typename... Args> object handle::call(Args&&... args) const {
tuple args_tuple = pybind11::make_tuple(std::forward<Args>(args)...); tuple args_tuple = pybind11::make_tuple(std::forward<Args>(args)...);
object result(PyObject_CallObject(m_ptr, args_tuple.ptr()), false); object result(PyObject_CallObject(m_ptr, args_tuple.ptr()), false);
if (!result) if (!result)

View File

@ -305,12 +305,18 @@ to_string(T value) { return std::to_string((int) value); }
NAMESPACE_END(detail) NAMESPACE_END(detail)
#define PYBIND11_RUNTIME_EXCEPTION(name) \
class name : public std::runtime_error { public: \
name(const std::string &w) : std::runtime_error(w) { }; \
name(const char *s) : std::runtime_error(s) { }; \
name() : std::runtime_error("") { } \
};
// C++ bindings of core Python exceptions // C++ bindings of core Python exceptions
struct stop_iteration : public std::runtime_error { public: stop_iteration(const std::string &w="") : std::runtime_error(w) {} }; class error_already_set : public std::runtime_error { public: error_already_set() : std::runtime_error(detail::error_string()) {} };
struct index_error : public std::runtime_error { public: index_error(const std::string &w="") : std::runtime_error(w) {} }; PYBIND11_RUNTIME_EXCEPTION(stop_iteration)
struct error_already_set : public std::runtime_error { public: error_already_set() : std::runtime_error(detail::error_string()) {} }; PYBIND11_RUNTIME_EXCEPTION(index_error)
/// Thrown when pybind11::cast or handle::call fail due to a type casting error PYBIND11_RUNTIME_EXCEPTION(cast_error) /// Thrown when pybind11::cast or handle::call fail due to a type casting error
struct cast_error : public std::runtime_error { public: cast_error(const std::string &w = "") : std::runtime_error(w) {} };
[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); } [[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); }
[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); } [[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); }

View File

@ -413,7 +413,7 @@ protected:
try { try {
if (kwargs_consumed == nkwargs) if (kwargs_consumed == nkwargs)
result = it->impl(it, args_, parent); result = it->impl(it, args_, parent);
} catch (detail::invalid_reference_cast &) { } catch (cast_error &) {
result = PYBIND11_TRY_NEXT_OVERLOAD; result = PYBIND11_TRY_NEXT_OVERLOAD;
} }

View File

@ -126,7 +126,7 @@ public:
return result; return result;
} }
template <typename T> inline T cast() const { return operator object().cast<T>(); } template <typename T> T cast() const { return operator object().cast<T>(); }
operator bool() const { operator bool() const {
if (attr) { if (attr) {