Commit b69c9efd authored by Stefan Behnel's avatar Stefan Behnel

Make sure that we raise a TypeError for invalid numbers of arguments for range().

Cython incorrectly generated (optimised) range code for "for i in range(1,2,3,4)" as if it saw "for i in range(1,2,3)".
This PR deactivates this optimization when too many arguments are provided, to match the behavior of Python.
Original patch by Max Bachmann.

Closes https://github.com/cython/cython/pull/4550
parent e4d9821f
...@@ -285,7 +285,7 @@ class IterationTransform(Visitor.EnvTransform): ...@@ -285,7 +285,7 @@ class IterationTransform(Visitor.EnvTransform):
return self._transform_reversed_iteration(node, iterable) return self._transform_reversed_iteration(node, iterable)
# range() iteration? # range() iteration?
if Options.convert_range and arg_count >= 1 and ( if Options.convert_range and 1 <= arg_count <= 3 and (
iterable.self is None and iterable.self is None and
function.is_name and function.name in ('range', 'xrange') and function.is_name and function.name in ('range', 'xrange') and
function.entry and function.entry.is_builtin): function.entry and function.entry.is_builtin):
......
...@@ -162,3 +162,12 @@ def for_in_gen(N): ...@@ -162,3 +162,12 @@ def for_in_gen(N):
""" """
for i in range(N): for i in range(N):
yield i yield i
def for_in_range_invalid_arg_count():
"""
>>> for_in_range_invalid_arg_count() # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError: ...
"""
for i in range(1, 2, 3, 4):
pass
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