Commit e2ac5fa7 authored by Stefan Behnel's avatar Stefan Behnel

fix infinite recursion in CF based None-check removal

parent 7deca631
...@@ -1483,12 +1483,22 @@ class NameNode(AtomicExprNode): ...@@ -1483,12 +1483,22 @@ class NameNode(AtomicExprNode):
def may_be_none(self): def may_be_none(self):
if self.cf_state: if self.cf_state:
# gard against infinite recursion on self-dependencies
if getattr(self, '_none_checking', False):
# self-dependency - either this node receives a None
# value from *another* node, or it can not reference
# None at this point => safe to assume "not None"
return False
self._none_checking = True
# evaluate control flow state to see if there were any # evaluate control flow state to see if there were any
# potential None values assigned to the node so far # potential None values assigned to the node so far
may_be_none = False
for assignment in self.cf_state: for assignment in self.cf_state:
if assignment.rhs.may_be_none(): if assignment.rhs.may_be_none():
return True may_be_none = True
return False break
del self._none_checking
return may_be_none
return super(NameNode, self).may_be_none() return super(NameNode, self).may_be_none()
def nonlocally_immutable(self): def nonlocally_immutable(self):
......
...@@ -82,3 +82,34 @@ def conditional_not_none(a, dict d not None): ...@@ -82,3 +82,34 @@ def conditional_not_none(a, dict d not None):
if a: if a:
d = {} d = {}
return d.get(1) return d.get(1)
@cython.test_fail_if_path_exists('//NoneCheckNode')
def self_dependency(int x):
"""
>>> self_dependency(1)
(1, 2)
>>> self_dependency(2)
(None, None)
"""
cdef dict a, b
a = {1:2}
b = {2:1}
for i in range(x):
a,b = b,a
return a.get(2), b.get(1)
@cython.test_assert_path_exists('//NoneCheckNode')
def self_dependency_none(int x):
"""
>>> self_dependency_none(False)
1
>>> self_dependency_none(True)
Traceback (most recent call last):
AttributeError: 'NoneType' object has no attribute 'get'
"""
cdef dict a, b
a = None
b = {2:1}
if x:
a,b = b,a
return b.get(2)
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