Commit bd4658c4 authored by Stefan Behnel's avatar Stefan Behnel

Optimise "range(enum)" (currently infers enum type for loop variable - worth reconsidering).

parent 99085010
...@@ -31,6 +31,8 @@ Features added ...@@ -31,6 +31,8 @@ Features added
Bugs fixed Bugs fixed
---------- ----------
* Loops over ``range(enum)`` were not converted into C for-loops.
* Compile time ``DEF`` assignments were evaluated even when they occur inside of * Compile time ``DEF`` assignments were evaluated even when they occur inside of
falsy ``IF`` blocks. (Github issue #1796) falsy ``IF`` blocks. (Github issue #1796)
......
...@@ -259,7 +259,7 @@ class IterationTransform(Visitor.EnvTransform): ...@@ -259,7 +259,7 @@ class IterationTransform(Visitor.EnvTransform):
return self._transform_reversed_iteration(node, iterator) return self._transform_reversed_iteration(node, iterator)
# range() iteration? # range() iteration?
if Options.convert_range and node.target.type.is_int: if Options.convert_range and (node.target.type.is_int or node.target.type.is_enum):
if iterator.self is None and function.is_name and \ if iterator.self is None and function.is_name and \
function.entry and function.entry.is_builtin and \ function.entry and function.entry.is_builtin and \
function.name in ('range', 'xrange'): function.name in ('range', 'xrange'):
......
...@@ -117,3 +117,23 @@ def test_return(): ...@@ -117,3 +117,23 @@ def test_return():
return i,n return i,n
print print
return "FAILED!" return "FAILED!"
ctypedef enum RangeEnum:
EnumValue1
EnumValue2
EnumValue3
@cython.test_assert_path_exists("//ForFromStatNode")
@cython.test_fail_if_path_exists("//ForInStatNode")
def test_enum_range():
"""
# NOTE: it's not entirely clear that this is the expected behaviour, but that's how it currently is.
>>> test_enum_range()
'RangeEnum'
"""
cdef RangeEnum n = EnumValue3
for i in range(n):
assert cython.typeof(i) == "RangeEnum", cython.typeof(i)
return cython.typeof(i)
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