[New-bugs-announce] [issue4536] SystemError if invalid arguments passed to range() and step=-1

Laszlo report at bugs.python.org
Thu Dec 4 22:20:36 CET 2008


New submission from Laszlo <laszlok2 at gmail.com>:

>>> range(1.0, 0, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

>>> range(1.0, 0, -1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: NULL result without error in PyObject_Call

The error here is that range() does not accept float arguments. However
in the second example the step argument is -1. Since -1 is also used to
indicate a integer overflow, when processing the step argument, it is
assumed that (step == -1 && PyErr_Occurred()) means that an overflow
occured.

However in this particular case, step is supposed to be -1, and the
error is a TypeError from the previous argument which is a float. The
error is then cleared and step is rounded to fit inside an integer.

        start = PyNumber_Index(start);
        stop = PyNumber_Index(stop);
        step = validate_step(step);
        if (!start || !stop || !step)
            goto Fail;

Now in the above code start is NULL, and the if statement checks for
this, and goes to Fail which return NULL. But no error condition is set
(it was cleared before) and NULL raises a SystemError.

My patch changes three things:
* In validate_step(): remove unnecessary 'step = PyNumber_Index(step)',
because this PyNumber_Index conversion is already done in the next line
by PyNumber_AsSsize_t().
* In validate_step(): don't clear the error is the result is -1. The
overflow error is already cleared by PyNumber_AsSsize_t(), and any other
errors should remain.
* In range_new(): check for NULL values before calling validate_step(),
because unlike for the other arguments where we call PyNumber_Index(),
calling validate_step() may clear the previous error.

----------
components: Interpreter Core
files: range.diff
keywords: patch
messages: 76928
nosy: laszlo
severity: normal
status: open
title: SystemError if invalid arguments passed to range() and step=-1
type: behavior
versions: Python 3.0, Python 3.1
Added file: http://bugs.python.org/file12226/range.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4536>
_______________________________________


More information about the New-bugs-announce mailing list