Commit a782a197 authored by Xavier Thompson's avatar Xavier Thompson

Raise an exception when conversion to CyObject fail

parent 99456781
...@@ -1658,12 +1658,19 @@ class CppClassNode(CStructOrUnionDefNode, BlockNode): ...@@ -1658,12 +1658,19 @@ class CppClassNode(CStructOrUnionDefNode, BlockNode):
# some types have not yet resolved that they can coerce to PyObject # some types have not yet resolved that they can coerce to PyObject
# in particular, any cypclass not yet examined ? # 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 (?) # 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 return # skip c methods with Python-incompatible return types
for argtype in cfunc_type.args: 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 return # skip c methods with Python-incompatible argument types
from .CypclassWrapper import underlying_name from .CypclassWrapper import underlying_name
......
...@@ -4228,6 +4228,8 @@ class CypClassType(CppClassType): ...@@ -4228,6 +4228,8 @@ class CypClassType(CppClassType):
def from_py_call_code(self, source_code, result_code, error_pos, code, def from_py_call_code(self, source_code, result_code, error_pos, code,
from_py_function=None, error_condition=None): from_py_function=None, error_condition=None):
extra_args = [self.wrapper_type.typeptr_cname if self.wrapper_type else 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( return self._assign_from_py_code(
source_code, result_code, error_pos, code, from_py_function, error_condition, extra_args=extra_args) source_code, result_code, error_pos, code, from_py_function, error_condition, extra_args=extra_args)
......
...@@ -190,6 +190,10 @@ ...@@ -190,6 +190,10 @@
* - borrow an Python reference * - borrow an Python reference
* - return a new atomic reference * - return a new atomic reference
* *
* In case of conversion failure:
* - raise an exception
* - return NULL
*
* template: * template:
* - W: the type of the extension type wrapper * - W: the type of the extension type wrapper
* - U: the type of the underlying cypclass * - U: the type of the underlying cypclass
...@@ -198,13 +202,20 @@ ...@@ -198,13 +202,20 @@
template <typename W, typename U> template <typename W, typename U>
static inline U* __Pyx_PyObject_AsCyObject(PyObject * ob, PyTypeObject * type) { static inline U* __Pyx_PyObject_AsCyObject(PyObject * ob, PyTypeObject * type) {
// the PyObject is not of the expected 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; return NULL;
}
W * wrapper = (W *)ob; W * wrapper = (W *)ob;
U * underlying = dynamic_cast<U *>(wrapper->nogil_cyobject); U * underlying = dynamic_cast<U *>(wrapper->nogil_cyobject);
// no underlying cyobject: shouldn't happen, playing it safe for now // 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 NULL;
}
// return a new atomic reference // return a new atomic reference
underlying->CyObject_INCREF(); underlying->CyObject_INCREF();
return underlying; 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