Commit d93be9df authored by Mark Florisson's avatar Mark Florisson

Allow nogil try/finally without 'with gil' block

parent f0cfc682
...@@ -2281,9 +2281,6 @@ class GilCheck(VisitorTransform): ...@@ -2281,9 +2281,6 @@ class GilCheck(VisitorTransform):
# which is wrapped in a StatListNode. Just unpack that. # which is wrapped in a StatListNode. Just unpack that.
node.finally_clause, = node.finally_clause.stats node.finally_clause, = node.finally_clause.stats
if node.state == 'gil':
self.seen_with_gil_statement = True
self.visitchildren(node) self.visitchildren(node)
self.nogil = was_nogil self.nogil = was_nogil
return node return node
...@@ -2319,24 +2316,14 @@ class GilCheck(VisitorTransform): ...@@ -2319,24 +2316,14 @@ class GilCheck(VisitorTransform):
def visit_TryFinallyStatNode(self, node): def visit_TryFinallyStatNode(self, node):
""" """
Take care of try/finally statements in nogil code sections. The Take care of try/finally statements in nogil code sections.
'try' must contain a 'with gil:' statement somewhere.
""" """
if not self.nogil or isinstance(node, Nodes.GILStatNode): if not self.nogil or isinstance(node, Nodes.GILStatNode):
return self.visit_Node(node) return self.visit_Node(node)
node.nogil_check = None node.nogil_check = None
node.is_try_finally_in_nogil = True node.is_try_finally_in_nogil = True
self.visitchildren(node)
# First, visit the body and check for errors
self.seen_with_gil_statement = False
self.visitchildren(node.body)
if not self.seen_with_gil_statement:
error(node.pos, "Cannot use try/finally in nogil sections unless "
"it contains a 'with gil' statement.")
self.visitchildren(node.finally_clause)
return node return node
def visit_Node(self, node): def visit_Node(self, node):
......
...@@ -159,6 +159,5 @@ _ERRORS = u""" ...@@ -159,6 +159,5 @@ _ERRORS = u"""
62:14: Coercion from Python not allowed without the GIL 62:14: Coercion from Python not allowed without the GIL
62:25: Coercion from Python not allowed without the GIL 62:25: Coercion from Python not allowed without the GIL
64:8: Try-except statement not allowed without gil 64:8: Try-except statement not allowed without gil
68:8: Cannot use try/finally in nogil sections unless it contains a 'with gil' statement.
85:8: For-loop using object bounds or target not allowed without gil 85:8: For-loop using object bounds or target not allowed without gil
""" """
...@@ -438,3 +438,21 @@ def test_nogil_try_finally_return(): ...@@ -438,3 +438,21 @@ def test_nogil_try_finally_return():
""" """
with nogil: with nogil:
nogil_try_finally_return() nogil_try_finally_return()
cdef int error_func() except -1 with gil:
raise Exception("propagate this")
def test_nogil_try_finally_error_label():
"""
>>> test_nogil_try_finally_error_label()
print me first
propagate this
"""
try:
with nogil:
try:
error_func()
finally:
with gil: print "print me first"
except Exception, e:
print e.args[0]
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