Commit 6c802335 authored by Matus Valo's avatar Matus Valo Committed by GitHub

Don't error when exception_check is set to True and return type is PyObject. (GH-4433)

With Cython syntax, we ignore the exception_check when a Python object is returned.
In pure Python mode, with a Python object type as return type, we reject it.
Instead of raising an error, we now just reset exception_check to False.

Found in https://github.com/cython/cython/issues/2529
parent ed6478f8
......@@ -712,6 +712,12 @@ class CFuncDeclaratorNode(CDeclaratorNode):
env.add_include_file('new') # for std::bad_alloc
env.add_include_file('stdexcept')
env.add_include_file('typeinfo') # for std::bad_cast
elif return_type.is_pyobject and self.exception_check:
# Functions in pure Python mode default to always check return values for exceptions
# (equivalent to the "except*" declaration). In this case, the exception clause
# is silently ignored for functions returning a Python object.
self.exception_check = False
if (return_type.is_pyobject
and (self.exception_value or self.exception_check)
and self.exception_check != '+'):
......
......@@ -50,8 +50,15 @@ def pyfunc(x): # invalid
return x + 1
@cython.exceptval(-1)
@cython.cfunc
def test_cdef_return_object_broken(x: object) -> object:
return x
_ERRORS = """
44:22: Calling gil-requiring function not allowed without gil
45:24: Calling gil-requiring function not allowed without gil
48:0: Python functions cannot be declared 'nogil'
53:0: Exception clause not allowed for function returning Python object
"""
......@@ -85,3 +85,19 @@ def call_cdef_inline(x):
"""
ret = cdef_inline(x)
return ret, cython.typeof(ret)
@cython.cfunc
def test_cdef_return_object(x: object) -> object:
"""
Test support of python object in annotations
>>> test_cdef_return_object(3)
3
>>> test_cdef_return_object(None)
Traceback (most recent call last):
...
RuntimeError
"""
if x:
return x
else:
raise RuntimeError()
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