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

2009-06-29 Thread Georg Brandl

Georg Brandl ge...@python.org added the comment:

Seems to be fixed in current 3k head.

--
resolution:  - out of date
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue4536
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-12-05 Thread Georg Brandl

Georg Brandl [EMAIL PROTECTED] added the comment:

Why is the overflow handling changed at all?

--
nosy: +georg.brandl

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue4536
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-12-05 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc [EMAIL PROTECTED] added the comment:

It's right that the overflow handling path is never never taken:

If step = PyNumber_Index(step) succeeds, 'step' is (a subclass of) an
int, and PyNumber_AsSsize_t cannot fail, not even on low memory
conditions, and large figures are clipped.

But if it were to fail (in some future implementation), the present code
would clear an eventual exception.
Maybe this part could be applied only to 3.1.

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue4536
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-12-05 Thread Laszlo

Laszlo [EMAIL PROTECTED] added the comment:

It is changed from PyErr_Clear() to Py_CLEAR(step) because in case
someone else makes the same mistake of not checking for an error before
calling validate_step() we do not want to hide the error.

It is true that if used properly this part will not get executed, but if
there is another similar programming error like the patch fixes in
range_new() it will at least let the programmer see the right traceback
in the terminal.

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue4536
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-12-04 Thread Laszlo

New submission from Laszlo [EMAIL PROTECTED]:

 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 [EMAIL PROTECTED]
http://bugs.python.org/issue4536
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-12-04 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc [EMAIL PROTECTED] added the comment:

The analysis is good, but there are two problems with your patch:

- it crashes in debug mode, because a Py_INCREF(step) is missing in 
validate_step() (A comment above says: Always returns a new reference)
- it does not work with 

 class Index:
...  def __index__(self):
... return 42
...
 list(range(0, 100, Index()))
TypeError: unsupported operand type(s) for //: 'int' and 'Index'

In short: PyNumber_Index is not an unnecessary call!
But this does not invalidate the other parts of the patch. Would you try again?

--
nosy: +amaury.forgeotdarc

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue4536
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-12-04 Thread Laszlo

Laszlo [EMAIL PROTECTED] added the comment:

Oops, sorry I didn't realize validate_step() is supposed to validate the
input AND return the converted value. Thanks for the feedback. 

This new patch should work fine. It retains only the two final points of
the previous patch; it doesn't clear the error, and it checks for other
NULL values before calling validate_step().

Added file: http://bugs.python.org/file12233/range.diff

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue4536
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-12-04 Thread Laszlo

Changes by Laszlo [EMAIL PROTECTED]:


Removed file: http://bugs.python.org/file12226/range.diff

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue4536
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-12-04 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc [EMAIL PROTECTED] added the comment:

The new patch is good for me.
I added unit tests, someone should review.

--
keywords: +needs review
stage:  - patch review
Added file: http://bugs.python.org/file12235/test_range.patch

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue4536
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com