don't try to cast 'None' into a C++ lvalue reference
diff --git a/example/issues.cpp b/example/issues.cpp
index bb773fa..a462214 100644
--- a/example/issues.cpp
+++ b/example/issues.cpp
@@ -21,7 +21,7 @@
// #70 compilation issue if operator new is not public
class NonConstructible { private: void *operator new(size_t bytes) throw(); };
py::class_<NonConstructible>(m, "Foo");
- m.def("getstmt", []() -> NonConstructible * { return nullptr; },
+ m2.def("getstmt", []() -> NonConstructible * { return nullptr; },
py::return_value_policy::reference);
#endif
@@ -101,4 +101,7 @@
list.append(py::cast(e));
return list;
});
-};
+
+ // (no id): should not be able to pass 'None' to a reference argument
+ m2.def("print_element", [](ElementA &el) { std::cout << el.value() << std::endl; });
+}
diff --git a/example/issues.py b/example/issues.py
index 2dfd059..de41769 100644
--- a/example/issues.py
+++ b/example/issues.py
@@ -7,7 +7,7 @@
from example.issues import DispatchIssue, dispatch_issue_go
from example.issues import Placeholder, return_vec_of_reference_wrapper
from example.issues import iterator_passthrough
-from example.issues import ElementList, ElementA
+from example.issues import ElementList, ElementA, print_element
import gc
print_cchar("const char *")
@@ -42,3 +42,8 @@
for i, v in enumerate(el.get()):
print("%i==%i, " % (i, v.value()), end='')
print()
+
+try:
+ print_element(None)
+except Exception as e:
+ print("Failed as expected: " + str(e))
diff --git a/example/issues.ref b/example/issues.ref
index 13bafc2..2d34f42 100644
--- a/example/issues.ref
+++ b/example/issues.ref
@@ -5,3 +5,6 @@
[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]
[3, 5, 7, 9, 11, 13, 15]
0==0, 1==1, 2==2, 3==3, 4==4, 5==5, 6==6, 7==7, 8==8, 9==9,
+Failed as expected: Incompatible function arguments. The following argument types are supported:
+ 1. (example.issues.ElementA) -> NoneType
+
diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h
index ffb4893..64e6eee 100644
--- a/include/pybind11/cast.h
+++ b/include/pybind11/cast.h
@@ -225,6 +225,8 @@
typename std::add_pointer<typename intrinsic_type<T>::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
template <typename type> class type_caster_base : public type_caster_generic {
@@ -254,7 +256,7 @@
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); }
protected:
typedef void *(*Constructor)(const void *stream);
diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h
index ea67f7e..264646c 100644
--- a/include/pybind11/pybind11.h
+++ b/include/pybind11/pybind11.h
@@ -409,8 +409,13 @@
}
}
}
- if (kwargs_consumed == nkwargs)
- result = it->impl(it, args_, parent);
+
+ try {
+ if (kwargs_consumed == nkwargs)
+ result = it->impl(it, args_, parent);
+ } catch (detail::invalid_reference_cast &) {
+ result = PYBIND11_TRY_NEXT_OVERLOAD;
+ }
if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
break;