Commit 01085d2a authored by Stefan Behnel's avatar Stefan Behnel

Make the GIL-avoidance in 7d99b0f0 actually work in nogil functions and not just nogil sections.

Closes #3558.
parent 7f5956f4
......@@ -1847,10 +1847,12 @@ class FuncDefNode(StatNode, BlockNode):
use_refnanny = not lenv.nogil or lenv.has_with_gil_block
gilstate_decl = code.insertion_point()
gilstate_decl = None
if acquire_gil or acquire_gil_for_var_decls_only:
code.put_ensure_gil()
code.funcstate.gil_owned = True
else:
gilstate_decl = code.insertion_point()
if profile or linetrace:
if not self.is_generator:
......@@ -1976,7 +1978,7 @@ class FuncDefNode(StatNode, BlockNode):
gil_owned = {
'success': code.funcstate.gil_owned,
'error': code.funcstate.gil_owned,
'gil_state_declared': False,
'gil_state_declared': gilstate_decl is None,
}
def assure_gil(code_path):
if not gil_owned[code_path]:
......
......@@ -10,26 +10,52 @@ cimport cython
"//GILStatNode//GILStatNode",
"//GILStatNode//GILStatNode//PrintStatNode",
)
def test_print_in_nogil(x):
def test_print_in_nogil_section(x):
"""
>>> test_print_in_nogil(123)
>>> test_print_in_nogil_section(123)
--123--
"""
with nogil:
print f"--{x}--"
@cython.test_assert_path_exists(
"//GILStatNode",
"//GILStatNode//PrintStatNode",
)
cpdef int test_print_in_nogil_func(x) nogil except -1:
"""
>>> _ = test_print_in_nogil_func(123)
--123--
"""
print f"--{x}--"
@cython.test_assert_path_exists(
"//GILStatNode",
"//GILStatNode//GILStatNode",
"//GILStatNode//GILStatNode//RaiseStatNode",
)
def test_raise_in_nogil(x):
def test_raise_in_nogil_section(x):
"""
>>> try: test_raise_in_nogil(123)
>>> try: test_raise_in_nogil_section(123)
... except ValueError as exc: print(exc)
... else: print("NOT RAISED !")
--123--
"""
with nogil:
raise ValueError(f"--{x}--")
@cython.test_assert_path_exists(
"//GILStatNode",
"//GILStatNode//RaiseStatNode",
)
cpdef int test_raise_in_nogil_func(x) nogil except -1:
"""
>>> try: test_raise_in_nogil_func(123)
... except ValueError as exc: print(exc)
... else: print("NOT RAISED !")
--123--
"""
raise ValueError(f"--{x}--")
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