Commit 3b6b2475 authored by Xavier Thompson's avatar Xavier Thompson

Raise an exception when conversion to CyObject fail

parent 00f25731
......@@ -1657,12 +1657,19 @@ class CppClassNode(CStructOrUnionDefNode, BlockNode):
# some types have not yet resolved that they can coerce to PyObject
# in particular, any cypclass not yet examined ?
if cfunc_return_type.is_cyp_class:
if cfunc_return_type.templates:
return # only skip templated cypclasses for now
# we pass the global scope as argument, should not affect the result (?)
if not cfunc_return_type.can_coerce_to_pyobject(env.global_scope()):
elif not cfunc_return_type.can_coerce_to_pyobject(env.global_scope()):
return # skip c methods with Python-incompatible return types
for argtype in cfunc_type.args:
if not argtype.type.can_coerce_to_pyobject(env.global_scope()):
if argtype.type.is_cyp_class:
if argtype.type.templates:
return # only skip templated cypclasses for now
elif not argtype.type.can_coerce_to_pyobject(env.global_scope()):
return # skip c methods with Python-incompatible argument types
from .CypclassWrapper import underlying_name
......
......@@ -4158,6 +4158,8 @@ class CypClassType(CppClassType):
def from_py_call_code(self, source_code, result_code, error_pos, code,
from_py_function=None, error_condition=None):
extra_args = [self.wrapper_type.typeptr_cname if self.wrapper_type else None]
if not error_condition:
error_condition = "!%s" % result_code
return self._assign_from_py_code(
source_code, result_code, error_pos, code, from_py_function, error_condition, extra_args=extra_args)
......
......@@ -190,6 +190,10 @@
* - borrow an Python reference
* - return a new atomic reference
*
* In case of conversion failure:
* - raise an exception
* - return NULL
*
* template:
* - W: the type of the extension type wrapper
* - U: the type of the underlying cypclass
......@@ -198,13 +202,20 @@
template <typename W, typename U>
static inline U* __Pyx_PyObject_AsCyObject(PyObject * ob, PyTypeObject * type) {
// the PyObject is not of the expected type
if (ob->ob_type != type)
if (ob->ob_type != type) {
PyErr_SetString(PyExc_TypeError, "Conversion Error: Could not convert to CyObject");
return NULL;
}
W * wrapper = (W *)ob;
U * underlying = dynamic_cast<U *>(wrapper->nogil_cyobject);
// no underlying cyobject: shouldn't happen, playing it safe for now
if (underlying == NULL)
if (underlying == NULL) {
PyErr_SetString(PyExc_TypeError, "Conversion Error: CyObject wrapper has no underlying CyObject");
return NULL;
}
// return a new atomic reference
underlying->CyObject_INCREF();
return underlying;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment