Commit 72848313 authored by da-woods's avatar da-woods Committed by GitHub

Backport "noexcept" function modifier to Cython 0.29.x (GH-4903)

As a no-op, but it parses fine.

Also add some basic compile tests, and some brief documentation.
parent 3de4be40
......@@ -2820,6 +2820,8 @@ def p_c_func_declarator(s, pos, ctx, base, cmethod_flag):
s.expect(')')
nogil = p_nogil(s)
exc_val, exc_check = p_exception_value_clause(s)
# TODO - warning to enforce preferred exception specification order
nogil = nogil or p_nogil(s)
with_gil = p_with_gil(s)
return Nodes.CFuncDeclaratorNode(pos,
base = base, args = args, has_varargs = ellipsis,
......@@ -2938,7 +2940,11 @@ def p_with_gil(s):
def p_exception_value_clause(s):
exc_val = None
exc_check = 0
if s.sy == 'except':
if s.sy == 'IDENT' and s.systring == 'noexcept':
s.next()
exc_check = False # No-op in Cython 0.29.x
elif s.sy == 'except':
s.next()
if s.sy == '*':
exc_check = 1
......
......@@ -414,6 +414,17 @@ use this form, since there isn't any error return value to test.
Otherwise, an explicit error return value allows the C compiler to generate
more efficient code and is thus generally preferable.
To explicitly mark a function as not returning an exception use
``noexcept``.
cdef int spam() noexcept:
...
This is worth doing because (a) "explicit is better than implicit", and
(b) the default behaviour for ``cdef`` functions will change in Cython 3.0
so that functions will propagate exceptions by default. Therefore, it is
best to mark them now if you want them to swallow exceptions in the future.
An external C++ function that may raise an exception can be declared with::
cdef int spam() except +
......
......@@ -18,9 +18,17 @@ cdef int brian() except? 0:
cdef int silly() except -1:
pass
cdef int not_so_silly() noexcept:
pass
cdef int not_so_silly_and_gilless() noexcept nogil:
pass
spam()
eggs()
grail()
tomato()
brian()
silly()
not_so_silly()
not_so_silly_and_gilless()
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