diff --git a/Lib/python/std_common.i b/Lib/python/std_common.i index 4a33921e7..b2fa500c8 100644 --- a/Lib/python/std_common.i +++ b/Lib/python/std_common.i @@ -156,7 +156,9 @@ namespace swigpy { int res = (SWIG_ConvertPtr(obj, (void**)&p, type_info(), 0) != -1) ? SWIG_OLDOBJ : 0; if (res) { - if (val) *val = p; + if (val) { + *val = p; + } } else { PyErr_Format(PyExc_TypeError, "a '%s *' is expected", type_name()); @@ -246,8 +248,8 @@ namespace swigpy { template struct traits_as { static Type as(PyObject *obj, bool throw_error) { - Type *v = 0; - int res = (obj ? asptr(obj, &v) : 0) && v; + Type *v = 0; + int res = (obj ? traits_asptr::asptr(obj, &v) : 0) && v; if (res) { if (res == SWIG_NEWOBJ) { Type r(*v); @@ -270,6 +272,27 @@ namespace swigpy { } } }; + + template + struct traits_as { + static Type* as(PyObject *obj, bool throw_error) { + Type *v = 0; + int res = (obj ? traits_asptr::asptr(obj, &v) : 0) && v; + if (res) { + return v; + } else { + // Uninitialized return value, no Type() constructor required. + std::string msg = "a value of type '"; + msg += swigpy::type_name(); + msg += "' is expected"; + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, msg.c_str()); + } + if (throw_error) throw std::invalid_argument(msg); + return 0; + } + } + }; template inline Type as(PyObject *obj, bool te = false) {